How do I save my imageView image into Gallery (Android Development)

前端 未结 5 2106
逝去的感伤
逝去的感伤 2021-01-05 17:01

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

5条回答
  •  情书的邮戳
    2021-01-05 17:29

    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);
    }
    

提交回复
热议问题