Android 图片先gzip压缩然后在Base64转成字符串

图片用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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
,需要用Java的Base64类将Base64字符串解码成byte数组,然后将byte数组通过GZIP进行压缩,最后再把压缩后的byte数组通过Base64类编码成压缩后的Base64字符串。以下是示例代码: ```java import java.util.zip.Deflater; import java.util.zip.Inflater; import java.util.Base64; public class Base64Compression { public static String compress(String base64String) { byte[] decodedBytes = Base64.getDecoder().decode(base64String); byte[] compressedBytes = compressBytes(decodedBytes); return Base64.getEncoder().encodeToString(compressedBytes); } public static String decompress(String compressedBase64String) { byte[] compressedBytes = Base64.getDecoder().decode(compressedBase64String); byte[] decompressedBytes = decompressBytes(compressedBytes); return Base64.getEncoder().encodeToString(decompressedBytes); } private static byte[] compressBytes(byte[] data) { Deflater deflater = new Deflater(); deflater.setInput(data); deflater.finish(); byte[] buffer = new byte[data.length]; int compressedSize = deflater.deflate(buffer); byte[] compressedData = new byte[compressedSize]; System.arraycopy(buffer, 0, compressedData, 0, compressedSize); return compressedData; } private static byte[] decompressBytes(byte[] data) { Inflater inflater = new Inflater(); inflater.setInput(data); byte[] buffer = new byte[data.length * 2]; int decompressedSize; try { decompressedSize = inflater.inflate(buffer); } catch (Exception e) { throw new RuntimeException("Failed to decompress data", e); } byte[] decompressedData = new byte[decompressedSize]; System.arraycopy(buffer, 0, decompressedData, 0, decompressedSize); return decompressedData; } } ``` 可以使用以下代码测试: ```java String base64String = "SGVsbG8gV29ybGQh"; String compressedBase64String = Base64Compression.compress(base64String); System.out.println(compressedBase64String); // 输出: "eJwrSS0uyczPAQAAP//I1g==" String decompressedBase64String = Base64Compression.decompress(compressedBase64String); System.out.println(decompressedBase64String); // 输出: "SGVsbG8gV29ybGQh" ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值