Android ExiFinInterface为所有标记返回null

Android ExiFinInterface为所有标记返回null,android,null,uri,exif,getattribute,Android,Null,Uri,Exif,Getattribute,当我调用.getMake()时,我的类有问题;它总是返回null,因此我得到字符串“No Data”。我知道Uri不是空的,因为我每次都会用uripath得到第一个吐司。我还知道图像上有标签“tag_MAKE”(我检查了它)。它甚至不适用于所有其他标签 我应该换什么 public class ExifE { private Uri uri; private ExifInterface exifI; private Context context; public ExifE(Context co

当我调用.getMake()时,我的类有问题;它总是返回null,因此我得到字符串“No Data”。我知道Uri不是空的,因为我每次都会用uripath得到第一个吐司。我还知道图像上有标签“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;
}

解决方案

创建ExiFinInterface时出现问题。 我不能使用
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);
    }
}

要澄清您当前的状态:您说您从
getMake()
方法得到了
无数据
响应。因此您没有从
createExifI
方法中获得任何
异常。因为如果发生
异常
,您的
exifI
实例将为
NULL
,您将从
getMake()
方法获得
NPE
,但您没有


如果上面的部分是正确的,那么问题的唯一原因是没有任何带有tag
ExifInterface.tag\u MAKE
的属性用于该
ExifInterface
实例。您可以尝试列出(记录/打印)该
ExifInterface
实例的所有其他属性,以确保其他属性存在,但标记不存在。

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