0

私の Android アプリでは、新しいゲームが読み込まれるたびに内部ストレージからファイルを読み取っています。これを最初の 4 回実行すると正常に動作しますが、5 回目に強制終了します。これが私のコードです

private String readFromInternalStorage(String filename) {
    FileInputStream fis=null;
    byte[] bytes = new byte[1000000];
    try {
        fis=startGame.openFileInput(filename);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }       
    try {
        fis.read(bytes);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return new String(bytes);

}

コードをいじっていると、バイト配列の長さを変更すると、強制的に閉じることなくファイルを読み取ることができる回数が変わることに気付きました。長さを 2000000 に変更すると 2 回目で閉じ、100000 に変更すると 8 回目で閉じます。メソッドが呼び出されるたびに新しいバイト配列を作成しているため、サイズが何も変わらないとは思わないため、なぜこれが起こるのかについてはまったくわかりません。

アップデート:

戻ってさらにテストを行った後、ファイル入力は、アプリが強制的に閉じられる理由とは何の関係もないようです。このコードがコメントアウトされていると、アプリは強制終了せずに 5 つのレベルを続けてロードするので、それが問題だと思いましたが、8 回試行しても強制終了するので、他に何か問題があることは明らかです。とにかく助けてくれてありがとう。

4

3 に答える 3

3

コードに「close()」が表示されません。

于 2012-08-17T20:07:29.943 に答える
1

配列サイズをハードコーディングしないでください。FileInputStreamまた、失敗した場合でも が閉じていることを確認するために、finally を使用する必要があります。

これを行う方法を示すコード サンプルを次に示します。

        FileInputStream fis;
        String info = "";
        try {
            fis = mContext.openFileInput(this.fileName);
            byte[] dataArray = new byte[fis.available()];
            if (dataArray.length > 0) {
                while (fis.read(dataArray) != -1) {
                    info = new String(dataArray);
                }
                Log.i("File Reading" , "Success!");
                isOk = true;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        } finally {
            fis.close();
        }
于 2012-08-17T20:12:10.410 に答える
0

あなたがすることの安全なバージョンは、例えば:

private String readFromInternalStorage(String filename) {
    FileInputStream fis = null;
    File file = new File(startGame.getFilesDir(), filename);

    long size = file.length();
    // impossible to have more than that (= 2GB)
    if (size > Integer.MAX_VALUE) {
        Log.d("XXX", "File too big");
        return null;
    }

    int iSize = (int) size;
    try {
        fis = new FileInputStream(file);

        // part of Android since API level 1 - buffer can scale
        ByteArrayBuffer bb = new ByteArrayBuffer(iSize);

        // some rather small fixed buffer for actual reading
        byte[] buffer = new byte[1024];

        int read;
        while ((read = fis.read(buffer)) != -1) {
            // just append data as long as we can read more
            bb.append(buffer, 0, read);
        }

        // return a new string based on the large buffer
        return new String(bb.buffer(), 0, bb.length());
    } catch (FileNotFoundException e) {
        Log.w("XXX", e);
    } catch (IOException e) {
        Log.w("XXX", e);
    } catch (OutOfMemoryError e) {
        // this could be left out. Keep if you read several MB large files.
        Log.w("XXX", e);
    } finally {
        // finally is executed even if you return in above code
        // fis will be null if new FileInputStream(file) throws
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                // ignored, nothing can be done if closing fails
            }
        }
    }
    return null;
}
于 2012-08-17T21:01:55.850 に答える