解压缩android中的文件时出现FileNotFoundException

时间:2015-08-21 06:54:19

标签: java android zip fileoutputstream

我收到错误FileNotFoundException打开失败:在Android中解压缩文件时EROFS(只读文件系统)。我已经失去了时间但没有得到解决方案。请帮我解决我的问题。

public class UnZipActivity extends Activity {

private String zipFile = Environment.getExternalStorageDirectory() + "/database/db.zip";
private String unzipLocation = Environment.getExternalStorageDirectory() + "/database/";
Button btnSelectPhoto;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_getimage);

    Log.d("Unzip", "Zipfile: " + zipFile);
    Log.d("Unzip", "location: " + unzipLocation);

    btnSelectPhoto = (Button)findViewById(R.id.btnSelectPhoto);

    btnSelectPhoto.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            unzip();
        }
    });
}

public void unzip() {


        FileInputStream fin = null;
        try {
            fin = new FileInputStream(new File(zipFile));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ZipInputStream zin = new ZipInputStream(fin);

        BufferedInputStream in = new BufferedInputStream(zin);

        ZipEntry ze = null;
        try {
            while ((ze = zin.getNextEntry()) != null) {
                Log.v("Unzip", "Unzipping " + ze.getName());

                if (ze.isDirectory()) {
                    _dirChecker(ze.getName());
                } else {
                    zin.closeEntry();
                    FileOutputStream fout = new FileOutputStream(new File(unzipLocation + ze.getName()));

                    BufferedOutputStream out = new BufferedOutputStream(fout);

                    /*for (int c = zin.read(); c != -1; c = zin.read()) {
                        fout.write(c);
                    }*/

                    byte b[] = new byte[1024];
                    int n;
                    while ((n = in.read(b,0,1024)) >= 0) {
                        out.write(b,0,n);
                    }

                    zin.closeEntry();
                    fout.close();
                }

            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            zin.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


}

private void _dirChecker(String dir) {
    File f = new File(unzipLocation + dir);

    if (!f.isDirectory()) {
        f.mkdirs();
    }
}
}

错误

    08-21 02:40:46.934: W/System.err(8495): java.io.FileNotFoundException: /storage/sdcard/database/objectdetail_create.sql: open failed: EROFS (Read-only file system)
08-21 02:40:46.934: W/System.err(8495):     at libcore.io.IoBridge.open(IoBridge.java:409)
08-21 02:40:46.944: W/System.err(8495):     at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
08-21 02:40:46.944: W/System.err(8495):     at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
08-21 02:40:46.954: W/System.err(8495):     at com.example.buttonstyleapp.UnZipActivity.unzip(UnZipActivity.java:71)
08-21 02:40:46.954: W/System.err(8495):     at com.example.buttonstyleapp.UnZipActivity$1.onClick(UnZipActivity.java:43)
08-21 02:40:46.954: W/System.err(8495):     at android.view.View.performClick(View.java:4240)
08-21 02:40:46.954: W/System.err(8495):     at android.view.View$PerformClick.run(View.java:17721)
08-21 02:40:46.954: W/System.err(8495):     at android.os.Handler.handleCallback(Handler.java:730)
08-21 02:40:46.964: W/System.err(8495):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-21 02:40:46.964: W/System.err(8495):     at android.os.Looper.loop(Looper.java:137)
08-21 02:40:46.974: W/System.err(8495):     at android.app.ActivityThread.main(ActivityThread.java:5103)
08-21 02:40:46.974: W/System.err(8495):     at java.lang.reflect.Method.invokeNative(Native Method)
08-21 02:40:46.974: W/System.err(8495):     at java.lang.reflect.Method.invoke(Method.java:525)
08-21 02:40:46.974: W/System.err(8495):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-21 02:40:46.984: W/System.err(8495):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-21 02:40:46.984: W/System.err(8495):     at dalvik.system.NativeStart.main(Native Method)
08-21 02:40:46.984: W/System.err(8495): Caused by: libcore.io.ErrnoException: open failed: EROFS (Read-only file system)
08-21 02:40:46.994: W/System.err(8495):     at libcore.io.Posix.open(Native Method)
08-21 02:40:47.004: W/System.err(8495):     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
08-21 02:40:47.014: W/System.err(8495):     at libcore.io.IoBridge.open(IoBridge.java:393)
08-21 02:40:47.014: W/System.err(8495):     ... 15 more

我在AndroidManifest.xml中添加了权限

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

0 个答案:

没有答案