我已将ScrollView转换为Bitmap图像并将其保存到外部存储中。一个Toast会弹出,或者说图像已经成功保存,或者它没有。
Bitmap bmp = screenShot();
Bitmap b = bmp.copy(Bitmap.Config.ARGB_8888, true);
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
String fileName = "story.png";
// create a File object for the parent directory
File wallpaperDirectory = new File(Environment.getExternalStorageDirectory().getPath() + "/Stories/");
// have the object build the directory structure, if needed.
wallpaperDirectory.mkdirs();
// create a File object for the output file
File outputFile = new File(wallpaperDirectory, fileName);
//now attach OutputStream to the file object, instead of a String representation
FileOutputStream fos = null;
try
{
fos = new FileOutputStream(outputFile);
b.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
MediaStore.Images.Media.insertImage(this.getContentResolver(), b, "Screen", "screen");
CharSequence text = "Story saved in file Stories";
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
CharSequence text = "Could not save story";
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
CharSequence text = "Could not save story";
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
Toast弹出,表示PNG图像已成功保存。但是,当我在Gallery中查看它时,我会收到以下错误。
07-08 02:22:02.597: E/MediaStore(6833): Failed to insert image
07-08 02:22:02.597: E/MediaStore(6833): java.io.FileNotFoundException: No such file or directory
07-08 02:22:02.597: E/MediaStore(6833): at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:146)
07-08 02:22:02.597: E/MediaStore(6833): at android.content.ContentProviderProxy.openAssetFile(ContentProviderNative.java:611)
07-08 02:22:02.597: E/MediaStore(6833): at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:922)
07-08 02:22:02.597: E/MediaStore(6833): at android.content.ContentResolver.openOutputStream(ContentResolver.java:669)
07-08 02:22:02.597: E/MediaStore(6833): at android.content.ContentResolver.openOutputStream(ContentResolver.java:645)
07-08 02:22:02.597: E/MediaStore(6833): at android.provider.MediaStore$Images$Media.insertImage(MediaStore.java:902)
07-08 02:22:02.597: E/MediaStore(6833): at com.example.letswrite.EndScreen.saveToDevice(EndScreen.java:128)
07-08 02:22:02.597: E/MediaStore(6833): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 02:22:02.597: E/MediaStore(6833): at java.lang.reflect.Method.invoke(Method.java:515)
07-08 02:22:02.597: E/MediaStore(6833): at android.view.View$1.onClick(View.java:3818)
07-08 02:22:02.597: E/MediaStore(6833): at android.view.View.performClick(View.java:4438)
07-08 02:22:02.597: E/MediaStore(6833): at android.view.View$PerformClick.run(View.java:18422)
07-08 02:22:02.597: E/MediaStore(6833): at android.os.Handler.handleCallback(Handler.java:733)
07-08 02:22:02.597: E/MediaStore(6833): at android.os.Handler.dispatchMessage(Handler.java:95)
07-08 02:22:02.597: E/MediaStore(6833): at android.os.Looper.loop(Looper.java:136)
07-08 02:22:02.597: E/MediaStore(6833): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-08 02:22:02.597: E/MediaStore(6833): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 02:22:02.597: E/MediaStore(6833): at java.lang.reflect.Method.invoke(Method.java:515)
07-08 02:22:02.597: E/MediaStore(6833): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-08 02:22:02.597: E/MediaStore(6833): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-08 02:22:02.597: E/MediaStore(6833): at dalvik.system.NativeStart.main(Native Method)
我能做些什么吗?我是否可能因为使用Android模拟器而收到这些错误?