网络图片保存,拉取网络图片后展示,保存到本地外部存储(前提是图片已展示),lin_qrcode是图片容器,或父容器,父父容器
Bitmap bitmap = BitmapUtil.getCacheBitmapFromView(lin_qrcode); if (bitmap != null) { boolean result = SimpleUtils.saveBitmapToLocation(bitmap); bitmap.recycle(); if (result) FunCom.showToast(getString(R.string.save_success)); }
内部存储图片,保存到本地外部存储(内部存储图片,即包名文件夹内的图片,无法被外部访问,需要保存到外部,在相册查看或其他应用获取),localUri为图片Uri
try { Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), localUri); if (bitmap != null) { boolean result = SimpleUtils.saveBitmapToLocation(bitmap); bitmap.recycle(); if (result) FunCom.showToast(context.getString(R.string.save_success)); } } catch (IOException e) { e.printStackTrace(); }
所用工具类
public static boolean saveBitmapToLocation(Bitmap bm) { try { File basePath = FileHelper.getBasePath(); Calendar now = new GregorianCalendar(); SimpleDateFormat simpleDate = new SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault()); String fileName = simpleDate.format(now.getTime()) + ".jpg"; File file = new File(basePath, fileName); FileOutputStream fos = new FileOutputStream(file); bm.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); Uri uri = Uri.fromFile(file); RedBeanApplication.getContext().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri)); } catch (FileNotFoundException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } return true; }