8

I am using an Imageview and a Button in 1 XML, and I am retriving the images as URL from webServer and displaying it on the ImageView. Now if the Button(Save) is clicked I need to save that particular image into SD card. How to do this?

NOTE: Present Image Should be saved.

Yury
  • 20,618
  • 7
  • 58
  • 86
Kalai Selvan.G
  • 482
  • 3
  • 10
  • 22
  • 1
    There are a lot of answers to your question, use search first! http://stackoverflow.com/questions/4875114/android-save-image-from-url-onto-sd-card – Dmytro Danylyk Feb 22 '12 at 14:03

2 Answers2

52

First, you need to get your Bitmap. You can already have it as an object Bitmap, or you can try to get it from the ImageView such as:

    BitmapDrawable drawable = (BitmapDrawable) mImageView1.getDrawable();
    Bitmap bitmap = drawable.getBitmap();

Then you must get to directory (a File object) from SD Card such as:

    File sdCardDirectory = Environment.getExternalStorageDirectory();

Next, create your specific file for image storage:

    File image = new File(sdCardDirectory, "test.png");

After that, you just have to write the Bitmap thanks to its method compress such as:

    boolean success = false;

    // Encode the file as a PNG image.
    FileOutputStream outStream;
    try {

        outStream = new FileOutputStream(image);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
        /* 100 to keep full quality of the image */

        outStream.flush();
        outStream.close();
        success = true;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

Finally, just deal with the boolean result if needed. Such as:

    if (success) {
        Toast.makeText(getApplicationContext(), "Image saved with success",
                Toast.LENGTH_LONG).show();
    } else {
        Toast.makeText(getApplicationContext(),
                "Error during image saving", Toast.LENGTH_LONG).show();
    }

Don't forget to add the following permission in your Manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Romain R.
  • 920
  • 12
  • 21
  • R u Sure,This example saves the present image on buttonclick.I tried but its not getting saved – Kalai Selvan.G Feb 23 '12 at 05:09
  • Rectified.Very Very Clear Example..Works Great,Thanks dude – Kalai Selvan.G Feb 23 '12 at 05:43
  • @ Romain: How to save the images in diferent names.If i clicks to save other image previous image got overrided..Whether we need to calculate the count of images stored there to do so – Kalai Selvan.G Feb 23 '12 at 06:11
  • Yes. For example you can use a static field in your class to increment the number of saved images. After, you can use it to generate the name of file. Or else you can use a timestamp in the name of file. Using [System.currentTimeMillis()](http://developer.android.com/reference/java/lang/System.html#currentTimeMillis%28%29) for example. – Romain R. Feb 27 '12 at 09:24
  • @RomainR. can you tell me how to do this when using camera – Abhishek Jun 22 '16 at 07:13
  • According to the following tutorial: https://developer.android.com/training/camera/photobasics.html, we can see that the `onActivityResult` provides us a `Bitmap` as an extra. – Romain R. Jun 22 '16 at 07:35
  • Helpful for me..So,I upvote for u.:) – komal akhani Aug 25 '17 at 07:18
5

Probable Solution is

Android - Saving a downloaded image from URL onto SD card

Bitmap bitMapImg;
void saveImage() {
        File filename;
        try {
            String path = Environment.getExternalStorageDirectory().toString();

            new File(path + "/folder/subfolder").mkdirs();
            filename = new File(path + "/folder/subfolder/image.jpg");

            FileOutputStream out = new FileOutputStream(filename);

            bitMapImg.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();

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

            Toast.makeText(getApplicationContext(), "File is Saved in  " + filename, 1000).show();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
Community
  • 1
  • 1
prayagupa
  • 30,204
  • 14
  • 155
  • 192