6

I am trying to create an onClick event to save an imageview into the phone Gallery by the click of a Button, below is my code. it does not save into the Gallery, can anyone help me figure out why?

    sharebtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View b) {
            // TODO Auto-generated method stub
            //attempt to save the image

            b = findViewById(R.id.imageView);
            b.setDrawingCacheEnabled(true);
                Bitmap bitmap = b.getDrawingCache();
                //File file = new File("/DCIM/Camera/image.jpg");
                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();
                }

        }
    });
ShenJie Lin
  • 61
  • 1
  • 1
  • 2

5 Answers5

7

I do this to save Image in gallery.

private void saveImageToGallery(){
    imageview.setDrawingCacheEnabled(true);
    Bitmap b = imageview.getDrawingCache();
    Images.Media.insertImage(getActivity().getContentResolver(), b,title, description);
}

insertImage() will return a String != null if image has been really saved. Also: Needs permission in the manifest as "android.permission.WRITE_EXTERNAL_STORAGE" And note that this puts the image at the bottom of the list of images already in the gallery.

Hope this helps.

jkincali
  • 995
  • 1
  • 9
  • 10
Sergio Carvalho
  • 251
  • 3
  • 9
3

Suppose the ImageView already keeps the image that you want to save, first, get the Bitmap

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

Then save it with below code:-

MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle , yourDescription);

And do not forget to set this permission in your manifest:-

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Prags
  • 2,457
  • 2
  • 21
  • 38
cathy.s
  • 41
  • 3
  • do i need to put extension in title ? – Ashish Nov 24 '18 at 13:02
  • This is by far the best solution I've been able to find, but it looks like it needs to require permission in realtime or it doesn't work. For those seeking a similar method, I'd suggest adding [the code available here](https://stackoverflow.com/a/39957656/6398434) before implementing this solution. – JorgeAmVF Dec 14 '18 at 00:37
1

You have to save the image to media provider. Here is a simple example:

Uri saveMediaEntry(String imagePath,String title,String description,long dateTaken,int orientation,Location loc) {
ContentValues v = new ContentValues();
v.put(Images.Media.TITLE, title);
v.put(Images.Media.DISPLAY_NAME, displayName);
v.put(Images.Media.DESCRIPTION, description);
v.put(Images.Media.DATE_ADDED, dateTaken);
v.put(Images.Media.DATE_TAKEN, dateTaken);
v.put(Images.Media.DATE_MODIFIED, dateTaken) ;
v.put(Images.Media.MIME_TYPE, “image/jpeg”);
v.put(Images.Media.ORIENTATION, orientation);
File f = new File(imagePath) ;
File parent = f.getParentFile() ;
String path = parent.toString().toLowerCase() ;
String name = parent.getName().toLowerCase() ;
v.put(Images.ImageColumns.BUCKET_ID, path.hashCode());
v.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, name);
v.put(Images.Media.SIZE,f.length()) ;
f = null ;
if( targ_loc != null ) {
v.put(Images.Media.LATITUDE, loc.getLatitude());
v.put(Images.Media.LONGITUDE, loc.getLongitude());
}
v.put(“_data”,imagePath) ;
ContentResolver c = getContentResolver() ;
return c.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, v);
}
Justin Vartanian
  • 1,149
  • 7
  • 18
1
public static void addImageToGallery(final String filePath, final Context context) {

    ContentValues values = new ContentValues();

    values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis());
    values.put(Images.Media.MIME_TYPE, "image/jpeg");
    values.put(MediaStore.MediaColumns.DATA, filePath);

    context.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);
}
AbcAeffchen
  • 14,400
  • 15
  • 47
  • 66
vijay
  • 1,475
  • 2
  • 16
  • 26
  • You've basically copied the code in another person's answer, but have not included any explanation at all about why your answer might work. – Andrew Barber Aug 25 '14 at 17:27
0
private void galleryAddPic() {
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f = new File(mCurrentPhotoPath);
    Uri contentUri = Uri.fromFile(f);
    mediaScanIntent.setData(contentUri);
    this.sendBroadcast(mediaScanIntent);
}

Check this out: http://developer.android.com/training/camera/photobasics.html#TaskGallery

hector6872
  • 1,336
  • 14
  • 18