6

我正在尝试创建一个 onClick 事件,通过单击按钮将图像视图保存到手机图库中,下面是我的代码。它没有保存到图库中,谁能帮我弄清楚为什么?

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

        }
    });
4

5 回答 5

7

我这样做是为了将图像保存在画廊中。

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”并请注意,这会将图像放在画廊中已经存在的图像列表的底部。

希望这可以帮助。

于 2013-06-28T09:16:04.470 回答
3

假设ImageView已经保留了要保存的图像,首先,获取位图

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

然后用下面的代码保存它: -

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

并且不要忘记在您的清单中设置此权限:-

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2017-04-20T07:42:07.377 回答
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);
}
于 2014-08-25T07:42:07.367 回答
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);
}
于 2013-03-13T05:05:50.173 回答
0
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

于 2013-07-25T20:13:10.207 回答