views:

11476

answers:

4

I am working on a function to download an image from a web server, redisplay it on the screen, and if the user wishes to keep the image, save it on the SD card in a certain folder. Is there an easy way to take a bitmap and just save it to the SD card in a folder of my choice?

My issue is that I can download the image, display it on screen as a Bitmap. The only way I have been able to find to save an image to a particular folder is to use FileOutputStream, but that requires a byte array. I am not sure how to convert (if this is even the right way) from Bitmap to byte array, so I can use a Fileoutput stream to write the data.

The other option I have is it use MediaStore :

MediaStore.Images.Media.insertImage(getContentResolver(), bm,
barcodeNumber + ".jpg Card Image", barcodeNumber
+ ".jpg Card Image");

Which works fine to save to SD card, but does not allow you to customize the folder.

Any assistance would be very much appreciated. Thank you in advance.

+3  A: 

Why not call the Bitmap.compress method with 100 (which sounds like it is lossless)?

TofuBeer
+17  A: 
try {
       FileOutputStream out = new FileOutputStream(filename);
       bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
       e.printStackTrace();
}
Ulrich Scheller
+3  A: 

Some formats, like PNG which is lossless, will ignore the quality setting.

Roces
A: 

Here you have a exmaple

        String path = Environment.getExternalStorageDirectory().toString();
        OutputStream fOut = null;
        File file = new File(path, "FitnessGirl"+Contador+".jpg");
            fOut = new FileOutputStream(file);

        getImageBitmap(myurl).compress(Bitmap.CompressFormat.JPEG, 85, fOut);
            fOut.flush();
            fOut.close();

MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
JoaquinG