Permission issue in reading file from external storage

Thanks for your time. Happy new year!

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

Permission p = new Permission(this,"WRITE_EXTERNAL_STORAGE");
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.");
    }
  };

};

1 Like

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

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:

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.

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


I had to allow all files. Then all work :grinning_face_with_smiling_eyes:

Again, thank you for your time, @jafal :+1:

that is good you could do it have nice time