Permission issue in reading file from external storage

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:

1 Like