开发者

Trying to save a file but getting "Parent directory of file does not exist" even after trying to create dir

开发者 https://www.devze.com 2023-01-25 16:42 出处:网络
I\'m trying to save an image to my sdcard but running into the following error: java.io.IOException: Parent directory of file does not exist: /sdcard/skdyImages/a46e2e08-9154-4fe7-96e8-2af0a7a92867.j

I'm trying to save an image to my sdcard but running into the following error:

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

I do have the p开发者_JAVA技巧ermissions in my manifest

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

Here is my code:

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();

Any ideas?


Try something like this...

// 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"));
0

精彩评论

暂无评论...
验证码 换一张
取 消