android bitmap保存本地图片,Android保存View转Bitmap并到本地图库实时更新
參考張大神的http://stormzhang.github.io/android/2014/07/24/android-save-image-to-gallery/
最開始我想的是截屏保存更方便,但很丑,于是查了下資料。不到30分鐘就弄出來了,很順利,還是感謝那些開源分享的大神們
遇到沒做過的,先思考,再動手找解決辦法。哈哈。下面是我項目中的的代碼,可以參考
private void save(final LinearLayout mView){
// 獲取圖片某布局
mView.setDrawingCacheEnabled(true);
mView.buildDrawingCache();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
// 要在運行在子線程中
bmp = mView.getDrawingCache(); // 獲取圖片
savePicture(bmp, "zzp_sale.png");// 保存圖片
mView.destroyDrawingCache(); // 保存過后釋放資源
}
},100);
}
public void savePicture(Bitmap bm, String fileName) {
Log.i("xing", "savePicture: ------------------------");
if (null == bm) {
Log.i("xing", "savePicture: ------------------圖片為空------");
return;
}
//建立指定文件夾
File foder = new File(Environment.getExternalStorageDirectory() , "zzp_sale");
if (!foder.exists()) {
foder.mkdirs();
}
File myCaptureFile = new File(foder, fileName);
try {
if (!myCaptureFile.exists()) {
myCaptureFile.createNewFile();
}
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
//壓縮保存到本地
bm.compress(Bitmap.CompressFormat.JPEG, 90, bos);
bos.flush();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
// 把文件插入到系統圖庫
try {
MediaStore.Images.Media.insertImage(context.getContentResolver(),
myCaptureFile.getAbsolutePath(), fileName, null);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 最后通知圖庫更新
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + myCaptureFile.getPath())));
Toast.makeText(context, "保存成功!", Toast.LENGTH_SHORT).show();
}
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的android bitmap保存本地图片,Android保存View转Bitmap并到本地图库实时更新的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 判断滑动方向,H5触摸事
- 下一篇: android listview ite