12

How can I get the URI of image saved in drawable. I have tried following formats, but everytime it cannot load the image.

imageURI= Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.indoor_thumbnail1);
imageURI=Uri.parse("android.resource://"+getPackageName()+"/drawables/imageName");
 imageURI=Uri.parse("android.resource://"+getPackageName()+"/drawables/imageName.png");
imageURI = Uri.parse("android.resource://"+ getResources().getResourceTypeName(R.drawable.indoor_thumbnail1)+"/" +getResources().getResourceEntryName(R.drawable.indoor_thumbnail1)+".png" );

Don't know why I can't fetch the image URI..

Bhavna
  • 836
  • 2
  • 6
  • 19

4 Answers4

17

Try this:

Resources resources = context.getResources();
Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + resources.getResourcePackageName(resId) + '/' + resources.getResourceTypeName(resId) + '/' + resources.getResourceEntryName(resId) );
Drew Gaynor
  • 8,292
  • 5
  • 40
  • 53
trippedout
  • 1,561
  • 13
  • 20
11

I found most of the answers confusing for a novice user so i am including an example.

your_package_name = org.xyz.abc

image in drawable is => myimage.jpg

Uri uri = Uri.parse("android.resource://org.xyz.abc/drawable/myimage");
or 
Uri uri = Uri.parse("android.resource://"+context.getPackageName()+"/drawable/myimage");
jaipster
  • 11,967
  • 2
  • 21
  • 24
10

This is what you really need:

Uri imageUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE +
"://" + getResources().getResourcePackageName(R.drawable.ic_launcher)
+ '/' + getResources().getResourceTypeName(R.drawable.ic_launcher) + '/' + getResources().getResourceEntryName(R.drawable.ic_launcher) );
Daniel Nyamasyo
  • 2,152
  • 1
  • 24
  • 23
xnagyg
  • 4,784
  • 2
  • 32
  • 24
  • 1
    I believe that it should be: Uri imageUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getResources().getResourcePackageName(R.drawable.ic_launcher) + '/' + getResources().getResourceTypeName(R.drawable.ic_launcher) + '/' + String.valueOf(R.drawable.ic_launcher ); – fobbymaster Nov 21 '15 at 01:22
1

You can also try this:

    Bitmap bm = BitmapFactory.decodeResource( getResources(), R.drawable.myimage_name);

String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
            File file = new File(extStorageDirectory, "MyIMG.png");
            FileOutputStream outStream = null;
            try {
                outStream = new FileOutputStream(file);
                bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
                outStream.flush();
                outStream.close();

    } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

    Uri imguri=Uri.fromFile(file);
legoscia
  • 39,593
  • 22
  • 116
  • 167
zephyr
  • 665
  • 6
  • 19