我需要在Android应用程序中获取可绘制图像的绝对路径或File对象。有没有办法做到这一点?
Drawable如下
context.getResources()。getDrawable(R.drawable.back_button)
答案 0 :(得分:7)
如果您的应用已安装在SD卡上,请尝试以下代码:
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);
}
答案 1 :(得分:6)
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);
答案 2 :(得分:2)
可绘制资源在运行时添加到二进制文件,因此无法通过路径访问。 你可以解码并使用drawables。
如果您需要可以在运行时通过路径访问的资源,请尝试使用assets文件夹。
希望这是有帮助的