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..
trippedout
Try this:
Resources resources = context.getResources();
Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + resources.getResourcePackageName(resId) + '/' + resources.getResourceTypeName(resId) + '/' + resources.getResourceEntryName(resId) );
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");
xnagyg
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) );
zephyr
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);
来源:https://stackoverflow.com/questions/19566840/get-uri-from-drawable-image