小编Kra*_*tha的帖子

MediaStore.Images.Media.insertImage - java.io.FileNotFoundException - Android

注意:类似的问题在下面的SO链接上被询问,但是所提出的解决方案都没有为我工作,因此开始这个线程.

  1. 尝试保存图像时,MediaStore.Images.Media.insertImage返回null
  2. 为什么Images.Media.insertImage返回null

问题陈述: 我正在从网址下载图像,然后尝试共享相同的图像.为了达到这个目的,我需要Uri,我通过在MediaStore.Images.Media中保存图像来获得它.

码:

A.从AsyncTask类中获取下载的图像

public void processDownloadedImage(Bitmap bitmap) {
        if (bitmap == null) {
            Toast.makeText(this,"Image could not be downloaded and shared!",Toast.LENGTH_LONG).show();
        }
        else {
            downloadedImage = bitmap;
            checkPermissionShowShareSheet();
        }
    }
Run Code Online (Sandbox Code Playgroud)

B.检查Marshmallow及更高级别的许可

public void checkPermissionShowShareSheet() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.System.canWrite(this)){
                requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},2909);
            }
            else {


            updateImageUri(downloadedImage);
            showShareSheet(downloadedImageUri);
        }
    }
    else {
        updateImageUri(downloadedImage);
        showShareSheet(downloadedImageUri);
    }
}
Run Code Online (Sandbox Code Playgroud)

C.获取图像Uri

private void updateImageUri(Bitmap inImage){
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

        fixMediaDir();
        String path = MediaStore.Images.Media.insertImage(this.getContentResolver(), inImage, …
Run Code Online (Sandbox Code Playgroud)

android uri image mediastore

5
推荐指数
1
解决办法
1993
查看次数

标签 统计

android ×1

image ×1

mediastore ×1

uri ×1