尝试保存图像时,MediaStore.Images.Media.insertImage返回null

时间:2014-04-14 12:01:07

标签: android canvas android-sdcard mediastore

我正在使用自定义视图,并且我正在使用画布,用户可以在其中绘制任何内容,之后我想将该图像保存在SD卡中,但是无法做到这一点。不知道发生了什么。

else if(view.getId()==R.id.save_btn){
            //save drawing
            AlertDialog.Builder saveDialog = new AlertDialog.Builder(this);
            saveDialog.setTitle("Save drawing");
            saveDialog.setMessage("Save drawing to device Gallery?");
            saveDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
                private FileOutputStream fOut;

                public void onClick(DialogInterface dialog, int which){
                    //save drawing
                    drawView.setDrawingCacheEnabled(true);

                    //attempt to save
                    String imgSaved = MediaStore.Images.Media.insertImage(
                            getContentResolver(), drawView.getDrawingCache(),
                            UUID.randomUUID().toString()+".png", "drawing");
                    //feedback
                    if(imgSaved!=null){
                        Toast savedToast = Toast.makeText(getApplicationContext(), 
                                "Drawing saved to Gallery!", Toast.LENGTH_SHORT);
                        savedToast.show();
                    }
                    else{
                        Toast unsavedToast = Toast.makeText(getApplicationContext(), 
                                "Oops! Image could not be saved.", Toast.LENGTH_SHORT);
                        unsavedToast.show();
                    }
                    drawView.destroyDrawingCache();
                }
            });
            saveDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog, int which){
                    dialog.cancel();
                }
            });
            saveDialog.show();
        }

这是错误细节

04-14 11:24:28.700: E/MediaStore(6866): Failed to insert image
04-14 11:24:28.700: E/MediaStore(6866): java.io.FileNotFoundException: No such file or directory
04-14 11:24:28.700: E/MediaStore(6866):     at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:146)
04-14 11:24:28.700: E/MediaStore(6866):     at android.content.ContentProviderProxy.openAssetFile(ContentProviderNative.java:577)
04-14 11:24:28.700: E/MediaStore(6866):     at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:673)
04-14 11:24:28.700: E/MediaStore(6866):     at android.content.ContentResolver.openOutputStream(ContentResolver.java:537)
04-14 11:24:28.700: E/MediaStore(6866):     at android.content.ContentResolver.openOutputStream(ContentResolver.java:513)
04-14 11:24:28.700: E/MediaStore(6866):     at android.provider.MediaStore$Images$Media.insertImage(MediaStore.java:891)
04-14 11:24:28.700: E/MediaStore(6866):     at com.example.clent.MainActivity$9.onClick(MainActivity.java:238)
04-14 11:24:28.700: E/MediaStore(6866):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
04-14 11:24:28.700: E/MediaStore(6866):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-14 11:24:28.700: E/MediaStore(6866):     at android.os.Looper.loop(Looper.java:137)
04-14 11:24:28.700: E/MediaStore(6866):     at android.app.ActivityThread.main(ActivityThread.java:5103)
04-14 11:24:28.700: E/MediaStore(6866):     at java.lang.reflect.Method.invokeNative(Native Method)
04-14 11:24:28.700: E/MediaStore(6866):     at java.lang.reflect.Method.invoke(Method.java:525)
04-14 11:24:28.700: E/MediaStore(6866):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-14 11:24:28.700: E/MediaStore(6866):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-14 11:24:28.700: E/MediaStore(6866):     at dalvik.system.NativeStart.main(Native Method)

我在尝试保存图像时总是收到此消息....“哎呀!图像无法保存。”.....

2 个答案:

答案 0 :(得分:26)

我在模拟器(Android 4.4)中遇到了这个问题,并且由于Android bug而导致问题,当用户之前没有在设备上拍照时会发生这种情况(即画廊是空的,尚未初始化。)。解决方法是手动初始化照片目录:

void fixMediaDir() {
    File sdcard = Environment.getExternalStorageDirectory();
    if (sdcard != null) {
        File mediaDir = new File(sdcard, "DCIM/Camera");
        if (!mediaDir.exists()) {
            mediaDir.mkdirs();
        }
    }
}

不确定这是否在Android的更高版本中得到修复。

答案 1 :(得分:8)

有点迟到的回答,但仍然......

不确定第238行是哪一个,但可能原因在于:

  

String imgSaved = MediaStore.Images.Media.insertImage(                               getContentResolver(),drawView.getDrawingCache(),                               UUID.randomUUID()。toString()+“。png”,“drawing”);

由于该方法位于点击监听器getContentResolver内,因此可能会返回与应用程序不同的解析器。保存对内容解析程序的私人引用,或者您可以尝试将getContentResolver替换为MainActivity.this.getContentResolver()

  

String imgSaved = MediaStore.Images.Media.insertImage(                               MainActivity.this.getContentResolver()                               drawView.getDrawingCache()                               UUID.randomUUID()。toString()+“。png”,                               “绘图”);

或者,它可能是一个许可问题。确保你有清单:

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

最后一点,insertImage的结果是URI,imgSaved不是该变量的最佳名称:)