0

我有从相机活动中获得的位图图像。有人可以指导我如何将这张图片存储在图库中吗?

代码:

在我的按钮 OnClickListener

    Intent campic=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(campic,cameradata ); 

在我的 onActivityResult

        if(resultCode==RESULT_OK)
        {
           Bundle bun=data.getExtras();
           bmp=(Bitmap)bun.get("data");
           SaveIamge(bmp);
           iveventpic.setImageBitmap(bmp);
        }
4

2 回答 2

0
MediaStore.Images.Media.insertImage(getContentResolver(), bmp, title, desc);

正如在这篇文章中看到的那样。

于 2013-04-25T01:47:28.450 回答
0

调用此函数将位图保存在 sdcard 中:

private void SaveIamge(Bitmap finalBitmap) {

String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");    
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete (); 
try {
       FileOutputStream out = new FileOutputStream(file);
       finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
       out.flush();
       out.close();

} catch (Exception e) {
       e.printStackTrace();
}
}

通过调用此行,您必须将该图像存储在图库中:

sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
        Uri.parse("file://" + Environment.getExternalStorageDirectory())));

并在清单中添加权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
于 2013-04-25T04:51:35.373 回答