需求:保存h5頁面圖片至相冊
分析:
1.獲取h5返回的base64碼。
2.將base64碼轉換為Bitmap圖片。
3.新建文件,將圖片存儲至指定路徑。
4.通知相冊更新。
1.圖片-base64解碼工具類如下。
注意:base64轉成Bitmap時,需要去除所有"/r/n"、"data:image/png;base64,"字符,否則會造成解碼失敗,Bitmap為空的情況。
public class TypeConverter { //圖片轉化成base64字符串 public static String imageEncode(String path) { //將圖片文件轉化為字節數組字符串,並對其進行Base64編碼處理 InputStream in = null; byte[] data = null; //讀取圖片字節數組 try { in = new FileInputStream(new File(path)); data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace(); } //對字節數組Base64編碼 return Base64.encodeToString(data, Base64.DEFAULT);//返回Base64編碼過的字節數組字符串 } //base64字符串轉化成圖片 public static String imageDecode(Context context, String imgStr,String imagePath,String imageName) { imgStr = imgStr.replaceAll("/r/ndata:image/png;base64,",""); //對字節數組字符串進行Base64解碼並生成圖片 if (imgStr == null) //圖像數據為空 return ""; //Base64解碼 byte[] b = Base64.decode(imgStr,Base64.DEFAULT); for(int i=0;i<b.length;++i) { if(b[i]<0) {//調整異常數據 b[i]+=256; } } //生成jpeg圖片 // String imgFilePath = context.getExternalCacheDir().getPath()+"/hema_card.jpg";//新生成的圖片 String imgFilePath = imagePath+"/"+imageName;//新生成的圖片 LogUtils.e("生成的圖片地址為:"+imgFilePath); OutputStream out = null; try { out = new FileOutputStream(imgFilePath); out.write(b); out.flush(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // 其次把文件插入到系統圖庫 try { MediaStore.Images.Media.insertImage(context.getContentResolver(), imagePath, imageName, null); } catch (FileNotFoundException e) { e.printStackTrace(); } // 最后通知圖庫更新 context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+imgFilePath))); return imgFilePath; } /** * bitmap轉為base64 * @param bitmap * @return */ public static String bitmapToBase64(Bitmap bitmap) { String result = null; ByteArrayOutputStream baos = null; try { if (bitmap != null) { baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); baos.flush(); baos.close(); byte[] bitmapBytes = baos.toByteArray(); result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (baos != null) { baos.flush(); baos.close(); } } catch (IOException e) { e.printStackTrace(); } } return result; } /** * base64轉為bitmap * @param base64Data * @return */ public static Bitmap base64ToBitmap(String base64Data) { base64Data = base64Data.replace("data:image/png;base64,",""); byte[] bytes = Base64.decode(base64Data, Base64.DEFAULT); return BitmapFactory.decodeByteArray(bytes, 0, bytes.length); } }
2.存儲文件至指定路徑。
2.1.添加存儲權限。
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2.2.保存圖片,並通知相冊刷新。
public static void saveImageToGallery(Context context, Bitmap bitmap, String fileName,String toast){ // 首先保存圖片 File file = new File(Environment.getExternalStorageDirectory().getPath()+"/"+fileName);//log: storage/emulated/0/hema2.jpg try { if(!file.exists()){ file.createNewFile(); } FileOutputStream fos = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); fos.flush(); fos.close(); if(!TextUtils.isEmpty(toast)){ ToastUtils.showToast(toast); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // 其次把文件插入到系統圖庫 try { MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null); } catch (FileNotFoundException e) { e.printStackTrace(); } // 最后通知圖庫更新 context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+file.getPath()))); }
參考博客:
1.https://blog.csdn.net/chunlongyuan/article/details/7696070
2.https://blog.csdn.net/wolfking0608/article/details/79289799#commentBox