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

}

相關文章
相關標籤/搜索