Save Bitmap in Android as JPEG in External Storage in a folder-ThrowExceptions

Exception or error:

I am using this code to save Bitmap in External Storage but it does not create the folder if it not exists:

String path = Environment.getExternalStorageDirectory().toString();
        OutputStream fOutputStream = null;
        File file = new File(path + "/Captures/", "screen.jpg");
        try {
            fOutputStream = new FileOutputStream(file);

            capturedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOutputStream);

            fOutputStream.flush();
            fOutputStream.close();

            MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
            return;
        } catch (IOException e) {
            e.printStackTrace();
            Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
            return;
        }

How can I save the image in the new directory if not exists and save default if the folder is there in the device?

How to solve:

try this it will gives u result sure:

String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/req_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-" + n + ".jpg";
File file = new File(myDir, fname);
Log.i(TAG, "" + file);
if (file.exists())
    file.delete();
try {
    FileOutputStream out = new FileOutputStream(file);
    bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
    out.flush();
    out.close();
} catch (Exception e) {
    e.printStackTrace();
}

add this one to show in gallery:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));

look at this link for clear answer:
show folder images in gallery

###

Please use the below code snippet might be help full

String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOutputStream = null;
File file = new File(path + "/Captures/", "screen.jpg");
if (!file.exists()) {
    file.mkdirs();
}

try {
    fOutputStream = new FileOutputStream(file);

    capturedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOutputStream);

    fOutputStream.flush();
    fOutputStream.close();

    MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
} catch (FileNotFoundException e) {
    e.printStackTrace();
    Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
    return;
} catch (IOException e) {
    e.printStackTrace();
    Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
    return;
}

###

Use the following:

File dir = new File(path + "/Captures/");
if(!dir.exists()) {
    dir.mkdirs();
}
File file = new File(path + "/Captures/", "screen.jpg");
 ......

###

late but might be helpful to someone. use below code it will save the bitmap in external directory more faster because of BufferOutPutStream.

public boolean storeImage(Bitmap imageData, String filename) {
    // get path to external storage (SD card)

    File sdIconStorageDir = null;

    sdIconStorageDir = new File(Environment.getExternalStorageDirectory()
            .getAbsolutePath() + "/myAppDir/");
    // create storage directories, if they don't exist
    if (!sdIconStorageDir.exist()) {
        sdIconStorageDir.mkdirs();
    }
    try {
        String filePath = sdIconStorageDir.toString() + File.separator + filename;
        FileOutputStream fileOutputStream = new FileOutputStream(filePath);
        BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
        //Toast.makeText(m_cont, "Image Saved at----" + filePath, Toast.LENGTH_LONG).show();
        // choose another format if PNG doesn't suit you
        imageData.compress(Bitmap.CompressFormat.PNG, 100, bos);
        bos.flush();
        bos.close();

    } catch (FileNotFoundException e) {
        Log.w("TAG", "Error saving image file: " + e.getMessage());
        return false;
    } catch (IOException e) {
        Log.w("TAG", "Error saving image file: " + e.getMessage());
        return false;
    }
    return true;
}

###

You should take a look in the documentation of File, you’ll find the method mkdir(). It is pretty much the same as the unix one :
https://developer.android.com/reference/java/io/File.html#mkdir()

Leave a Reply

Your email address will not be published. Required fields are marked *