Get absolute path of android drawable image

烈酒焚心 提交于 2019-11-28 10:13:35

问题


I need to get the absolute path or the File object of my drawable image in android application. Is there any way to do this?

Drawable is as following

context.getResources().getDrawable(R.drawable.back_button)


回答1:


If you app is insalled on sdcard try this code :

Bitmap bitMap = BitmapFactory.decodeResource(getResources(),R.id.img1);

File mFile1 = Environment.getExternalStorageDirectory();

String fileName ="img1.jpg";

File mFile2 = new File(mFile1,fileName);
try {
       FileOutputStream outStream;

       outStream = new FileOutputStream(mFile2);

       bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);

       outStream.flush();

       outStream.close();

} catch (FileNotFoundException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
} catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
}

String sdPath = mFile1.getAbsolutePath().toString()+"/"+fileName;

Log.i("hiya", "Your IMAGE ABSOLUTE PATH:-"+sdPath); 

File temp=new File(sdPath);

if(!temp.exists()){
       Log.e("file","no image file at location :"+sdPath);
}



回答2:


Uri path = Uri.parse("android.resource://com.nikhil.material/" + R.drawable.image);

OR

Uri otherPath = Uri.parse("android.resource://com.nikhil.material/drawable/image");

OR

Uri path = Uri.parse("android.resource://"+BuildConfig.APPLICATION_ID+"/" + R.drawable.image);



回答3:


drawable resources are added to binary at run time therefore cannot be accessed by path. you can decode and use drawables instead.

If you need resources you can access at runtime by path, try using assets folder instead.

Hope this is helpful



来源:https://stackoverflow.com/questions/25758159/get-absolute-path-of-android-drawable-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!