如何将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) {

                 }
            }
        };

最佳答案

而不是以这种方式寻找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>

关于java - 如何在nexus7上解压缩代码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14357249/

10-10 06:28