private void saveImage() {
ImageView对象.buildDrawingCache();
Bitmap bitmap = ImageView对象.getDrawingCache();
//将Bitmap转换成二进制,写入本地
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyImage");
if (!dir.exists()) {
dir.mkdir();
}
File file = new File(dir, img_desc.substring(0, 10) + ".png");
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(byteArray, 0, byteArray.length);
fos.flush();
//用广播通知相册进行更新相册
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(file);
intent.setData(uri);
PictureActivity.this.sendBroadcast(intent);
Snackbar.make(mContainer, "图片保存成功~恭喜你收获到新的图片~~", Snackbar.LENGTH_SHORT).show();
} catch (Exception e) {
e.printStackTrace();
}
}
Android app中将图片保存到本地相册并自命名相册名的代码方法
最新推荐文章于 2023-11-28 20:56:08 发布