【问题标题】:get image uri from Intent(MediaStore.ACTION_IMAGE_CAPTURE)从 Intent(MediaStore.ACTION_IMAGE_CAPTURE) 获取图像 uri
【发布时间】:2015-05-25 06:09:45
【问题描述】:

嗨,当我做这样的事情时

 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);

我使用的位图是这样的。

但是,如果我想要 imageuri 以便从中获取完整尺寸的图像怎么办。我尝试从类似上面的意图中获取图像 uri

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

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

【问题讨论】:

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


【解决方案1】:

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

【讨论】:

  • 谢谢我去看看
【解决方案2】:

在某些设备中存在与该意图有关的错误。查看this 了解如何解决它。

【讨论】:

  • 谢谢我去看看
【解决方案3】:

在某些设备中,onActivityForResult() 中的 Uri 为空。所以你需要 设置 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;
    }

【讨论】:

    猜你喜欢
    • 2017-02-05
    • 1970-01-01
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-10
    • 1970-01-01
    相关资源
    最近更新 更多