Following short code will open Android’s File explorer.
In this example only txt files are admitted and displayed in console, but of course you can customize for every extension as you see fit.
Important! Give read permission!
What Android version are you using/targeting? It’s correct to use the Intent approach but I’m not sure using getExternalStorageDirectory() will work on the most recent?
What you have done will work for your phone but won’t for a new device because Google made big changes to external storage access for security reasons. Instead of being able to use getExternalStorageDirectory() etc from which you get a file path, you have to use the URI provided by your intent along with DocumentFileif necessary.
I’m not sure either whether loadStrings() will work as this takes file path and the new method provides a URI.
I posted some code a whil back and this is slightly changed version … for the way load is handled. Basically you get a byte array holding you file and you then have to do whatever… eg convert to strings etc
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);
int fileSize = is.available ;
loadArray = new byte[fileSize] ;
for (int i=0; i<fileSize; I++) {
loadArray[i] = (byte) is.read();
}
is.close();
displayText2 = "File loaded successfully" ;
//do whatever you need with loadArray ....
}
catch (Exception e) {
displayText2 = "File Load Error ..." ;
}
break;
}
}
}//end onActivityResult