Get URI from drawable image

送分小仙女□ 提交于 2019-11-28 12:03:56
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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!