有关位图图像和图像存储的问题

时间:2013-04-25 01:37:18

标签: android imageview bitmapimage

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

代码:

在我的按钮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);
        }

2 个答案:

答案 0 :(得分:0)

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

this post.

所示

答案 1 :(得分: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" />