【发布时间】:2014-11-03 16:10:19
【问题描述】:
我需要在 android 应用程序中获取我的可绘制图像的绝对路径或 File 对象。有没有办法做到这一点?
可绘制如下
context.getResources().getDrawable(R.drawable.back_button)
【问题讨论】:
-
by drawable id 我已经获取了 Drawable 对象。我需要得到那个的绝对路径
我需要在 android 应用程序中获取我的可绘制图像的绝对路径或 File 对象。有没有办法做到这一点?
可绘制如下
context.getResources().getDrawable(R.drawable.back_button)
【问题讨论】:
如果您的应用安装在 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);
}
【讨论】:
Uri path = Uri.parse("android.resource://com.nikhil.material/" + R.drawable.image);
或
Uri otherPath = Uri.parse("android.resource://com.nikhil.material/drawable/image");
或
Uri path = Uri.parse("android.resource://"+BuildConfig.APPLICATION_ID+"/" + R.drawable.image);
【讨论】:
BuildConfig.APPLICATION_ID like Uri.parse("android.resource://"+BuildConfig.APPLICATION_ID+"/" + R.drawable.image); idea from -> stackoverflow.com/a/29109743/1815624
可绘制资源在运行时添加到二进制文件中,因此无法通过路径访问。 你可以解码并使用drawables。
如果您需要可以在运行时通过路径访问的资源,请尝试使用 assets 文件夹。
希望对你有帮助
【讨论】: