【问题标题】:Selected Image content the correct path but cursor is null选定的图像内容正确的路径,但光标为空
【发布时间】:2015-05-30 06:45:18
【问题描述】:

我正在尝试显示相机刚刚拍摄的照片并将其显示在 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

这个值是预期值吗?请你告诉我。

所以,光标为空而行

cursor.moveToFirst();

吐出错误空指针:-/。我正在使用 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();
        }
    }

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

【问题讨论】:

  • 你想用上面的代码实现什么???
  • 我可以发布我的代码,我会在其中做同样的事情
  • 我试过这个:用 android.provider.MediaStore.Images.Media.DATA 改变 MediaStore.Images.Media 没有成功: -(
  • getMediaFileUri 是直接取自样本还是其他东西?
  • 这里定义了getoutputmediafileuri()方法:developer.android.com/guide/topics/media/…

标签: android android-camera android-cursor


【解决方案1】:

您需要将fileUri 作为额外的,如此处所示Android Developers

fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

并尝试将您的 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);
}

【讨论】:

  • 是的,我试过了,正如您在上面注释的代码中看到的那样。执行您的建议会使 onActivityResult() 部分中的 data=null :-/ 。我晚点回来,时间不早了。
  • onActivityResult,不使用ContentResolver从uri获取路径,可以直接使用new File(fileUri).getAbsolutePath()
  • 拍照后,我的相机应用程序显示两个按钮,取消和确定。似乎当我按 OK 时,相机应用程序被杀死而没有将意图图像发送到 onActivityResult() 所以,数据为空。 :-/ 该死。我会尝试这种方法stackoverflow.com/questions/12952859/…
  • 那是我在回答中指向您的确切页面。该指南非常易于遵循,并展示了实现您想要的目标的多种方法。
  • 我认为我的相机应用程序无法访问 mnt/sdcard/external_sd/Pictures,这就是 onActivityResult 方法中数据为空的原因。拍照时,我的相机将照片保存在 /mnt/sdcard/DCIM/Camera/ 中。第一个路径是android开发者页面建议的,我该怎么办?
猜你喜欢
  • 2017-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-07
  • 2015-11-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多