Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 尝试获取图像方向时出现NullPointerException_Android_Nullpointerexception_Android Camera_Android Cursor - Fatal编程技术网

Android 尝试获取图像方向时出现NullPointerException

Android 尝试获取图像方向时出现NullPointerException,android,nullpointerexception,android-camera,android-cursor,Android,Nullpointerexception,Android Camera,Android Cursor,嘿,我似乎不明白这个错误。 我正在尝试通过拍照或从“多媒体资料”中选择图像。 当我在选定的图像上尝试该方法时,效果很好,但当我从相机拍摄图像时,我在光标.close()行上得到错误 我有以下代码从gallery中捕获图像: if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) { Uri selectedImage = mImageUri;

嘿,我似乎不明白这个错误。 我正在尝试通过拍照或从“多媒体资料”中选择图像。 当我在选定的图像上尝试该方法时,效果很好,但当我从相机拍摄图像时,我在
光标.close()
行上得到错误

我有以下代码从gallery中捕获图像:

    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {  
    Uri selectedImage = mImageUri;
    getContentResolver().notifyChange(selectedImage, null);
    ImageView imageView = (ImageView) findViewById(R.id.chosenImage2);
    ContentResolver cr = getContentResolver();

    try {
         bitmap = android.provider.MediaStore.Images.Media
         .getBitmap(cr, selectedImage);
         //flip image if needed
         bitmap = Helpers.flipBitmap(bitmap, Helpers.getOrientation(this, selectedImage));

        imageView.setImageBitmap(bitmap);

    } catch (Exception e) {
        Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                .show();
        e.printStackTrace();
        Log.e("Camera", e.toString());

    }

}
这是getOrientation代码:

  public static int getOrientation(Context context, Uri photoUri) {
        Cursor cursor = context.getContentResolver().query(photoUri,
                new String[] { MediaStore.Images.ImageColumns.ORIENTATION },
                null, null, null);

        try {
            if (cursor.moveToFirst()) {
                return cursor.getInt(0);
            } else {
                return -1;
            }
        } finally {
            cursor.close();
        }
    }
这会产生空指针异常,我不明白为什么

有什么帮助吗

编辑:

这就是我所说的意图:

     ImageView imageView = (ImageView) findViewById(R.id.chosenImage2);
     if(imageView.getDrawable() == null){
         Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
         File photo = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis()+ ".jpg");
         intent.putExtra(MediaStore.EXTRA_OUTPUT,
         Uri.fromFile(photo));
         mImageUri = Uri.fromFile(photo);
         startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
     }
}
查询(…)可能返回null,您可以在中找到

很可能是
cursor.moveToFirst()
抛出
NullPointerException
停止执行
try
块,但最后运行
代码:
cursor.close()
它是
null.close()
=>KABAM

您可以检查
光标!=在不同的位置为空。例如,在输入
之前,请尝试
块或最后进入

然而,最安全的方法是捕获NullPointerException

public static int getOrientation(Context context, Uri photoUri) {
    Cursor cursor = context.getContentResolver().query(photoUri,
            new String[] { MediaStore.Images.ImageColumns.ORIENTATION },
            null, null, null);
    //cursor might be null!

    try {
        int returnMe;
        if (cursor.moveToFirst()) {
            returnMe = cursor.getInt(0);
        } else {
            returnMe = -1;
        }
        cursor.close();
        return returnMe;
    } catch(NullPointerException e) {
        //log: no cursor found returnung -1!
        return -1;
    }
}

除此之外,它很可能为null,因为您的URI参数无效。。一个可能的原因是你没有恢复你的活动状态,并且转动相机后Mimageri字段变为空。这会使错误消失,但它不再获得图像方向。还有Paul Jan,我不明白转动相机是如何相关的,我处理了活动结果并制作了活动肖像,只是为了不让它成为原因。对不起,顺便说一句,对于迟到的回复:)在正确处理错误后,是时候问一下为什么会发生错误了-阅读:为什么
光标
为空?Aus Paul Jan指出,很有可能photoUri无效,因此创建游标失败。查看第一个代码块,我看到moethod的调用方式如下
Helpers.getOrientation(this,selectedImage)
,而本质上是
selectedImage=mimageri
。后者是“从天而降”。如果你不能从这里解决这个问题,我建议你提出另一个更具体的问题。