KaliMa
KaliMa

Reputation: 2060

Storing pictures in Android apps

It is relatively straightforward to retrieve an image from the phone's camera app using an Intent, where that image is in the form of a Bitmap.

I don't know if this is an appropriate question for SO really but is it common practice to just save the entire bitmap as-is? Or do most people compress / resize it down?

Upvotes: 0

Views: 32

Answers (2)

Rajpal Singh
Rajpal Singh

Reputation: 311

This is a simple method as below:

   private Boolean saveImage(Bitmap bitmap){

    ByteArrayOutputStream bao = null;
    File file = null, image = null;
    Boolean save = false;

    try{

        bao = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bao);

        image = new File(Environment.getExternalStorageDirectory() + "", "/yourSelectedFolder/");

        if (!image.exists()) {
            if (!image.mkdirs()) {
                Toast.makeText(context, "Error: Folder Not Created!\nPlease Try Again.", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(context, "Folder Successfully Created!", Toast.LENGTH_LONG).show();
            }
        }


        file = new File(Environment.getExternalStorageDirectory().toString() + "/yourSelectedFolder/","filename" + ".jpeg");
        save = file.createNewFile();

        FileOutputStream fos = new FileOutputStream(file);
        fos.write(bao.toByteArray());
        fos.close();



        if (save){
            Toast.makeText(context, "Image Successfully Saved", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(context, "Image Not Saved", Toast.LENGTH_LONG).show();
        }

    } catch (IOException e) {
        e.printStackTrace();
        Toast.makeText(context, "Error: "+e, Toast.LENGTH_LONG).show();
    }
    return save;
}

Upvotes: 0

Gabe Sechan
Gabe Sechan

Reputation: 93542

You tend to save it using Bitmap.compress, which will compress it for you. Feel free to use PNG which is a lossless format, so no quality loss will occur when you reinflate it.

Of course if you're using an intent to get it from the camera, its usually saved to the file system already. In which case that file is certainly compressed already.

Upvotes: 1

Related Questions