将ImageVIew中的图片保存到本地相册中

一:将ImageView中的图片转换成Bitmapui

二:将Bitmap 转换成二进制,写入本地this

三:用广播通知相册进行更新相册spa


实现代码以下:orm

//将ImageView中的图片转换成Bitmap图片

mshowpicture.buildDrawingCache();get

Bitmap bitmap=mshowpicture.getDrawingCache();it

//将Bitmap 转换成二进制,写入本地io

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);ast

    fos.flush();stream

 //用广播通知相册进行更新相册

    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();

}