【问题标题】:ExifInterface returns null for all TagsExifInterface 为所有标签返回 null
【发布时间】:2013-11-13 17:46:49
【问题描述】:

当我调用 .getMake(); 时,我的班级有问题它总是返回 null,所以我得到字符串“No Data”。我知道 Uri 不是空的,因为我每次都用 uripath 得到第一个 Toast。我也知道图像有标签“TAG_MAKE”(我检查过)。它甚至不适用于所有其他标签。

我应该改变什么?

public class ExifE { private Uri uri;
private ExifInterface exifI;
private Context context;

public ExifE(Context con) {
    context = con;
    SharedPreferences prefs = context.getSharedPreferences("prefs", context.MODE_PRIVATE);
    uri = Uri.parse(myPrefs.getString("currentImageUri", "fail"));
    Toast.makeText(context, uri.getPath(), Toast.LENGTH_SHORT).show();
    try {
        this.createExifI(uri.getPath());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public void createExifI(String filePath) throws IOException {
    this.exifI = new ExifInterface(filePath);
}

public String getMake() {
    String make;
    if (exifI.getAttribute(ExifInterface.TAG_MAKE) != null) {
        make = exifI.getAttribute(ExifInterface.TAG_MAKE);
    } else {
        make = "No Data";
    }
    return make;
}

解决方案

创建 ExifInterface 时出现问题。 我不能使用 uri.getPath(); ,我必须调用它来获取真正的文件路径而不是 MediaStore 路径。

private String getRealPathFromURI(Uri contentURI, Activity activity) {
    Cursor cursor = activity.getContentResolver().query(contentURI, null, null, null, null);
    if (cursor == null) { // Source is Dropbox or other similar local file
                            // path
        return contentURI.getPath();
    } else {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        return cursor.getString(idx);
    }
}

【问题讨论】:

  • 您确定exifI 创建正确吗?你什么时候打电话给getMake()?您是否尝试使用调试器跟踪您的代码?我建议您在调用 getAttribute() 之前检查 exifI 是否为空。
  • 我创建的 ExifInterface 是假的。我不能使用uri.getPath() 我必须使用真正的路径而不是 MediaStore 路径所以我必须调用它:` private String getRealPathFromURI(Uri contentURI, Activity activity) { Cursor cursor = activity.getContentResolver().query(contentURI,空,空,空,空); if (cursor == null) { // 来源是 Dropbox 或其他类似的本地文件 // 路径 return contentURI.getPath(); } else { cursor.moveToFirst(); int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);返回 cursor.getString(idx); } }`

标签: android null uri exif getattribute


【解决方案1】:

为了说明你目前的状态:你说你从getMake()方法得到"No Data"响应。因此;你没有从createExifI 方法中得到任何Exception。因为如果发生Exception,您的exifI 实例将是NULL,您将从getMake() 方法获得NPE,但您没有。

如果上述部分是正确的,那么问题的唯一原因是该ExifInterface 实例没有任何带有标签ExifInterface.TAG_MAKE 的属性。您可以尝试列出(记录/打印)该 ExifInterface 实例的所有其他属性,以确保存在除 TAG_MAKE 之外的其他属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-09
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多