Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.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 选定的图像内容路径正确,但光标为空_Android_Android Camera_Android Cursor - Fatal编程技术网

Android 选定的图像内容路径正确,但光标为空

Android 选定的图像内容路径正确,但光标为空,android,android-camera,android-cursor,Android,Android Camera,Android Cursor,我正在尝试显示相机刚刚拍摄的照片,并将其显示在imageView中。这是存在问题的方法: protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 100 && resultCode == RESULT_OK) { selectedImage = fileUri;//data.getData();

我正在尝试显示相机刚刚拍摄的照片,并将其显示在imageView中。这是存在问题的方法:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 100 && resultCode == RESULT_OK) {

        selectedImage = fileUri;//data.getData();

        String[] filePathColumn = {MediaStore.Images.Media.DATA};

        Cursor cursor =  getContentResolver().query(selectedImage,filePathColumn, null, null, null);
        //managedQuery(selectedImage,filePathColumn, null, null, null);//
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        picturePath = cursor.getString(columnIndex);
        cursor.close();

        // The following three lines work perfectly :-) 
        // if I comment the part of the cursor lines above
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        ImageView imageView = (ImageView) findViewById(R.id.Imageprev);
        imageView.setImageBitmap(photo);
    }
}
调试时,我看到一些值,例如

selectedImage : file:///mnt/sdcard/external_sd/Pictures/MyCameraApp/IMG_20150530_032752.jpg
我想这没关系。另一个有趣的价值是

filePathColumn : _data
该值是否为预期值?请你告诉我

因此,游标为null,并且该行

cursor.moveToFirst();
吐出错误null指针:-/。我正在用Android 2.2在真实设备上调试代码。救命啊

版本

这是调用上一个的方法

private void clickpic() {

    // Check Camera
        if (getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {

            // Open default camera
            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            /*
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

            // start the image capture Intent
            startActivityForResult(intent, 100);
            */

            fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create

            //the getOutputMediaFileUri is implemented as saving media file suggests by developer.android.com
            //intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, fileUri); // set the image file

            // start the image capture Intent
            startActivityForResult(intent,100);

        } else {
            Toast.makeText(MainActivity.this, "Camera not supported", Toast.LENGTH_LONG).show();
        }
    }

注释的代码是我失败的尝试。

您需要将
fileUri
作为一个额外的文件,如下所示

并尝试将您的
onActivityResult
替换为:

if (requestCode == 100 && resultCode == RESULT_OK) {

    selectedImage = fileUri;

    Bitmap photo = BitmapFactory.decodeFile(new File(selectedImage));
    ImageView imageView = (ImageView) findViewById(R.id.Imageprev);
    imageView.setImageBitmap(photo);
}

你想用上面的代码实现什么???我可以发布我正在做的代码吗?我已经尝试过了:通过android.provider.MediaStore.Images.Media.DATA更改MediaStore.Images.Media.DATA,但没有成功:-(getMediaFileUri直接取自示例或其他内容吗?getoutputmediafileuri()方法是在这里定义的:是的,正如您在上面的注释代码中看到的那样,我试过了。执行您的建议会使onActivityResult()部分中的data=null:-/。我稍后返回,这里已经晚了。onActivityResult,而不是使用
ContentResolver
从uri获取路径,您可以直接使用
新文件(fileUri)。getAbsolutePath()
拍照后,我的相机应用程序会显示两个按钮,取消和确定。似乎当我按下确定时,相机应用程序会被关闭,而不会将目标图像发送到onActivityResult()所以,数据是空的。:/dam。我会尝试这种方法,这正是我在回答中给你指出的页面。该指南非常容易理解,并显示了多种实现你想要的方法。我认为我的相机应用程序无法访问mnt/sdcard/external_sd/Pictures,这就是为什么onActivityResult方法中的数据是空的。拍照时,我的camera将照片保存在/mnt/sdcard/DCIM/Camera/中。第一条路径由android开发者页面建议,我能做什么?
if (requestCode == 100 && resultCode == RESULT_OK) {

    selectedImage = fileUri;

    Bitmap photo = BitmapFactory.decodeFile(new File(selectedImage));
    ImageView imageView = (ImageView) findViewById(R.id.Imageprev);
    imageView.setImageBitmap(photo);
}