如何压缩和解压缩android中png图片 [英] How to zip and unzip png images in android

查看:182
本文介绍了如何压缩和解压缩android中png图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序嗨当我点击拉链扣,我需要压缩的图像文件,当我点击解压缩按钮,我需要解压的文件,我使用尝试低于code来压缩图像,但我的问题是,当我点击拉链按钮压缩文件创建,但此后在使用WinZip软件系统我尝试打开文件,但它不是开放其展示它似乎并没有一个有效的归档有效的文件在那里我做了错误的ü可以让我怎么压缩和解压缩图片

公共类MainActivity延伸活动{
    / **当第一次创建活动调用。 * /

 按钮压缩,解压缩;
  的String [] S =新的String [2];
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    邮编=(按钮)findViewById(R.id.button1);
    zip.setOnClickListener(新OnClickListener(){        公共无效的onClick(查看为arg0){
            // TODO自动生成方法存根
            S [0] =/ SD卡/ saved_images / Main.png
            //s[1]=\"/sdcard/Physics_Lab/Stefans_Law/stefan_law.txt; //第二个文件的路径
            COM preSS C =新的COM preSS(S/ SD卡/ saved_images / stefen.zip);
            c.zip();
        }
    });    }
  }
    公共类的COM $ P $ {PSS
      私有静态最终诠释BUFFER = 80000;      私有String [] _​​FILES;
        私人字符串_zipFile;   公众的COM preSS(字符串[]文件,弦乐的压缩文件){
        _files =文件;
     _zipFile = zip文件;    }   公共无效ZIP(){
    尝试{
     原产地的BufferedInputStream = NULL;
         FileOutputStream中DEST =新的FileOutputStream(_zipFile);        ZipOutputStream出=新ZipOutputStream(新的BufferedOutputStream(DEST));  字节的数据[] =新的字节[缓冲]  的for(int i = 0; I< _files.length;我++){
      Log.d(补充说:,_文件[I]);
    Log.v(的COM preSS,添加+ _FILES [I]);
    科幻的FileInputStream =新的FileInputStream(_FILES [I]);
    起源=新的BufferedInputStream(FI,BUFFER);
    入门的ZipEntry =新的ZipEntry(_FILES [I] .substring(_FILES [I] .lastIndexOf(/)+ 1));
    out.putNextEntry(输入);
    诠释计数;
    而((计数= origin.read(数据,0,缓冲液))!= - 1){
      out.write(数据0,计);
    }
    origin.close();
  }  out.close();
}赶上(例外五){
  e.printStackTrace();
}   }     }


解决方案

您可以检查出这2个教程,压缩和解压缩文件

Android的Zip文件

的Andr​​oid解压文件

Hi in my app when i click zip button i need to zip image file and and when i click unzip button i need to unzip file,i tried using below code to zip image but my problem is when i click zip button zip file is creating,but after that in system using winzip software i try to open file but its not opening its showing "it does not appear to a valid archive valid file" where i did mistake can u let me how to zip and unzip images

public class MainActivity extends Activity { /** Called when the activity is first created. */

Button zip,unzip; 
  String []s=new String[2];   
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);



    zip=(Button)findViewById(R.id.button1);
    zip.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            s[0]="/sdcard/saved_images/Main.png";    
            //s[1]="/sdcard/Physics_Lab/Stefans_Law/stefan_law.txt"; // path of the second file
            Compress c =new Compress(s,"/sdcard/saved_images/stefen.zip");   
            c.zip();    
        }
    });

    }
  }
    public class Compress { 
      private static final int BUFFER = 80000; 

      private String[] _files; 
        private String _zipFile; 

   public Compress(String[] files, String zipFile) { 
        _files = files; 
     _zipFile = zipFile; 

    } 

   public void zip() { 
    try  { 
     BufferedInputStream origin = null; 
         FileOutputStream dest = new FileOutputStream(_zipFile); 

        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest)); 

  byte data[] = new byte[BUFFER]; 

  for(int i=0; i < _files.length; i++) { 
      Log.d("add:",_files[i]);
    Log.v("Compress", "Adding: " + _files[i]); 
    FileInputStream fi = new FileInputStream(_files[i]); 
    origin = new BufferedInputStream(fi, BUFFER); 
    ZipEntry entry = new ZipEntry(_files[i].substring(_files[i].lastIndexOf("/") + 1)); 
    out.putNextEntry(entry); 
    int count; 
    while ((count = origin.read(data, 0, BUFFER)) != -1) { 
      out.write(data, 0, count); 
    } 
    origin.close(); 
  } 

  out.close(); 
} catch(Exception e) { 
  e.printStackTrace(); 
} 

   } 

     } 

解决方案

You can check out these 2 tutorials to zip and unzip the file

Android Zip file

Android Unzip file

这篇关于如何压缩和解压缩android中png图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆