我在这里有一个图像:/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