Android 中将 "base64图片" 解码为".png"图片
16lz
2021-01-23
android 中将 "base64图片" 解码为".png"图片
参考:
Convert base64 image data to an image file(.png) and save it to Local file system
方法一
String base64Img = "";// 若包含"data:image/jpeg;base64,"前缀,只取后边的部分if (base64Img.contains(",")) { String[] base64ImgArray = base64Img.split(","); if (base64ImgArray.length == 2) { base64Img = base64ImgArray[1]; }}// 解码try { byte[] decodedString = Base64.decode(base64Img, Base64.DEFAULT); Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); if (mBitmapCreateListener != null) { mBitmapCreateListener.onBitmapCreate(decodedByte); }} catch (Exception e) { e.printStackTrace(); // 回调生成图片失败 if (mBitmapCreateListener != null) { mBitmapCreateListener.onBitmapCreate(null); }}
方法二
FileOutputStream fos = null;try { if (base64ImageData != null) { fos = context.openFileOutput("imageName.png", Context.MODE_PRIVATE); byte[] decodedString = android.util.Base64.decode(base64ImageData, android.util.Base64.DEFAULT); fos.write(decodedString); fos.flush(); fos.close(); }} catch (Exception e) {} finally { if (fos != null) { fos = null; }}
更多相关文章
- Android切换主线程更新UI方法总结
- Android保存图片到系统相册
- android Log.isLoggable方法的使用
- Android 软件盘弹出时把顶部局顶上去的解决方法
- Android WebView选择图片、发送图片
- android的singleInstance启动模式及方法调用
- Android 技巧 - listview 里面的图片不能响应onClick事件?
- Android 中数据库查询方法 query() 中的 selectionArgs 的用法
- RadioButton和CheckBox自定义按钮图片的样式