Search code examples
androidimageviewsave-image

Android, save images (in ImageView) to mobile devices


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


Solution

  •    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) {
       }