Get URI from drawable image

后端 未结 4 1734
忘掉有多难
忘掉有多难 2020-12-10 13:31

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.resou         


        
相关标签:
4条回答
  • 2020-12-10 14:08

    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);
    
    0 讨论(0)
  • 2020-12-10 14:19

    Try this:

    Resources resources = context.getResources();
    Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + resources.getResourcePackageName(resId) + '/' + resources.getResourceTypeName(resId) + '/' + resources.getResourceEntryName(resId) );
    
    0 讨论(0)
  • 2020-12-10 14:24

    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");
    
    0 讨论(0)
  • 2020-12-10 14:32

    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) );
    
    0 讨论(0)
提交回复
热议问题