尝试保存文件,但即使在尝试创建目录后,仍然“获取文件的父目录”

时间:2010-11-19 01:51:25

标签: android

我正在尝试将图像保存到我的SD卡,但遇到以下错误:

java.io.IOException: Parent directory of file does not exist: /sdcard/skdyImages/a46e2e08-9154-4fe7-96e8-2af0a7a92867.jpg

我的清单中有权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

这是我的代码:

String newName = UUID.randomUUID().toString();
            Bitmap bmp = ImageLoader.getInstance().getBitmap(e.getUrl());
            File file = new File("/sdcard/skdyImages", newName + ".jpg");
            file.getParentFile().mkdirs();
            file.createNewFile();
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
            bmp.compress(CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

尝试这样的事情......

// Automatically creates folder on sdcard called /Android/data/<package>/files
// if it doesn't exist
File ImageDir = new File(getExternalFilesDir(null).getAbsolutePath());

BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(ImageDir + "/" + newName + ".jpg"));