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

时间:2016-05-23 17:49:54

标签: android image uri mediastore

  

注意:类似的问题通过下面的SO链接询问,但没有   建议的解决方案适合我,因此开始这个线程。

  1. MediaStore.Images.Media.insertImage is returning null when trying to save the image
  2. Why Images.Media.insertImage return null
  3. 问题陈述: 我正在从网址下载图像,然后尝试共享相同的图像。为了达到这个目的,我需要Uri,我通过在MediaStore.Images.Media中保存图像来获得它。

    代码:

    一个。从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();
            }
        }
    

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

    ℃。获取图像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, "SomeImage", null);
            storePathInSharedPreferences(path);
            Uri uri = Uri.parse(path);
    
            downloadedImageUri = uri;
        }
    
        void fixMediaDir() {
            File sdcard = Environment.getExternalStorageDirectory();
            if (sdcard != null) {
                File mediaDir = new File(sdcard, "DCIM/Camera");
                if (!mediaDir.exists()) {
                    mediaDir.mkdirs();
                }
            }
        }
    

    d。清单文件使用权限

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    

    问题:从下面行返回的值为空

    String path = MediaStore.Images.Media.insertImage(this.getContentResolver(), inImage, "SomeImage", null);
    

    有什么想法吗?

    以下是Stackbacktrace的一部分:

    Failed to insert image java.io.FileNotFoundException: No such file or directory       
    at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:146)
    at android.content.ContentProviderProxy.openAssetFile(ContentProviderNative.java:620)
    at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:939)
    at android.content.ContentResolver.openOutputStream(ContentResolver.java:686)
    at android.content.ContentResolver.openOutputStream(ContentResolver.java:662)
    at android.provider.MediaStore$Images$Media.insertImage(MediaStore.java:934)
    at myorg.myapp.ImageDescription.updateImageUri(ImageDescription.java:165)
    

1 个答案:

答案 0 :(得分:0)

更改此

<uses-permission android:name="android.permission.read_external_storage" />

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>