Android 图片先gzip压缩然后在Base64转成字符串
来源:互联网 发布:中国经济数据统计 编辑:程序博客网 时间:2024/05/16 01:10
图片用gzip解压,然后转为Base64
public static String pngToString(File f1, File f2) throws Exception {//第一个File是需要压缩的图片路径,第二个File是图片压缩后生成的一个文件路径 // InputStreamReader in = new InputStreamReader(new FileInputStream(f1), "UTF-8"); FileInputStream in = new FileInputStream(f1); //使用GZIPOutputStream包装OutputStream流,使其具体压缩特性,最后会生成.gz压缩包 // 并且里面有一个文件 BufferedOutputStream out = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(f2))); System.out.println("开始写压缩文件..."); int c; byte[] buf = new byte[1024]; while ((c = in.read(buf, 0, 1024)) != -1) { /* * 注,这里是压缩一个字符文件,前面是以字符流来读的,不能直接存入c,因为c已是Unicode * 码,这样会丢掉信息的(当然本身编码格式就不对),所以这里要以UTF-8来解后再存入。 *///out.write(String.valueOf((char) c).getBytes("UTF-8")); out.write(buf, 0, c); } in.close(); out.close();// System.out.println("开始读压缩文件...");// //使用GZIPInputStream包装InputStream流,使其具有解压特性// BufferedReader in2 = new BufferedReader(new InputStreamReader(// new GZIPInputStream(new FileInputStream(f2)), "UTF-8"));// String s;// //读取压缩文件里的内容// while ((s = in2.readLine()) != null) {// System.out.println(s);// }// in2.close(); //下面转Base64 String base64 = null; InputStream in1 = null; try { in1 = new FileInputStream(f2); byte[] bytes = new byte[in1.available()]; int length = in1.read(bytes); base64 = Base64.encodeToString(bytes, 0, length, Base64.DEFAULT); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); } } return base64; }
onActivityResult获得的图片返回
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { final Uri selectedImage = data.getData(); final String path = ImageUtils.getRealPathFromUri(getActivity(), selectedImage); String[] filePathColumn = {MediaStore.Images.Media.DATA}; Log.e("Person", selectedImage + " -----path" + path); // content://media/external/images/media/789294 Glide.with(this).load(selectedImage).bitmapTransform(new CropCircleTransformation(getActivity())).into(ivPersonPic); new Thread() { @Override public void run() { super.run(); final String gzip = ContastUtils.IMAGE_PATH + "/" + System.currentTimeMillis() + ".gz"; File filep = new File(path); final File fileg = new File(gzip); String base = null; try { // TypeConverter.zip(filep, fileg); base = TypeConverter.pngToString(filep, fileg);//得到base64字符串 final String finalBase = base; getActivity().runOnUiThread(new Runnable() { @Override public void run() { Log.e("--------", gzip); } }); } catch (Exception e) { e.printStackTrace(); } } }.start(); }
阅读全文
0 0
- Android 图片先gzip压缩然后在Base64转成字符串
- UIImage图片转成Base64字符串
- android将图片转成字符串,再将字符串转成图片 (base64处理的字符串)
- 图片转成NSDATA然后BASE64编码POST到服务器
- 图片转成base64的字符串, 如何让图片在html中显示
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- Android图片压缩(质量压缩和尺寸压缩)&Bitmap转成字符串上传
- java中的守护线程
- 安装 Ruby, Rails 运行环境 常见的错误
- 小程序发布上线-微信小程序开发-视频教程17
- keil uVersion4的安装卸载+破解
- Java正则表达式的语法与示例
- Android 图片先gzip压缩然后在Base64转成字符串
- Python中替换元素
- Javaweb学习总结15:JSP基础语法
- ORACLE集群概念和原理
- 关于在线代码评审的几点考量
- Python之创建tuple
- CentOS7安装,dracut-initqueue timeout 等各种问题
- javascript权威指南(6)随笔
- mybatis编写POJO包装类型扩展parameterType字段