我正在尝试将我在某些活动中拥有的位图保存到为此目的创建的目录中的外部存储。将图像保存到 sdcard(外部存储器,而不是外部 sd)的方法在不同的类(和不同的包)中,所以我认为我需要上下文,但我找不到任何地方提供上下文(试过openFileOutput
但它String fileName
不能包含路径分隔符)。当我运行我的代码时,我在 log cat 中收到此错误:
04-11 22:13:14.899: E/error(13833): /mnt/sdcard/myTomatoes/covers/378194.PNG: open failed: ENOENT (No such file or directory)
04-11 22:13:14.899: W/System.err(13833): java.io.FileNotFoundException: /mnt/sdcard/myTomatoes/covers/378194.PNG: open failed: ENOENT (No such file or directory)
04-11 22:13:14.899: W/System.err(13833): at libcore.io.IoBridge.open(IoBridge.java:416)
04-11 22:13:14.899: W/System.err(13833): at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
04-11 22:13:14.899: W/System.err(13833): at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
04-11 22:13:14.899: W/System.err(13833): at BL.ImageMethods.SaveImageToMemory(ImageMethods.java:60)
04-11 22:13:14.899: W/System.err(13833): at com.example.mytomatoes.MovieDetailsActivity$4.onClick(MovieDetailsActivity.java:337)
04-11 22:13:14.899: W/System.err(13833): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
04-11 22:13:14.899: W/System.err(13833): at android.os.Handler.dispatchMessage(Handler.java:99)
04-11 22:13:14.899: W/System.err(13833): at android.os.Looper.loop(Looper.java:137)
04-11 22:13:14.899: W/System.err(13833): at android.app.ActivityThread.main(ActivityThread.java:5039)
04-11 22:13:14.899: W/System.err(13833): at java.lang.reflect.Method.invokeNative(Native Method)
04-11 22:13:14.899: W/System.err(13833): at java.lang.reflect.Method.invoke(Method.java:511)
04-11 22:13:14.899: W/System.err(13833): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-11 22:13:14.899: W/System.err(13833): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-11 22:13:14.899: W/System.err(13833): at dalvik.system.NativeStart.main(Native Method)
04-11 22:13:14.899: W/System.err(13833): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
04-11 22:13:14.899: W/System.err(13833): at libcore.io.Posix.open(Native Method)
04-11 22:13:14.899: W/System.err(13833): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
04-11 22:13:14.899: W/System.err(13833): at libcore.io.IoBridge.open(IoBridge.java:400)
这些是我的权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
这是我保存文件的方法:
public static void SaveImageToMemory(Context context, Bitmap img, int rottenId) {
Log.i("SAVE IMAGE", "start save");
File sd = Environment.getExternalStorageDirectory();
File location = new File(sd.getAbsolutePath()+ "/myTomatoes/covers");
location.mkdir();
File dest = new File(location, rottenId + ".PNG");
try {
Log.i("SAVE IMAGE", "trying to save: " + dest.getPath());
FileOutputStream fos = new FileOutputStream(dest);
img.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
} catch (IOException e) {
Log.e("error", e.getMessage());
e.printStackTrace();
}
}
创建File
对象的所有混乱行都是我尝试的结果......原始代码是:
File location = new File(Environment.getExternalStorageDirectory() + "/myTomatoes/covers");
File dest = new File(location, rottenId + ".PNG");
我还尝试先创建ByteArrayOutputStream
位图并将其压缩到它,然后再做fos.write(mByteArrayOpStream.toByteArray())
,但它没有用。
顺便说一句,我正在使用模拟器,但我也在几个“Galaxy S”上检查了这个,并且发生了同样的错误......(当我尝试在手机或 DDMS 中浏览文件时,我可以看到它没有甚至创建文件夹...