Save Image to Media Provider on Android


/ Published in: Java
Save to your folder(s)

Save an image to the media library on android


Copy this code and paste it in your HTML
  1. Uri saveMediaEntry(String imagePath,String title,String description,long dateTaken,int orientation,Location loc) {
  2. ContentValues v = new ContentValues();
  3. v.put(Images.Media.TITLE, title);
  4. v.put(Images.Media.DISPLAY_NAME, displayName);
  5. v.put(Images.Media.DESCRIPTION, description);
  6. v.put(Images.Media.DATE_ADDED, dateTaken);
  7. v.put(Images.Media.DATE_TAKEN, dateTaken);
  8. v.put(Images.Media.DATE_MODIFIED, dateTaken) ;
  9. v.put(Images.Media.MIME_TYPE, "image/jpeg");
  10. v.put(Images.Media.ORIENTATION, orientation);
  11.  
  12. File f = new File(imagePath) ;
  13. File parent = f.getParentFile() ;
  14. String path = parent.toString().toLowerCase() ;
  15. String name = parent.getName().toLowerCase() ;
  16. v.put(Images.ImageColumns.BUCKET_ID, path.hashCode());
  17. v.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, name);
  18. v.put(Images.Media.SIZE,f.length()) ;
  19. f = null ;
  20.  
  21. if( targ_loc != null ) {
  22. v.put(Images.Media.LATITUDE, loc.getLatitude());
  23. v.put(Images.Media.LONGITUDE, loc.getLongitude());
  24. }
  25. v.put("_data",imagePath) ;
  26. ContentResolver c = getContentResolver() ;
  27. return c.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, v);
  28. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.