Android:如何将图像保存到缓存目录?
问题描述:
我看到很多类似的问题trought stackoverflow,但没有人帮助我。我的代码是这样的:Android:如何将图像保存到缓存目录?
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
//create name based on time? just a simple link to photo
//save to cache file
File file = getExternalCacheDir();
if (file != null) {
try {
String root = file.getAbsolutePath();
File imageFile = new File(file, "m_images.jpeg");
boolean tr = imageFile.mkdirs();
FileOutputStream stream = new FileOutputStream(imageFile);
boolean complete = image.compress(Bitmap.CompressFormat.PNG, 90, stream);
if (!complete) {
Log.d("tag", "image doesn't saved");
}
Log.d("tag", "image saved");
} catch (IOException e) {
Log.d("tag", "Can't save image", e);
}
但是当我打开FileOoutputStream我总是得到这样的例外:
java.io.FileNotFoundException: /mnt/sdcard/Android/data/com.whatever/cache/m_images.jpeg (Is a directory)
你能说这是哪里的问题的原因是什么?为什么文件不会创建?
答
例外/mnt/sdcard/Android/data/com.whatever/cache/m_images.jpeg (Is a directory)
表示它是一个目录。这将是执行好当删除线boolean tr = imageFile.mkdirs();
谢谢,它有助于 – 2012-07-22 16:42:44