MediaStore.Images.Media.insertImage在尝试保存图像时返回null [英] MediaStore.Images.Media.insertImage is returning null when trying to save the image

查看:2280
本文介绍了MediaStore.Images.Media.insertImage在尝试保存图像时返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用自定义视图,在我使用一个画布,用户可以在其中绘制任何东西,之后,我想保存该图像在SD卡bt无法做到这一点。不知道发生了什么。

I am using an custom view and in that i am using an canvas in which a user can draw anything and after that i want to save that image in sd card bt was not able to do that. Don't know what is going on.

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)

我总是在尝试保存时收到此消息图片....糟糕!无法保存图片。

I am always getting this message while trying to save image...."Oops! Image could not be saved.".....

推荐答案

我在模拟器这是由于 Android错误,当用户没有使用时解决方法是手动初始化照片目录:

I had this issue in the Emulator (Android 4.4) and turns out it's due to an Android bug, where it happens when the user hasn't taken a photo on the device before (i.e. gallery is empty and hasn't been initialized.). The workaround is to initialize the photo directory manually:

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

不确定这是否在以后的Android版本中已修复。

Not sure if this is fixed in later versions of Android.

这篇关于MediaStore.Images.Media.insertImage在尝试保存图像时返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆