# Android 13 MANAGE\_EXTERNAL\_STORAGE stops working, what can I do?

**URL:** <https://discourse.processing.org/t/android-13-manage-external-storage-stops-working-what-can-i-do/40362>\
**Category:** Processing for Android\
**Created:** [December 29, 2022, 1:00pm UTC](https://discourse.processing.org/t/android-13-manage-external-storage-stops-working-what-can-i-do/40362 "2022-12-29T13:00:06Z")\
**Posts on this page:** 9\
**Page:** 1

<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:** [December 29, 2022, 1:00pm UTC](https://discourse.processing.org/t/android-13-manage-external-storage-stops-working-what-can-i-do/40362/1 "2022-12-29T13:00:06Z")

</div>

I’m using Processing 3.5.4. My app has been working fine on Android 12. Now my phone is updated to Android 13 and my app is no longer working, due to permission changes.

My app needs the permission to generate a file in an directory (in external storage) where user can open it with a 3rd party editor and modify it. Then the app reads the updated file.  
If I use the Processing “data” directory I don’t have permission issue. But I have no idea where I can open the file to edit.

According to what I found, Android 13 no longer allows apps to write to external storage (MANAGE\_EXTERNAL\_STORAGE).

What can I do now?

---

<div class="post-metadata">

**Author:** ![shedMusic](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/shedmusic/32/601_2.png) [@shedMusic](https://discourse.processing.org/u/shedMusic)\
**Post date:** [January 9, 2023, 7:38am UTC](https://discourse.processing.org/t/android-13-manage-external-storage-stops-working-what-can-i-do/40362/2 "2023-01-09T07:38:40Z")

</div>

@bigboss97

Google have changed how external storage can be accessed. It is no longer possible to have simple write external storage permission. This was a real headache for me when my app stopped being able to use a external SD card!

You are going to have to use the android Storage Access Framework … It means learning new stuff, and I initially thought it pointless, but it does have advantages.

You can read about it in the Android API documentation and I will try to post some getting started code for you later today!

---

<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 14, 2023, 11:19am UTC](https://discourse.processing.org/t/android-13-manage-external-storage-stops-working-what-can-i-do/40362/3 "2023-01-14T11:19:11Z")

</div>

Thanks for reply, any update?

---

<div class="post-metadata">

**Author:** ![shedMusic](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/shedmusic/32/601_2.png) [@shedMusic](https://discourse.processing.org/u/shedMusic)\
**Post date:** [January 14, 2023, 12:16pm UTC](https://discourse.processing.org/t/android-13-manage-external-storage-stops-working-what-can-i-do/40362/4 "2023-01-14T12:16:39Z")

</div>

I’ll post code today 😊

---

<div class="post-metadata">

**Author:** ![shedMusic](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/shedmusic/32/601_2.png) [@shedMusic](https://discourse.processing.org/u/shedMusic)\
**Post date:** [January 15, 2023, 8:35am UTC](https://discourse.processing.org/t/android-13-manage-external-storage-stops-working-what-can-i-do/40362/5 "2023-01-15T08:35:21Z")

</div>

@bigboss97

Here it is 😲

```auto
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

```

---

<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 19, 2023, 6:51am UTC](https://discourse.processing.org/t/android-13-manage-external-storage-stops-working-what-can-i-do/40362/6 "2023-01-19T06:51:41Z")

</div>

Thanks for the code. It works beautifully 👍

I have few questions…  
As I can see, the file can only be saved under “Downloads”. That’s fine for me. But is it possible to create sub-directory in that and save the file?  
I also noticed, when I access my phone via PC I see my file saved under “Download” (without ‘s’) and that has one file less than “Downloads”. That’s a bit weird.

---

<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 19, 2023, 11:36am UTC](https://discourse.processing.org/t/android-13-manage-external-storage-stops-working-what-can-i-do/40362/7 "2023-01-19T11:36:52Z")

</div>

Uri is used in the code. I had a look and it’s something like:  
content://com.android.providers.downloads.documents/document/msf%3A1000005220

I’m using _loadJSONObject()_ in my program. So I need a path. I tried:

> JSONObject jo= loadJSONObject( uri.getPath());

But it returned:

> Couldn’t create a reader for /document/msf:1000005220

Then I googled and found something:

> <https://gist.github.com/Zhiw/bdb4e4f8b8bac4b9d288>

The looks so complicated to me. Do I have to do all these or is there a easier way?

---

<div class="post-metadata">

**Author:** ![shedMusic](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/shedmusic/32/601_2.png) [@shedMusic](https://discourse.processing.org/u/shedMusic)\
**Post date:** [January 21, 2023, 8:31pm UTC](https://discourse.processing.org/t/android-13-manage-external-storage-stops-working-what-can-i-do/40362/8 "2023-01-21T20:31:15Z")

</div>

@bigboss97  
You should be able to save or load from anywhere your phone has access to. If I remember correctly, the default is Downloads, but you should be able to navigate to the SD card, create folders etc and save your file.

---

<div class="post-metadata">

**Author:** ![shedMusic](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/shedmusic/32/601_2.png) [@shedMusic](https://discourse.processing.org/u/shedMusic)\
**Post date:** [January 21, 2023, 8:34pm UTC](https://discourse.processing.org/t/android-13-manage-external-storage-stops-working-what-can-i-do/40362/9 "2023-01-21T20:34:50Z")

</div>

@bigboss97

Yes it is using a uri … I too wanted to use a file path but you can’t! I think there may be ways to convert between paths and URIs but I’d have to look into it.

I think you will need to use the DocumentFile class … I want to know how to do this too … so I will try!
