androidu
androidu

Reputation: 4718

Android - saving an image to card throws an error

I'm trying to save an image to card and I get the error "< file path > (IS A DIRECTORY)" altough the file's absolute path is correct and the file is an image and not a directory. What am I doing wrong here? I need to mention that I create all the necessary directories before saving the image to disk and I have all the permissions.

file.getAbsolutePath() //returns something like this:

/mnt/sdcard/app_name/folder/image.jpg

.. I construct the picture file like this: File img = new File(dir, image.jpg);

public static void saveImg(File pic, Bitmap picture) {
    try {
        FileOutputStream out = new FileOutputStream(pic);
        picture.compress(Bitmap.CompressFormat.JPEG, 100, out);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Upvotes: 0

Views: 46

Answers (1)

Erol
Erol

Reputation: 6526

First step is to check your sd card to see if you really have a directory with that name (in case you are calling mkdirs() on the image file before creating the stream by any chance).

Then, you can try using this code to create your stream:

String fileName = "image.jpg";
File path = Environment.getExternalStorageDirectory();
File file = new File(path, fileName);
path.mkdirs();
OutputStream os = new FileOutputStream(file);

Upvotes: 2

Related Questions