有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从Intent获取图像uri(MediaStore.ACTION\u image\u CAPTURE)

嗨,当我这样做的时候

 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在我的日志中为空。因此,任何人都可以让我知道如何获取图像uri。我不想使用额外的输出并指定自己的路径。提前谢谢


共 (3) 个答案

  1. # 2 楼答案

    在某些设备中有一个与此相关的bug。看看this了解如何解决它

  2. # 3 楼答案

    In some devices, the Uri is null in onActivityForResult(). So you need to set Uri to placing the captured image.

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