要获得一个drawable,你可以用这个sn-p:
Drawable drawable = getResources().getDrawable(android.R.drawable.your_drawable);
我不确定你的方式是否正确,因为我从未见过这样的方式。您真的需要图像的路径才能在 ExifInterface 类上使用它吗?
好的,我进行了一些挖掘并找到了this question,这使我找到了this one。看起来,您可以不从您的 apk 中的资源获取绝对路径。一个好的解决方案是你将它作为文件保存在外部存储器上,然后你就可以得到你想要的路径。
首先,将此添加到您的 AndroidManifest.xml 中,以便您的应用可以写入手机内存:
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
好的,要保存它,你可以试试这个,首先从你的可绘制资源创建一个位图:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.your_drawable);
之后获取你要保存图片的路径,并将其放在String 上。有关here 的更多信息。
Android 文档有一个很好的例子来说明如何获取路径。你可以看到它here。
为简单起见,我将从文档中复制并粘贴 sn-p。
void createExternalStoragePrivateFile() {
// Create a path where we will place our private file on external
// storage.
File file = new File(getExternalFilesDir(null), "DemoFile.jpg");
try {
// Very simple code to copy a picture from the application's
// resource into the external file. Note that this code does
// no error checking, and assumes the picture is small (does not
// try to copy it in chunks). Note that if external storage is
// not currently mounted this will silently fail.
InputStream is = getResources().openRawResource(R.drawable.balloons);
OutputStream os = new FileOutputStream(file);
byte[] data = new byte[is.available()];
is.read(data);
os.write(data);
is.close();
os.close();
} catch (IOException e) {
// Unable to create file, likely because external storage is
// not currently mounted.
Log.w("ExternalStorage", "Error writing " + file, e);
}
}
void deleteExternalStoragePrivateFile() {
// Get path for the file on external storage. If external
// storage is not currently mounted this will fail.
File file = new File(getExternalFilesDir(null), "DemoFile.jpg");
if (file != null) {
file.delete();
}
}
boolean hasExternalStoragePrivateFile() {
// Get path for the file on external storage. If external
// storage is not currently mounted this will fail.
File file = new File(getExternalFilesDir(null), "DemoFile.jpg");
if (file != null) {
return file.exists();
}
return false;
}
然后,获取你保存在外部存储器上的文件的路径,然后做你想做的。
我也会保留旧示例。可以使用getExternalStorageDirectory()方法获取路径,或者getExternalCacheDir()。之后,您可以使用名为getAbsolutePath() 的File 方法来获取您的String。
String path = (...) // (you can choose where to save here.)
File file = new File(path, "your_drawable.png");
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); // You can change the quality from 0 to 100 here, and the format of the file. It can be PNG, JPEG or WEBP.
out.flush();
out.close();
有关 Bitmap 类的更多信息,请查看the docs。
如果您需要更多信息,请告诉我,我会尝试展示更多示例。
编辑:我看到了你的链接,那里有这个sn-p:
//change with the filename & location of your photo file
String filename = "/sdcard/DSC_3509.JPG";
try {
ExifInterface exif = new ExifInterface(filename);
ShowExif(exif);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, "Error!",
Toast.LENGTH_LONG).show();
}
如您所见,如果您真的想查看内部图像资源的exif数据,则必须将其保存在其他地方,然后您可以尝试获取该文件的绝对路径,然后调用显示exif的方法。