How to check space in android when creating files or saving images?

In android I create txt files and save images. But I'm not sure if this is safe because I don't really check the location before saving. Does anyone have any code here, or know of a tutorial that checks for space?

I only have this when saving images. Can I assume that if there is no room, it will throw an IOException or I need some other code to do this?

thank

private static void imageSaverHelper(File file, Bitmap bitmap) throws IOException {
    file.getParentFile().mkdirs();

    OutputStream fOut = new FileOutputStream(file);
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);

    fOut.flush();
    fOut.close();
}

      

+3


source to share


1 answer


If there is not enough space, you will get an exception. There is no other sane way to deal with this. Since the image is compressed, there is no way to accurately predict how much space it will take without compressing it.

Even though you can get the exact measurement of the space that will be required, there might be something else in the background using that space before you can write your file.



The best way is to write the file, but be prepared to handle exceptions.

+2


source







All Articles