Question

I've googled a lot of information about my issue but I really don't get what i'm doing wrong, I just want to save an image to sdcard in specific folder, but folder.mkdir() always return false and I get an exception

  java.io.FileNotFoundException: /mnt/sdcard/sakhcomcache/tv/1.gif: open failed: ENOENT (No such file or directory)

and of course I have a permission in my manifest

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

Here is my code

.....
private final static String CACHE_PATH = "/sakhcomcache/";
public final static String CACHE_FOLDER_TV = "/tv";
.....

public static void saveImageOnSDCard(final Bitmap image, final String cacheFolder, final String name) {
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                File folder = new File(Environment.getExternalStorageDirectory() + CACHE_PATH + cacheFolder);
                if (!folder.exists()) {
                    boolean create_succes = folder.mkdir();

                    //create_succes always false

                    if(create_succes){
                        Log.i("create_succes", "create_succes");
                    }
                }
                File imageFile = new File(folder +"/"+ name.substring(name.lastIndexOf("/")));
                if (!imageFile.exists()) {
                    FileOutputStream out = new FileOutputStream(imageFile);
                    image.compress(Bitmap.CompressFormat.PNG, 100, out);
                    out.flush();
                    out.close();
                    Log.i("save succes", "save succes");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();

}
Was it helpful?

Solution

try this

//creating directory

File root = new File(Environment.getExternalStorageDirectory()
                + File.separator + "myDir" + File.separator);
root.mkdirs();
directoryCreated = new File(root, "filename");

//saving file

FileOutputStream out = new FileOutputStream(directoryCreated);
bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
bm.recycle();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top