获取android可绘制图像的绝对路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25758159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get absolute path of android drawable image
提问by Gayan Dinuzhka
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?
我需要在 android 应用程序中获取我的可绘制图像的绝对路径或 File 对象。有没有办法做到这一点?
Drawable is as following
可绘制如下
context.getResources().getDrawable(R.drawable.back_button)
context.getResources().getDrawable(R.drawable.back_button)
回答by KOTIOS
If you app is insalled on sdcard try this code :
如果您的应用程序安装在 sdcard 上,请尝试以下代码:
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);
}
回答by Nikhil Borad
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);
回答by Farai
drawable resources are added to binary at run time therefore cannot be accessed by path. you can decode and use drawables instead.
drawable 资源在运行时被添加到二进制文件中,因此无法通过路径访问。您可以解码并使用可绘制对象。
If you need resources you can access at runtime by path, try using assets folder instead.
如果您需要可以在运行时通过路径访问的资源,请尝试使用 assets 文件夹。
Hope this is helpful
希望这有帮助