我目前正在尝试为我的Android应用程序保存内部内存中的文件,但我无法正确执行此操作!
这是我存储位图的方法:
private void storeBitmapInMemory(String id) throws IOException {
URL newurl = new URL(PictureUrl);
Bitmap bm = BitmapFactory.decodeStream(newurl.openConnection().getInputStream());
// The openfileOutput() method creates a file on the phone/internal storage in the context of your application
final FileOutputStream fos = context.openFileOutput(PREFIX + id +".jpeg", Context.MODE_PRIVATE);
// Use the compress method on the BitMap object to write image to the OutputStream
bm.compress(CompressFormat.JPEG, 100, fos);
}
当我执行此方法时,一切看起来都很好,但实际上没有什么可以保存...当我尝试用这种方法从内存中读取文件时:
private Bitmap getStoredBitmapFromMemory(int idAvatar) {
File cacheDir = context.getCacheDir();
File f = new File(cacheDir, PREFIX + Integer.toString(idAvatar) + ".jpeg");
FileInputStream fis = null;
try {
fis = new FileInputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return BitmapFactory.decodeStream(fis);
}
它抛出了这个例外:
05-02 10:50:22.487: W/System.err(25805): java.io.FileNotFoundException: /data/data/com.example.xxxx/cache/AVATAR_400021371.jpeg: open failed: ENOENT (No such file or directory)
05-02 10:50:22.497: W/System.err(25805): at libcore.io.IoBridge.open(IoBridge.java:416)
05-02 10:50:22.497: W/System.err(25805): at java.io.FileInputStream.<init>(FileInputStream.java:78)
05-02 10:50:22.497: W/System.err(25805): at com.example.xxxx.Performer.getStoredBitmapFromMemory(Performer.java:140)
我的storeBitmapInMemory有什么问题?