我使用的是自定义视图,并在我使用的画布中,用户可以画任何东西之后,我想保存在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();
}
这是错误的详细信息
HERE IS THE ERROR DETAILS
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.".....
晚了一些答案,但仍...
A bit late answer, but still...
不知道这是行238,但可能的原因是在这里:
Not sure which is line 238, but probably the reason is here:
字符串imgSaved = MediaStore.Images.Media.insertImage( getContentResolver(),drawView.getDrawingCache(), 。UUID.randomUUID()的toString()+PNG,绘图);
String imgSaved = MediaStore.Images.Media.insertImage( getContentResolver(), drawView.getDrawingCache(), UUID.randomUUID().toString()+".png", "drawing");
由于该方法是在一个点击监听器 getContentResolver
可返回从一个应用程序不同的解析器。无论是保存私人参考内容解析,或者你可以尝试更换 getContentResolver
与 MainActivity.this.getContentResolver()
Since the method is within a click listener getContentResolver
may be returning a Resolver different from the one for the application. Either save a private reference to the content resolver, or you could try replacing getContentResolver
with MainActivity.this.getContentResolver()
:
字符串imgSaved = MediaStore.Images.Media.insertImage( MainActivity.this.getContentResolver(), drawView.getDrawingCache(), UUID.randomUUID()的toString()+png格式, 绘图);
String imgSaved = MediaStore.Images.Media.insertImage( MainActivity.this.getContentResolver(), drawView.getDrawingCache(), UUID.randomUUID().toString() + ".png", "drawing");
或者,它可能是一个问题的权限管理。确保在清单中有:
Alternatively, it might be a permissioning problem. Make sure in the manifest you have:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
最后一个音符, insertImage
的结果是一个URI, imgSaved
不是最好的名字该变量:)
One final note, the result of insertImage
is an URI, imgSaved
is not the best name for that variable :)