Android 13 MANAGE_EXTERNAL_STORAGE stops working, what can I do?

@bigboss97

Here it is :astonished:

import android.content.Intent;
import android.net.Uri;
import android.provider.DocumentsContract;
import android.content.Context;
import android.content.ContentResolver;
import android.app.Activity;


Context context ;
Activity activity ;
byte[] saveArray = {0, 1, 2, 3, 4, 5, 6, 7} ; //dummy data
byte[] loadArray ;
String displayText1 = "tap HERE to save";
String displayText2 = "tap HERE to load";
int REQUEST_CODE ;

void setup() {
  activity = getActivity();
  context = getContext();
  fill(255);
  textSize(width/20) ;
  textAlign(CENTER, CENTER);
}

void draw() {
  background(0);
  text(displayText1, 0, 0, width, height/2) ;
  text(displayText2, 0, height/2, width, height/2) ;
}

void mouseReleased() {
  if (mouseY <height/2) {
    //start an Intent to save
    REQUEST_CODE = 1;
    Intent saveIntent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
    saveIntent.setType("*/*");
    activity.startActivityForResult(saveIntent, REQUEST_CODE);
  } else {
    //start an Intent to load
    REQUEST_CODE = 2;
    Intent loadIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    loadIntent.setType("*/*"); //any file type
    activity.startActivityForResult(loadIntent, REQUEST_CODE);
  }
}

@Override
  public void onActivityResult(int requestCode, int resultCode, Intent resultData) {

  if (resultCode == Activity.RESULT_OK && resultData != null) {
    ContentResolver contentResolver = context.getContentResolver();
    Uri uri = resultData.getData();


    switch (REQUEST_CODE) {
    case 1: // save file
      try {
        OutputStream os = contentResolver.openOutputStream(uri);
        os.write(saveArray);
        os.close();
        displayText1 = "File saved sccessfully" ;
        //saveArray should now be saved
      }
      catch (Exception e) {
        displayText1 = "File Save Error ..." ;
      }
      break;
    case 2:
      try { //load file
        InputStream is = contentResolver.openInputStream(uri);
        loadArray = new byte[0] ;
        int i = is.read();
        while (i != -1) {
          loadArray = expand(loadArray, loadArray.length + 1);
          loadArray[loadArray.length -1] = (byte) i;
          i = is.read();
        }
        is.close();
        displayText2 = "File loaded successfully"  ;
        //do whatever you need with loadArray ....
      }
      catch (Exception e) {
        displayText2 = "File Load Error ..." ;
      }
      break;
    }
  }
}//end onActivityResult

1 Like