一:将ImageView中的图片转换成Bitmap
二:将Bitmap 转换成二进制,写入本地
三:用广播通知相册进行更新相册
实现代码如下:
//将ImageView中的图片转换成Bitmap
mshowpicture.buildDrawingCache();
Bitmap bitmap=mshowpicture.getDrawingCache();
//将Bitmap 转换成二进制,写入本地
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG , 100 , stream);
byte[] byteArray = stream.toByteArray();
File dir=new File(Environment.getExternalStorageDirectory ().getAbsolutePath()+"/picture" );
if(!dir.isFile()){
dir.mkdir();
}
File file=new File(dir,imagename +".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);
ShowPicture.this .sendBroadcast(intent);
Log.v(LOG_TAG, "------>通知相册更新成功" );
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}