如何将我的imageView图像保存到图库中(Android开发)

问题描述:

我想创建一个onClick事件,通过点击按钮将图像视图保存到手机图库中,下面是我的代码。它不保存到画廊,任何人都可以帮我弄清楚为什么?如何将我的imageView图像保存到图库中(Android开发)

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

     } 
    }); 
+1

你有没有给你的清单文件的写入权限。 – Raghunandan 2013-03-13 05:01:24

您必须将图像保存到媒体提供商。这里有一个简单的例子:

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

我这样做是为了在图库中保存图片。

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

insertImage()将返回String != null如果图像已经真的救了。 另请注意:需要将清单中的权限设置为“android.permission.WRITE_EXTERNAL_STORAGE” 并注意,这会将图像置于图库中图像列表的底部。

希望这会有所帮助。

+1

任何你可以让它显示在图像列表顶部的方法? – 2014-12-29 19:27:24

+1

是的,我使用samkirton提供的技巧。 [见这里](https://gist.github.com/samkirton/0242ba81d7ca00b475b9)。它工作得很好 – 2015-01-08 16:13:42

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

检查了这一点:http://developer.android.com/training/camera/photobasics.html#TaskGallery

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

您基本上已经将代码复制到了另一个人的答案中,但是没有包含任何关于您的答案可能起作用的解释。 – 2014-08-25 17:27:47

假设的ImageView已保留您要保存,第一,得到位图图像;

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

然后保存它;

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

而且,不要忘记清单中的设置权限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />