[英]How do I save my imageView image into Gallery (Android Development)
我正在嘗試創建一個onClick事件,通過單擊按鈕將imageview保存到手機庫中,下面是我的代碼。 它沒有存入畫廊,任何人都可以幫我找出原因嗎?
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();
}
}
});
我這樣做是為了在圖庫中保存圖像。
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”並注意這會將圖像放在圖庫列表的底部。
希望這可以幫助。
假設ImageView
已經保存了要保存的圖像,首先獲取位圖
imageView.buildDrawingCache();
Bitmap bm=imageView.getDrawingCache();
然后使用以下代碼保存: -
MediaStore.Images.Media.insertImage(getContentResolver(), yourBitmap, yourTitle , yourDescription);
並且不要忘記在清單中設置此權限: -
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
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);
}
您必須將圖像保存到媒體提供商。 這是一個簡單的例子:
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 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.