[SOLVED] Show image stored in SDCARD (sdk 26)

Hello, I have try many tutorials/issues whitout succes, but i’have got the same results : java.lang.IllegalArgumentException: File /storage/emulated/0/DCIM/cat.jpg contains a path separator

Here is my code :

import android.os.Environment;
PImage img;

void setup() {
  String path=new String(android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DCIM).getAbsolutePath());
  println(path);
  img = loadImage(sketchPath(path+"/cat.jpg"));
}

I think it’s due to api 26 …

1 Like

@julienrat===

That has nothing to see with API and this error was solved 20 times on this forum: Environment.getExternalStoragePublicDirectory(…) returns a FILE and a file cannot be called “/dossier1/directory2/subfolder4/monfichier.jpg” but only “monfichier.jpg” !
So, as explained in the other posts, instead of your code you have to write:

File myFile = Environment.getExternalStoragePublicDirectory(...));
String myPath= myFile.toString();
PImage img = loadImage(myPath+"/monFichier.jpg");/// if needed add subfolders!
image(img, 0,0);

that s all…If you have got write/read permission on runtime (depending of your API)

1 Like

Hi ! Many thx for your response !
I have tested your code but results are the same …

java.lang.IllegalArgumentException: File /storage/emulated/0/DCIM/cat.jpg contains a path separator

import android.os.Environment;

File myFile = Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DCIM);
String myPath= myFile.toString();
print(myPath);
PImage img = loadImage(myPath+"/cat.jpg");/// if needed add subfolders!
image(img, 0,0);

have you added permission???

Here is my manifest file


<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="">
    <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="26"/>
    <application android:icon="@drawable/icon" android:label="">
        <activity android:name=".MainActivity" android:theme="@style/Theme.AppCompat.Light.NoActionBar.FullScreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>

@julienrat===
adding permission with the Manifest does not work anymore: you have to ask for permission on run time by code; yet, if you look at your app when it crashes i think that you can see under the crash pop up another one which is “this app is asking permission bla bla”) and (only for testing!) you can (perhaps) acceed to this popup and agree…though it is not at all the good way!

Permissions was set with the processing IDE not directly in manifest file :slight_smile:

No, you have to ask them by code…

Yes i understand now
something like your post ? :slight_smile:

1 Like

@julienrat===
exactly!
(dont copy/paste without changing my typo new string[])

SOLVED ! Thx a lot !
There is a builtin function https://android.processing.org/reference/permissions/request.html

Here is my code

import android.os.Environment;
PImage img;
File myFile = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DCIM);
void setup() {
  requestPermission("android.permission.READ_EXTERNAL_STORAGE", "initRead");
  requestPermission("android.permission.WRITE_EXTERNAL_STORAGE", "initWrite");
}
void draw() {

}

void initRead(boolean granted) {
  if (granted) {   
    println("init read sdcard OK");
    String myPath= myFile.toString();
    img = loadImage(myPath+"/cat.jpg");/// if needed add subfolders!
    image(img,0,0);
    // ...
  } else {
    println("Read SDcard is not available");
    // ...
  }
}
void initWrite(boolean granted) {
  if (granted) {   
    println("init write sdcard OK");
    // ...
  } else {
    println("Write SDcard is not available");
    // ...
  }
}
1 Like

HI,
I am new to developing apps for android phones using processing and am testing the ability to write to, read from external storage like the sd card and came across this post. However the above code with requestPermission(…) method does not seem to work with android phones with versions above android pie (SDK 28), so does not work with SDK 29,30 for example. I get the error:

AndroidDevice: cannot find process id, console output will be disabled.

followed by:

"Read SDcard is not available"
"Write SDcard is not available"

I tried setting android:targetSdkVersion=“28”/> in the andriodManifest.xml file but this had no effect.

I also looked on the android .developer website which seems to indicate that the permissions setting methods for post SDK28 seems to have changed but this just confused me. Is there an updated method for setting permissions to access directories/files etc on external storage using processing?

Is there a tutorial related to this?

Any help is much appreciated.

Cheers.

Hi,
And thanks for the reply.

I have since found that the requestPermission(…) method does seem to work as long as the associated definitely exists in the manifest file as well as being coded. Dont assume that it does even though it has been added in the code. Is this normal behaviour?

I have also found that when the application is first run on a device a popup occurs asking for permission to use the particular feature but after that every time the app is run is doesnt - is this normal behaviour?

Thanks for all your help.

Cheers.

for api 28 and above

public class Permission{
  
  PApplet parent;
  String p;

  public Permission(PApplet pParent,String permissionName) {
    parent = pParent;
    p = permissionName;
    parent.requestPermission("android.permission."+permissionName, "onPermissionResult", this);
  };

  public void onPermissionResult(boolean granted) {
    if (!granted) {
      PApplet.println("User did not grant", p ,"permission.", p ,"is disabled.");
    }
  };

};

//make sure to check permission in sketch permissions
void setup(){
// change the string value to desired permission
Permission p = new Permission(this, "WRITE_EXTERNAL_STORAGE");
}