尝试读取文件时发生IOException

时间:2015-06-30 23:51:49

标签: file android-4.4-kitkat

我和我的朋友正在为我们的学习创建Android应用程序,其工作方式与KeePass相同。

问题是这个应用程序可以在Android 4.0.3的朋友手机上运行,​​而在Android 4.4.2 KitKat的手机上也可以。为什么呢?

我可以创建没有异常的数据库文件,但是当我尝试登录时会抛出IOException。我们认为FileInputStream可能抛出此异常,但我们不知道为什么。

以下是出现异常的代码:

public void onClickLogon(View view){
    _db = null;
    FileInputStream fis = null;
    try {
        fis = openFileInput(this.login.getText().toString() + ".db");
        _db = (PasswordDatabase) com.example.tomus.passwordwalletbeta.Security.decrypt(fis, this.login.getText().toString(), this.password.getText().toString());
        fis.close();
        if(_db==null){
            this.info.setText("Wrong informations.");
        }
        else {
            Intent intent = new Intent(MainActivity.this, LoggedActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("db", _db);
            bundle.putString("login", login.getText().toString());
            bundle.putString("password", password.getText().toString());
            intent.putExtras(bundle);
            this.login.setText("");
            this.password.setText("");
            startActivity(intent);
        }
    } catch (InvalidKeyException | NoSuchPaddingException | NoSuchAlgorithmException e) {
        this.info.setText("Wrong informations.");
    } catch (FileNotFoundException e) {
        this.info.setText("FileNotFound");
    } catch (IOException e) {
        this.info.setText("IOException");
    }
}

@Edit:这是错误日志,现在我可以看到无法找到该文件。在4.4.2中创建文件或路径是否有任何变化?

Process: com.example.tomus.passwordwalletbeta, PID: 5495
java.lang.IllegalStateException: Could not execute method of the activity
        at android.view.View$1.onClick(View.java:3857)
        at android.view.View.performClick(View.java:4487)
        at android.view.View$PerformClick.run(View.java:18746)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:149)
        at android.app.ActivityThread.main(ActivityThread.java:5077)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at android.view.View$1.onClick(View.java:3852)
        at android.view.View.performClick(View.java:4487)
        at android.view.View$PerformClick.run(View.java:18746)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:149)
        at android.app.ActivityThread.main(ActivityThread.java:5077)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
        at dalvik.system.NativeStart.main(Native Method)
Caused by: java.io.StreamCorruptedException
        at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:2070)
        at java.io.ObjectInputStream.<init>(ObjectInputStream.java:371)
        at com.example.tomus.passwordwalletbeta.Security.decrypt(Security.java:82)
        at com.example.tomus.passwordwalletbeta.MainActivity.onClickLogon(MainActivity.java:74)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at android.view.View$1.onClick(View.java:3852)
        at android.view.View.performClick(View.java:4487)
        at android.view.View$PerformClick.run(View.java:18746)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:149)
        at android.app.ActivityThread.main(ActivityThread.java:5077)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
        at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案