如何在nexus7上解压缩代码

时间:2013-01-16 11:23:29

标签: java android

如何将编程android从资源解压缩到/ sdcard /,我的代码在nexus7下面没有成功,但是如果运行除了nexus7就好了,就能提取数据了。

运行解压缩。

new Thread(new Runnable() {
    @Override
    public void run() {
        UnZip.start();
    }
}).start();


 private Thread UnZip = new Thread() {
            @Override
        public void run() {
            try {
                final int BUFFER = 8192;

                ZipInputStream inputStream = new ZipInputStream(getAssets().open("file.zip"));

                for (ZipEntry entry = inputStream.getNextEntry(); entry != null; entry = inputStream
                        .getNextEntry()) {

                    String innerFileName = "/sdcard/" + File.separator
                            + entry.getName();
                    File innerFile = new File(innerFileName);

                    if (entry.isDirectory()) {
                        innerFile.mkdirs();
                    } else {
                        FileOutputStream outputStream = new FileOutputStream(
                                innerFileName);

                        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(
                                outputStream, BUFFER);

                        int count = 0;
                        byte[] data = new byte[BUFFER];
                        while ((count = inputStream.read(data, 0, BUFFER)) != -1) {
                            bufferedOutputStream.write(data, 0, count);
                        }

                        bufferedOutputStream.flush();
                        bufferedOutputStream.close();
                    }
                }
                inputStream.closeEntry();
                inputStream.close();

                     } catch (Exception e) {  

                 }
            }
        };

1 个答案:

答案 0 :(得分:1)

而不是以这种方式寻找SD卡

 String innerFileName = "/sdcard/" + File.separator
                            + entry.getName();

使用Environment API

  String innerFileName = Environment.getExternalStorageDirectory().toString() + File.separator + entry.getName();

不要忘记将WRITE_EXTERNAL_STORAGE权限添加到AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>