0

I would like to ask for some help. Currently I am facing the problem on how to save the image which is shown in the imageview.

so I have a picture in the imageview and a save button at the bottom. So how do I actually save this image to my mobile devices.

Clarification of some doubts.

I have a jpg file in the imageview. so I want to save that image file by clicking on a button which I put it in right below the imageview. So now I am facing a problem whereby I am unable to save the file.

Thank You

James Yeo
  • 116
  • 1
  • 3
  • 12

1 Answers1

1
   1.  For getting image from ImageView

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

   2.  For saving that 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) {
   }
Akash Jindal
  • 505
  • 3
  • 7
  • I have tried your code but it hit the exception. does this requires any external SD card? or it can be store in the inbuild space within the phone – James Yeo Aug 26 '14 at 10:24
  • didn't u changed the code a little but ok so u do one thing for now File root = new File(Environment.getExternalStorageDirectory() + File.separator + "folder_name" + File.separator); now as u doesn't have folder_name folder in your phone so have your line upto this File root = new File(Environment.getExternalStorageDirectory(); – Akash Jindal Aug 26 '14 at 10:43
  • as the line u will now skip will define a location for saving the photo while Enviornment.getExternalStorgaeDirectory() gives the path of default location in phone – Akash Jindal Aug 26 '14 at 10:50