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


#1

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 …


#2

@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)


#3

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);

#4

have you added permission???


#5

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>

#6

@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!


#7

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


#8

No, you have to ask them by code…


#9

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


#10

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


#11

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");
    // ...
  }
}