0

如何将android编程从asset解压到/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) {  

                 }
            }
        };
4

1 回答 1

1

而不是以这种方式查找 sdcard

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

使用环境 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>
于 2013-01-16T11:47:56.203 回答