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();


        }

你可能感兴趣的:(工具)