2

i am trying to coding an apllication to get an image from gallery then do some effects oon it, and after showing the image, i want to save it as JPG in SdCard. can any on plz thell me how to save image on sdcard, and where should i put save code? this is where i want to save, after clicking on button2:

public void onClick(View arg0) {
                    Bitmap yourimage;
                    yourimage=toGrayscale(yourSelectedImage);
                    ImageButton effected=(ImageButton)findViewById(R.id.imageButton2);
                    int width = 150;
                    int height =150; 
                    Bitmap resizedbitmap=Bitmap.createScaledBitmap(yourimage, width, height, true);
                    effected.setImageBitmap(resizedbitmap);
}

i mean after set image as resizedbitmap i want to save it in sdcard

Reza_Rg
  • 3,345
  • 7
  • 32
  • 48
  • Possible duplicate, or at least includes several possible answers: http://stackoverflow.com/questions/649154/android-bitmap-save-to-location?rq=1 – mehmetminanc Sep 08 '12 at 23:20

3 Answers3

7

this is basically it

Bitmap bmp = createBitmap();
OutputStream stream = new FileOutputStream("/sdcard/test.jpg");
bmp.compress(CompressFormat.JPEG, 100, stream);
WIllJBD
  • 6,144
  • 3
  • 34
  • 44
4
**This Code Cover the Following Topics**

1. Save a bitmap Image on sdcard a jpeg
2. Create a folder on sdcard
3. Create every file Separate name
4. Every file save with date and time
5. Resize the image in very small size
6. Best thing image Quality fine not effected from Resizing


The following method is used to create an image file using the bitmap

public void createImageFromBitmap(Bitmap bmp) {

    FileOutputStream fileOutputStream = null;
    try {

        // create a File object for the parent directory
        File wallpaperDirectory = new File("/sdcard/Capture/");
        // have the object build the directory structure, if needed.
        wallpaperDirectory.mkdirs();

        //Capture is folder name and file name with date and time
        fileOutputStream = new FileOutputStream(String.format(
                "/sdcard/Capture/%d.jpg",
                System.currentTimeMillis()));

        // Here we Resize the Image ...
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.JPEG, 100,
                byteArrayOutputStream); // bm is the bitmap object
        byte[] bsResized = byteArrayOutputStream.toByteArray();


        fileOutputStream.write(bsResized);
        fileOutputStream.close();


    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
    }
}


   and add this in manifest

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
Rashid Ali
  • 561
  • 6
  • 14
2

If You have an image in bitmpa formate, for example if you take a screen shot of your apps and now you want to store that bitmap image object as an image file in sdcard in "jpg" format, then use this few line of code, hope you will get your answer.

//----------- Store the image file in sdcard with image file name..-------

  OutputStream fOut = null;
 File file = new File("sdcard/pir_fahim_shah.jpg");
     try
     {
        fOut = new FileOutputStream(file);
         b.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
         try 
         {
            fOut.flush();
             fOut.close();
          }
         catch (IOException e) 
        {   e.printStackTrace();  }
        } catch (FileNotFoundException e) 
          {     e.printStackTrace();    }



 try {
  MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
} 
 catch (FileNotFoundException e) 
{     e.printStackTrace();   }
Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81