从Intent获取图像uri(MediaStore.ACTION_IMAGE_CAPTURE)

时间:2015-05-25 06:09:46

标签: java android android-intent android-camera-intent start-activity

我喜欢这样的事情

 Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }

在onActivityResult中,我从相机应用程序的数据意图中获取图像的缩略图

Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(imageBitmap);

我使用的是这样的位图。

但是,如果我想要图像库,以便我可以从中获取完整尺寸的图像。我尝试从上面的意图获取图像uri

            Uri uri = data.getData();
            if (uri != null) {
                Log.d(TAG, uri.toString());
            }else{
                Log.d(TAG,"uri is null");
            }

这样做我知道uri在我的logcat中是空的。所以任何人都可以让我知道如何获取图像uri.I不想使用EXTRA_OUTPUT并指定我自己的路径。提前谢谢

3 个答案:

答案 0 :(得分:0)

有一个well documented bug,它出现在低分辨率设备中。请检查this thread以获取解决方法。

答案 1 :(得分:0)

在某些设备中存在该意图的错误。请查看this以了解如何解决此问题。

答案 2 :(得分:0)

  

在某些设备中,onActivityForResult()中的Uri为null。所以你需要   将Uri设置为放置捕获的图像。

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // If there any applications that can handle this intent then call the intent.
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        Uri fileUri = Uri.fromFile(getOutputMediaFile());
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        startActivityForResult(takePictureIntent, CAMERA_PICKER);
    }

public File getOutputMediaFile() {
        // To be safe, you should check that the SDCard is mounted
        // using Environment.getExternalStorageState() before doing this.

        File mediaStorageDir;
        // If the external directory is writable then then return the External pictures directory.
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyApp");
        } else {
            mediaStorageDir = Environment.getDownloadCacheDirectory();
        }

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }

        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
        File mediaFile;
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");

        return mediaFile;
    }