Get URI from drawable image

后端 未结 4 1733
忘掉有多难
忘掉有多难 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);
    

提交回复
热议问题