我拍了一张照片并在ImageView中展示它。然后,我从活动中获取ImageView中的位图,当我按下按钮时,我想将此位图保存到手机相册中。我该怎么办?
我拍了一张照片并在ImageView中展示它。然后,我从活动中获取ImageView中的位图,当我按下按钮时,我想将此位图保存到手机相册中。我该怎么办?
在按钮的 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();
}
}
查看这个保存位图
试试这个
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();
}
Environment.getExternalStorageDirectory();
接下来只需创建一个文件路径到该目录,并使用 outputStream 将位图写入该目录。