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

**URL:** <https://discourse.processing.org/t/solved-show-image-stored-in-sdcard-sdk-26/4397>\
**Category:** Processing for Android\
**Created:** [October 12, 2018, 3:42pm UTC](https://discourse.processing.org/t/solved-show-image-stored-in-sdcard-sdk-26/4397 "2018-10-12T15:42:38Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![julienrat](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@julienrat](https://discourse.processing.org/u/julienrat)\
**Post date:** [October 12, 2018, 3:42pm UTC](https://discourse.processing.org/t/solved-show-image-stored-in-sdcard-sdk-26/4397/1 "2018-10-12T15:42:38Z")

</div>

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 :

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

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [October 13, 2018, 8:55am UTC](https://discourse.processing.org/t/solved-show-image-stored-in-sdcard-sdk-26/4397/2 "2018-10-13T08:55:02Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![julienrat](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@julienrat](https://discourse.processing.org/u/julienrat)\
**Post date:** [October 13, 2018, 9:51am UTC](https://discourse.processing.org/t/solved-show-image-stored-in-sdcard-sdk-26/4397/3 "2018-10-13T09:51:33Z")

</div>

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`

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

```

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [October 13, 2018, 9:53am UTC](https://discourse.processing.org/t/solved-show-image-stored-in-sdcard-sdk-26/4397/4 "2018-10-13T09:53:09Z")

</div>

have you added permission???

---

<div class="post-metadata">

**Author:** ![julienrat](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@julienrat](https://discourse.processing.org/u/julienrat)\
**Post date:** [October 13, 2018, 9:54am UTC](https://discourse.processing.org/t/solved-show-image-stored-in-sdcard-sdk-26/4397/5 "2018-10-13T09:54:24Z")

</div>

Here is my manifest file

```auto

<?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>

```

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [October 13, 2018, 9:59am UTC](https://discourse.processing.org/t/solved-show-image-stored-in-sdcard-sdk-26/4397/6 "2018-10-13T09:59:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![julienrat](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@julienrat](https://discourse.processing.org/u/julienrat)\
**Post date:** [October 13, 2018, 10:01am UTC](https://discourse.processing.org/t/solved-show-image-stored-in-sdcard-sdk-26/4397/7 "2018-10-13T10:01:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [October 13, 2018, 10:26am UTC](https://discourse.processing.org/t/solved-show-image-stored-in-sdcard-sdk-26/4397/8 "2018-10-13T10:26:58Z")

</div>

No, you have to ask them by code…

---

<div class="post-metadata">

**Author:** ![julienrat](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@julienrat](https://discourse.processing.org/u/julienrat)\
**Post date:** [October 13, 2018, 10:28am UTC](https://discourse.processing.org/t/solved-show-image-stored-in-sdcard-sdk-26/4397/9 "2018-10-13T10:28:48Z")

</div>

Yes i understand now  
something like your post ? 🙂

> [@Porting sketch to Android Studio - Problem with request permission](https://discourse.processing.org/t/porting-sketch-to-android-studio-problem-with-request-permission/3454/5):
>
> OK, act.requestPermissions( new String[]{Manifest.permission.WRITE\_EXTERNAL\_STORAGE}, 10); works as far as it is now asking for permission. still no file written by Table class. Hm, maybe i got another mistake in my code. testing… thanks a lot act = this.getActivity(); I missed that one.

---

<div class="post-metadata">

**Author:** ![akenaton](https://avatars.discourse-cdn.com/v4/letter/a/a88e4f/32.png) [@akenaton](https://discourse.processing.org/u/akenaton)\
**Post date:** [October 13, 2018, 10:31am UTC](https://discourse.processing.org/t/solved-show-image-stored-in-sdcard-sdk-26/4397/10 "2018-10-13T10:31:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![julienrat](https://avatars.discourse-cdn.com/v4/letter/j/7ea924/32.png) [@julienrat](https://discourse.processing.org/u/julienrat)\
**Post date:** [October 13, 2018, 10:45am UTC](https://discourse.processing.org/t/solved-show-image-stored-in-sdcard-sdk-26/4397/11 "2018-10-13T10:45:50Z")

</div>

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

Here is my code

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

```

---

<div class="post-metadata">

**Author:** ![katesfb](https://avatars.discourse-cdn.com/v4/letter/k/57b2e6/32.png) [@katesfb](https://discourse.processing.org/u/katesfb)\
**Post date:** [May 26, 2021, 4:58am UTC](https://discourse.processing.org/t/solved-show-image-stored-in-sdcard-sdk-26/4397/12 "2021-05-26T04:58:28Z")

</div>

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:

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

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [May 26, 2021, 5:02am UTC](https://discourse.processing.org/t/solved-show-image-stored-in-sdcard-sdk-26/4397/13 "2021-05-26T05:02:21Z")

</div>

> [@fileInput class android, pick Folder or File](https://discourse.processing.org/t/fileinput-class-android-pick-folder-or-file/30148):
>
> Here is a class to handle file input in android. here is the base sketch; 
> 
> > **Summary**
> >
> > import android.app.Activity; import android.content.Intent; import processing.core.PApplet; import android.os.Bundle; import android.os.Environment; import android.content.Context; import android.provider.MediaStore; import android.net.Uri; import android.graphics.BitmapFactory; import android.database.Cursor; import android.os.Build.VERSION\_CODES; import android.os.Build; import android.graphics.Bitmap; fileInp…

---

<div class="post-metadata">

**Author:** ![katesfb](https://avatars.discourse-cdn.com/v4/letter/k/57b2e6/32.png) [@katesfb](https://discourse.processing.org/u/katesfb)\
**Post date:** [May 31, 2021, 10:12am UTC](https://discourse.processing.org/t/solved-show-image-stored-in-sdcard-sdk-26/4397/14 "2021-05-31T10:12:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [May 31, 2021, 10:18am UTC](https://discourse.processing.org/t/solved-show-image-stored-in-sdcard-sdk-26/4397/15 "2021-05-31T10:18:59Z")

</div>

for api 28 and above

```auto
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");
}

```
