如何从 MediaStore.Images.Media.getBitmap(contentResolver, uri) 获取位图?

How to get Bitmap from MediaStore.Images.Media.getBitmap(contentResolver, uri)?

我在从我的 uri 中检索位图时遇到困难。我正在尝试通过 Intent 打开相机后获取从相机拍摄的图像。

                    try {
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                            imageUri = FileProvider.getUriForFile(mContext, mContext.getApplicationContext().getPackageName(), createImageFile());
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);   // set the image file name
                        }
                        startActivityForResult(intent, PERMISSION_REQUEST_CODE_CAMERA);
                    } catch (ActivityNotFoundException e) {
                        e.printStackTrace();
                    }

登录时的imageUri为

content://com.example/external_files/Pictures/CustomFolder/15122017_photo_115344.jpg

相机打开拍照后,调用了我的onActivityResult。因为我使用 MediaStore.EXTRA_OUTPUT 我没有在我的数据对象中收到任何东西,它是空的。我尝试使用 MediaStore.Images.Media.getBitmap() 来检索位图。我传入 imageUri。我认为这可能不正确,因为我收到了一个例外。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PERMISSION_REQUEST_CODE_CAMERA) {
        if (resultCode == Activity.RESULT_OK) {

            Bitmap bitmap;
            try {
                bitmap = MediaStore.Images.Media.getBitmap(mContext.getContentResolver(), imageUri);
                ivProfilePhoto.setImageBitmap(bitmap);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        } else if (resultCode == Activity.RESULT_CANCELED){

        }
    }

我收到的异常是

java.io.FileNotFoundException: No such file or directory

编辑:

我可以挂在文件上,但这似乎也不起作用。这是我试过的。我更新了我的意图如下

                    try {
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                            mFile = createImageFile();
                            imageUri = FileProvider.getUriForFile(mContext, mContext.getApplicationContext().getPackageName(), mFile);
                            intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);   // set the image file name
                        }
                        startActivityForResult(intent, PERMISSION_REQUEST_CODE_CAMERA);
                    } catch (ActivityNotFoundException e) {
                        e.printStackTrace();
                    }

然后在 onActivityResult 中,我执行以下操作

            Bitmap bitmap = BitmapFactory.decodeFile(mFile.getPath());
            ivProfilePhoto.setImageBitmap(bitmap);

没有为图像设置任何内容。另外,仅供参考,这就是我创建目录和设置文件图像名称的方式。

private File createImageFile() {
    File imgFileDir = getDir();
    if (!imgFileDir.exists() && !imgFileDir.mkdirs()) {
        Logger.e(TAG, "Directory does not exist");
        imgFileDir.mkdirs();
    }
    Logger.e("TEST", "imgFileDir= " + imgFileDir);
    // Locale.US to get local formatting
    SimpleDateFormat timeFormat = new SimpleDateFormat("hhmmss", Locale.US);
    SimpleDateFormat dateFormat = new SimpleDateFormat("ddMMyyyy", Locale.US);
    String time = timeFormat.format(new Date());
    String date = dateFormat.format(new Date());
    String photoFile = date + "_photo_" + time + ".jpg";
    filename = imgFileDir.getPath() + File.separator + photoFile;

    Logger.i(TAG, "time(hhmmss): " + time + " date(ddmmyyyy): " + date + "\nfilename: "
            + filename + " photoFile: " + photoFile);
    return new File(filename);
}

private File getDir() {
    File sdDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    return new File(sdDir, "CustomFolder");
}

理想情况下,切换到使用图像加载库(例如 Glide、Picasso),以便您的 I/O 和位图处理可以在后台线程上执行。

除此之外,按住 File 对象,而不是 Uri,并使用 File(例如,BitmapFactory.decodeFile())加载图像。这会更快,因为没有 ContentProvider 开销。

根据我的理解,你想从位图中获取 uri,所以这段代码工作得非常好:-

 public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

这对我有用

val bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);

添加这个

public void onClick(View v) {   
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, 1);
}

当你用上面的代码替换你的代码时,你的这个

public void onActivityResult(int requestCode, int resultCode,
@Nullable Intent data){}

方法将开始工作

//No Need to write this code in onclick method
    Intent intent=new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT)
    startActivityForResult(intent,1);
    Toast.makeText(getContext(), "image"+intent, Toast.LENGTH_SHORT).show();