如果選擇了設備的主存儲器,我需要使用正確的方案而不是使用content://com.android.externalstorage.documents/tree/primary:
的DocumentFile或Uri獲取文件。 要獲得文件或圖像的絕對路徑,我需要一個與file:/// storage/emulated/0或storage/emulated/0但我找不到一個方法來獲取正確的Uri用於構建文件以寫入EXIF數據印象。將EXIF數據寫入使用DocumentFile類保存的圖像
我的情況是:
- 用戶選擇保存與
content://com.android.externalstorage.documents
onActivityResult返回URI()圖像的路徑。我將此路徑保存到SharedPreferences中以便稍後使用treeUri.toString()
。 - 用戶拍照和圖像保存
DocumentFile.fromTreeUri(MainActivity.this, Uri.parse(uriString));
- 這其中,我失敗了,獲得該正確地指向圖像文件,烏里與內容://不返回現有image.Correct URI應該
file:///storage/emulated/
,我可以將此Uri轉換爲使用的文件File filePath = new File(URI.create(saveDir.getUri().toString()));
如何獲取使用Uri構建文件或文件所需的Uri,我是從SAF UI獲得的?
編輯:ExifInterface Support Library是Android 7.1 +可以使用InputStream或FileDescriptor引入。
Uri uri; // the URI you've received from the other app
InputStream in;
try {
in = getContentResolver().openInputStream(uri);
ExifInterface exifInterface = new ExifInterface(in);
// Now you can extract any Exif tag you want
// Assuming the image is a JPEG or supported raw format
} catch (IOException e) {
// Handle any errors
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ignored) {}
}
}
注: ExifInterface不會與遠程InputStreams,如那些從HttpURLConnection的返回工作。強烈建議只在content://或file:// URIs中使用它們。
上面的代碼片段顯然是讀取數據,因爲它打開InputStream來讀取數據。我需要能夠將EXIF數據寫入JPEG文件。