# Permission issue in reading file from external storage

**URL:** <https://discourse.processing.org/t/permission-issue-in-reading-file-from-external-storage/34184>\
**Category:** Processing for Android\
**Created:** [December 18, 2021, 1:13pm UTC](https://discourse.processing.org/t/permission-issue-in-reading-file-from-external-storage/34184 "2021-12-18T13:13:46Z")\
**Posts on this page:** 5\
**Page:** 3

<div class="post-metadata">

**Author:** ![bigboss97](https://avatars.discourse-cdn.com/v4/letter/b/47e85d/32.png) [@bigboss97](https://discourse.processing.org/u/bigboss97)\
**Post date:** [January 1, 2022, 4:52am UTC](https://discourse.processing.org/t/permission-issue-in-reading-file-from-external-storage/34184/41 "2022-01-01T04:52:19Z")

</div>

Thanks for your time. Happy new year!

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [January 1, 2022, 1:47pm UTC](https://discourse.processing.org/t/permission-issue-in-reading-file-from-external-storage/34184/42 "2022-01-01T13:47:13Z")

</div>

Not sure if this code works past Android 10 but works on all other versions.

Make sure to also set permissions in the desktop app or apde. Set target api to 28

Delete original install and reinstall. You have to make sure its deleted first or you wont get the permission prompt.

Finally

```auto
Permission p = new Permission(this,"WRITE_EXTERNAL_STORAGE");

```

```auto
public class Permission{
  
  PApplet parent;
  
  public boolean requestedPortraitImage = false;

  public Permission(PApplet pParent,String permissionName) {
    parent = pParent;
    parent.requestPermission("android.permission."+permissionName, "onPermissionResult", this);
    println(permissionName);
  };

  public void onPermissionResult(boolean granted) {
    if (!granted) {
      PApplet.println("User did not grant camera permission. Camera is disabled.");
    }
  };

};

```

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [January 1, 2022, 8:15pm UTC](https://discourse.processing.org/t/permission-issue-in-reading-file-from-external-storage/34184/43 "2022-01-01T20:15:51Z")

</div>

> [@paulgoux](#):
>
> Not sure if this code works past Android 10 but works on all other versions.
> 
> Make sure to also set permissionis in the desktop app or apde. Set target api to 28

hi  
months ago  
i tried all your topics about load save they all run on android 10

---

<div class="post-metadata">

**Author:** ![bigboss97](https://avatars.discourse-cdn.com/v4/letter/b/47e85d/32.png) [@bigboss97](https://discourse.processing.org/u/bigboss97)\
**Post date:** [January 2, 2022, 3:52am UTC](https://discourse.processing.org/t/permission-issue-in-reading-file-from-external-storage/34184/44 "2022-01-02T03:52:39Z")

</div>

OK, I got it working on Android 11. But first of all a huge THANK to @jafal with all the sample codes.  
I modified the loadImage() routine for my purpose:

```auto
import android.os.Environment;
import java.io.FileInputStream;
import android.Manifest; 
import android.content.pm.PackageManager;
import android.app.Activity; 
import android.os.Build; 
import android.os.Build.VERSION_CODES; 

Activity activity; 
JSONObject json;

private static String[] PERMISSIONS_STORAGE = { Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE }; 
private void requestParticularPermission() { 
  activity.requestPermissions(PERMISSIONS_STORAGE, 2020);
}
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
  switch (requestCode) { 
  case 2020: 
    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
      println("permissions granted");
    } else { 
      println("permissions not granted");
    } 
    break; 
  default: 
    activity.onRequestPermissionsResult(requestCode, permissions, grantResults);
  }
}

void setup() {
  activity = this.getActivity();
  if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1)
    requestParticularPermission();
}

void draw() {
}

void mousePressed() {
  String dir= new String(Environment.getExternalStorageDirectory().getAbsolutePath());
  try {
    json = loadJSONObject( dir+"/twinkBOSS/env.txt");
    println( json.getString("ip"));
  }
  catch ( Exception e) {
    println(e);
  }
}

```

When the above code is run there will be a popup asking for permission, but photos and media only. That’s the reason that loading image wasn’t an issue.

 ![Screenshot_20220102-143530_Permission controller](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/2e39fb31466ad65aeb75f882e835ee4f3add1669.png)

When I checked the app on the phone I saw the issue:

 ![Screenshot_20220102-143818_Permission controller](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/a58ad125f8bf0efc00baaf4a5d28c24755b7b42a.png)  
I had to allow all files. Then all work 😄

Again, thank you for your time, @jafal 👍

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [January 2, 2022, 7:37am UTC](https://discourse.processing.org/t/permission-issue-in-reading-file-from-external-storage/34184/45 "2022-01-02T07:37:39Z")

</div>

> [@bigboss97](#):
>
> OK, I got it working on Android 11.

that is good you could do it have nice time

[Previous page](https://discourse.processing.org/t/permission-issue-in-reading-file-from-external-storage/34184.md?page=2)
