我正在尝试将我在某些活动中使用的Bitmap保存到我为此目的创建的目录中的外部存储。将图像保存到SD卡(外部存储器,而不是外部存储器)的方法是在不同的类(和不同的包)中,所以我假设我将需要上下文,但我找不到任何给出上下文的地方(试过{ {1}}但它得到的openFileOutput
不能包含路径分隔符。当我运行我的代码时,我在log cat中得到了这个错误:
String fileName
这些是我的权限:
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
并且它不起作用。
顺便说一下,我正在使用模拟器,但我也在几个“Galaxy S”上检查了这个并且发生了相同的错误...(当我尝试在手机或DDMS中浏览文件时,我可以看到它甚至没有创建文件夹......
答案 0 :(得分:2)
从您的问题描述中,您似乎无法创建该文件夹。所以请确保以下内容
目录已创建
if(!file.isExist()) boolean created = file.mkdir();
稍后您可以通过测试布尔值来检查您的文件夹是否已创建。如果没有创建您的文件夹,没有理由进一步使用您的代码。
或者,您可以使用mkdirs()
来创建所有必需的父目录。
答案 1 :(得分:1)
试试这段代码:
File newD = new File(Environment.getExternalStorageDirectory()
+ File.separator +"myTomatoes"+ File.separator + "covers");
if(!newD.exists()){
newD.mkdirs();
}
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
_bitmapScaled.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
//you can create a new file name "test.jpg" in sdcard folder.
File f = new File( newD + File.separator + "test.jpg")
f.createNewFile();
//write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
// remember close de FileOutput
fo.close();
答案 2 :(得分:0)
看看这个:
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED))
{
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath() + "/Pictures");
dir.mkdirs();
File file = new File(dir, filename);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
FileOutputStream f = null;
f = new FileOutputStream(file);
if (f != null) {
f.write(baos.toByteArray());
f.flush();
f.close();
}
}
}
或点击此链接:http://android-er.blogspot.in/2010/07/save-file-to-sd-card.html
答案 3 :(得分:0)
希望这有助于将图像存储在Android应用程序的缓存目录中,并在另一个活动中显示该图像。
<强> Oneactivity.java 强>
将图像存储在缓存目录中
File file = new File(getCacheDir(), "thursday.png");
try {
OutputStream output;
output = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, output);
output.flush();
output.close();
}
catch (Exception e) {
e.printStackTrace();
}
从缓存目录中提取图片。
BitMap bmp5;
try {
File myFile = new File(getCacheDir()+"/thursday.png");
bmp5 = BitmapFactory.decodeFile(myFile.toString());
System.out.println("am setting wallpaper");
myWallpaperManager.setBitmap(bmp5);
} catch (IOException e) {
e.printStackTrace();
}