0

I have Written the code for Editing the Image now I want to save that edited image in the sdcard

image=(ImageView)findViewById(R.id.image);
        Intent intent = getIntent();
        File sdCardDirectory = Environment.getExternalStorageDirectory();
        photo = (Bitmap) intent.getParcelableExtra("photoo");
        image.setImageBitmap(photo);
user1758835
  • 215
  • 4
  • 17

3 Answers3

5

Try this,

 void saveImage() {

    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/saved_images");

    String fname = "Image.jpg";
    File file = new File (myDir, fname);
    if (file.exists ()) file.delete (); 
    try {
           FileOutputStream out = new FileOutputStream(file);
           myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
           out.flush();
           out.close();

    } catch (Exception e) {
           e.printStackTrace();
    }
}
Lucifer
  • 29,392
  • 25
  • 90
  • 143
vinothp
  • 9,939
  • 19
  • 61
  • 103
  • my dear please ,your answer code save only one image but i have image gallery in my app , how can i save any no. of images i need to save to sd card , thanks – androidqq6 Apr 14 '13 at 23:19
  • @androidqq6 if you want to save many number of images then loop it. Eg If you have 15 images then use `for loop for(int i=0;i<=15;i++){//Code for image save}` to save with dynamic image name. – vinothp Apr 15 '13 at 08:08
  • thanks for reply my friend i already start bounty question regard this subject , i used for loop method to save images from one of my app activity which is infinite gallery activity but i faced two problem , by press save for certain image : 1- its save the same image 15 times (the same image ) depend on your code: for loop for(int i=0;i<=15;i++)and if you press to save another image it save nothing , please can you take alook on my bounty post :http://stackoverflow.com/questions/15665830/numbering-of-image-saved-from-app-resource-to-sd-card – androidqq6 Apr 15 '13 at 09:30
  • @androidqq6 answered in your question. Check that – vinothp Apr 15 '13 at 10:34
1

Ok first of all you need to give Write Permissions in AndroidManifest.xml as below,

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Now lets look at the code that actually write your edited image,

// Getting the SDCard Path
File sdcard = Environment.getExternalStorageDirectory();
File editedFile = new File( sdcard, "myphoto.jpeg" );

// if file is already exists then first delete it
if ( editedFile.exists() ) 
{   
    editedFile.delete(); 
}

FileOutputStream fOut = new FileOutputStream( editedFile );
photo.compress( Bitmap.CompressFormat.JPEG, 90, fOut );
Lucifer
  • 29,392
  • 25
  • 90
  • 143
0

It is quite simple.

File outpt = new File( sdCardDirectory, "photo.jpeg" );
FileOutputStream outptOs = new FileOutputStream( outpt );
photo.compress( Bitmap.CompressFormat.JPEG, 90, outptOs );
Lucifer
  • 29,392
  • 25
  • 90
  • 143
Blackbelt
  • 156,034
  • 29
  • 297
  • 305