Soheyl
Soheyl

Reputation: 549

How to save an image from ImageView?

have imagview want to save it to memory here is my code :

View content = findViewById(R.id.full_image_view);
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File root = Environment.getExternalStorageDirectory();
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");
try {
    root.createNewFile();
    FileOutputStream ostream = new FileOutputStream(root);
    bitmap.compress(CompressFormat.JPEG, 100, ostream);
    ostream.close();
} catch (Exception e) {
    e.printStackTrace();
}


}

after saving nothing happens and no image is exist ?

Upvotes: 0

Views: 6022

Answers (5)

Vipul
Vipul

Reputation: 28093

How about using following snippet ?

Drawable d = imageView.getBackground();
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();

File file = new File(Environment.getExternalStorageDirectory(), "fileName.ext");
outStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();

Upvotes: 0

Hariharan
Hariharan

Reputation: 24853

Try this..

Change root.createNewFile(); to cachePath.createNewFile();

File root = Environment.getExternalStorageDirectory();
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");
try {
    cachePath.createNewFile();
    FileOutputStream ostream = new FileOutputStream(cachePath);
    bitmap.compress(CompressFormat.JPEG, 100, ostream);
    ostream.close();
} catch (Exception e) {
    e.printStackTrace();
}

EDIT:

FileOutputStream ostream = new FileOutputStream(cachePath);

Upvotes: 2

Jitesh Upadhyay
Jitesh Upadhyay

Reputation: 5260

To get bitmap from imageview:

  imageview.buildDrawingCache();
    Bitmap bm=imageview.getDrawingCache();

To save it in a file:

 OutputStream fOut = null;
    Uri outputFileUri;
     try {
    File root = new File(Environment.getExternalStorageDirectory()
      + File.separator + "folder_name" + File.separator);
    root.mkdirs();
   File sdImageMainDirectory = new File(root, "myPicName.jpg");
    outputFileUri = Uri.fromFile(sdImageMainDirectory);
    fOut = new FileOutputStream(sdImageMainDirectory);
   } catch (Exception e) {
    Toast.makeText(this, "Error occured. Please try again later.",
      Toast.LENGTH_SHORT).show();
   }

   try {
    bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
    fOut.flush();
    fOut.close();
   } catch (Exception e) {
   }

add in manifest:

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

Upvotes: 0

dipali
dipali

Reputation: 11188

1 - you need an appropriate permission in amnifest:

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

2- out.flush() check the out is not null..

3 -

String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + 
                            "/yourforlderName";
    File dir = new File(file_path);
if(!dir.exists())
    dir.mkdirs();
    File file = new File(dir, "yourforlderName" + image + ".png");
    FileOutputStream fOut = new FileOutputStream(file);

    bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);
    fOut.flush();
    fOut.close();

if you don't create directory in your sdcard then how to store images in sdcard of specific location? so please check this...i hope its useful to you.

Upvotes: 0

chiru
chiru

Reputation: 645

Use this function to save in SD card:

private void SaveIamge(Bitmap finalBitmap) {

    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/saved_images");    
    myDir.mkdirs();
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fname = "Image-"+ n +".jpg";
    File file = new File (myDir, fname);
    if (file.exists ()) file.delete (); 
    try {
           FileOutputStream out = new FileOutputStream(file);
           finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
           out.flush();
           out.close();

    } catch (Exception e) {
           e.printStackTrace();
    }
}

and add in manifest:

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

Upvotes: 1

Related Questions