February 20, 2010

[SOLVED] Android: Save Image to Media Provider on Android




  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. File f = new File(imagePath) ;


  12. File parent = f.getParentFile() ;


  13. String path = parent.toString().toLowerCase() ;


  14. String name = parent.getName().toLowerCase() ;


  15. v.put(Images.ImageColumns.BUCKET_ID, path.hashCode());


  16. v.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, name);


  17. v.put(Images.Media.SIZE,f.length()) ;


  18. f = null ;




  19. if( targ_loc != null ) {


  20. v.put(Images.Media.LATITUDE, loc.getLatitude());


  21. v.put(Images.Media.LONGITUDE, loc.getLongitude());


  22. }


  23. v.put("_data",imagePath) ;


  24. ContentResolver c = getContentResolver() ;


  25. return c.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, v);


  26. }



Save Image to Media Provider on Android - Java - Snipplr.

3 comments:

  1. displayName is used on line 4, but isn't in scope, at least within this code fragment.

    ReplyDelete
  2. very nice... thank you very much. it went straight to my head. :)

    ReplyDelete
  3. where to add this code ?

    ReplyDelete