我拍照并在imageview中显示。然后我在我的活动中从imageview获取位图,当我按下按钮时我想将这个位图保存到手机的图库中。我该怎么办?
答案 0 :(得分:7)
在Button onClick
中调用此函数private void saveImage() {
File myDir=new File("/sdcard/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();
}
}
检查此save bitmap
答案 1 :(得分:1)
试试这个
Bitmap toDisk = Bitmap.createBitmap(w1,h1,Bitmap.Config.ARGB_8888);
setBitmap(toDisk);
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ban_background);
Bitmap resizeImage1=Bitmap.createScaledBitmap(myBitmap,590,350,false);
try {
toDisk.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("/mnt/sdcard/image".jpg")));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
答案 2 :(得分:0)
首先必须获取手机外部存储目录的路径,然后是存储图库图像的目录。在大多数Android机型上这是/ mnt / sdcard / pictures但我不建议硬编码这条路径而是使用
Environment.getExternalStorageDirectory();
之后只需创建一个指向该目录的文件路径,并使用outputStream将位图写入该目录。