2013-03-13 66 views
3

我想創建一個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

回答

1

您必須將圖像保存到媒體提供商。這裏有一個簡單的例子:

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

我這樣做是爲了在圖庫中保存圖片。

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

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

您基本上已經將代碼複製到了另一個人的答案中,但是沒有包含任何關於您的答案可能起作用的解釋。 – 2014-08-25 17:27:47

1

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

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

然後保存它;

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

而且,不要忘記清單中的設置權限。

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