我在這裏有一個圖像:/storage/emulated/0/Pictures/1487951012463.jpg 標題顯然是一個文本。此標題顯示在android 5.1.1的默認圖庫查看器中。 (選擇圖像 - >詳情)如何在android中的圖庫中獲取圖像的MediaStore.Images.ImageColumns.TITLE?
但是從我的應用程序中,我無法讀取它。
有人可以幫我嗎? 我使用的代碼是:
public static void loadTitle(Context context) {
Uri contentUri = Uri.parse("/storage/emulated/0/Pictures/1487951012463.jpg");
String[] projection = new String[] {
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DISPLAY_NAME,
MediaStore.Images.ImageColumns.DATE_TAKEN,
MediaStore.Images.ImageColumns.LATITUDE,
MediaStore.Images.ImageColumns.LONGITUDE,
MediaStore.Images.ImageColumns.DESCRIPTION,
MediaStore.Images.ImageColumns.SIZE,
};
Cursor cursor = context.getContentResolver().query(contentUri, projection, null, null, null);
cursor.moveToFirst();
String title, name;
title = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.TITLE));
name = cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DISPLAY_NAME));
Log.e("Title", title);
Log.e("Display_Name", name);
}
應用崩潰的光標不包含任何內容。
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.database.Cursor.moveToFirst()' on a null object reference
我把這一項活動,如果這重要...
我在做什麼錯?
編輯 - 編輯 - 編輯 - 編輯 - 編輯 - 編輯 - 編輯
我試圖做同樣與ExifInterface因爲它建議。我得到的結果是'null'。
ExifInterface exif = new ExifInterface("/storage/emulated/0/Pictures/1487955364408.jpg");
String x = exif.getAttribute("XPTitle");
Log.e("Title with Exif: ", "X = "+ x);
請幫忙。
謝謝你的回答。你是對的,它是頭部的EXIF。標籤名稱是'XPTitle'(如果是的話)。我試圖使用ExifInterface來獲取該標籤,但是我得到的所有東西都是空的。我昨天在某處讀到ExifInterface不可靠,最好避免它。這就是爲什麼我開始走上另一條路。 因此,你對Uri的評價是,我試圖將路徑轉換爲uri的方式不正確,對吧? Uri contentUri = Uri.parse(「/ storage/emulated/0/Pictures/1487951012463.jpg」); 你能給我推薦一種解決方法嗎?非常感謝 – d4Mn
將contentUri替換爲「MediaStore.Images.Media.EXTERNAL_CONTENT_URI」 –
@ d4Mn:「我昨天在某處讀到ExifInterface不可靠,最好避免它」 - 「MediaStore」本身,用於索引圖像,使用'ExifInterface'實現讀取元數據。第三方應用程序將自己查詢「MediaStore」或使用「ExifInterface」實現。 – CommonsWare