2012-05-11 8 views
1

私はAndroidのアプリを書いています。 2つのアクティビティ、1つは「hello message」と入力するTextEdit、内部ストレージにメッセージを保存するボタンです。第二は主な活動です。アプリ起動後にHelloメッセージが表示されます。IOはFileInputStream.readの例外です。 Android

第二アクティビティ:

String s = ((EditText) findViewById(R.id.message_act_editText_hello)).getText().toString(); 
    FileOutputStream fos = openFileOutput(Lab2AndroidActivity.FILENAME, Context.MODE_PRIVATE); 
    fos.write(s.getBytes()); 
    fos.close(); 

最初の(メイン)活動:

static String FILENAME = "message_file.zip"; 
    FileOutputStream fos; 
    try { 
     //piece of code to guarantee that file exists 
     fos = openFileOutput(Lab2AndroidActivity.FILENAME, Context.MODE_APPEND); 
     fos.close(); 
    } catch (FileNotFoundException e1) { 
     e1.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 


    try { 
     fis = openFileInput(FILENAME); 
     messageString = new StringBuffer(""); 
     while ((length = fis.read(buffer)) != -1) { 
      String temp = new String(buffer, 0,length); 
      messageString.append(temp); 
      fis.close(); 
     } 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    Toast t = Toast.makeText(this, messageString, 3000); 
    t.show(); 

私はラインでlogcatにIO例外を取得しています:

while ((length = fis.read(buffer)) != -1) 

が、アプリが動作しているようです正しく(アプリケーションの開始後に定義されたメッセージが表示されます)。私は説明を見つけようとしましたが、いくつかのトピックが見つかりましたが、すべて大容量のファイル、資産のファイル、または圧縮ファイルに従っていました。 私は別の拡張機能を試して

static String FILENAME = "message_file.zip", 
static String FILENAME = "message_file.txt", 

ように私のファイルに名前を付けてみましたが、常に私は同じIO例外を取得しています。

ありがとうございます。当然の

答えて

0

に追加する必要が忘れています。問題は断片化しました:

while ((length = fis.read(buffer)) != -1) { 
     String temp = new String(buffer, 0,length); 
     messageString.append(temp); 
     fis.close(); 
    } 

キャッチは何ですか?

fis.close(); 

は後にしてください。私は昨日気付かなかった...

0

あなたのファイルが終了していないIO例外を取得し、あなたがそれを開くために要求 あなたはこのコードを使用することができ、あなたの最初の活動では、コード

File myFile = new File("/sdcard/mysdfile.txt"); 

のこのpeiceを忘れてしまう

public class SecondActivity extends Activity { 

    private TextView txtData2; 
    @Override 
     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main2); 
     txtData2 = (TextView) findViewById(R.id.textView2); 
     try { 
      File myFile = new File("/sdcard/mysdfile.txt"); 
      FileInputStream fIn = new FileInputStream(myFile); 
      BufferedReader myReader = new BufferedReader(
        new InputStreamReader(fIn)); 
      String aDataRow = ""; 
      String aBuffer = ""; 
      while ((aDataRow = myReader.readLine()) != null) { 
       aBuffer += aDataRow + "\n"; 
      } 
      txtData2.setText(aBuffer); 
      myReader.close(); 
      Toast.makeText(getBaseContext(), 
        "Done reading SD 'mysdfile.txt'", 
        Toast.LENGTH_SHORT).show(); 
     } catch (Exception e) { 
      Toast.makeText(getBaseContext(), e.getMessage(), 
        Toast.LENGTH_SHORT).show(); 
     } 
    } 

} 

もみ:あなたはこれを使用することができます秒1で

public class MainActivity extends Activity { 
    /** Called when the activity is first created. */ 
    EditText txtData; 
    Button btnWriteSDFile; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    // bind GUI elements with local controls 
    txtData = (EditText) findViewById(R.id.txtData); 
    txtData.setHint("Enter some lines of data here..."); 

    btnWriteSDFile = (Button) findViewById(R.id.btnWriteSDFile); 
    btnWriteSDFile.setOnClickListener(new OnClickListener() { 

    public void onClick(View v) { 
     // write on SD card file data in the text box 
     try { 
      File myFile = new File("/sdcard/mysdfile.txt"); 
      myFile.createNewFile(); 
      FileOutputStream fOut = new FileOutputStream(myFile); 
      OutputStreamWriter myOutWriter = 
            new OutputStreamWriter(fOut); 
      myOutWriter.append(txtData.getText()); 
      myOutWriter.close(); 
      fOut.close(); 
      Toast.makeText(getBaseContext(), 
        "Done writing SD 'mysdfile.txt'", 
        Toast.LENGTH_SHORT).show(); 
      Intent i = new Intent(getApplicationContext(),SecondActivity.class); 
      startActivity(i); 
     } catch (Exception e) { 
      Toast.makeText(getBaseContext(), e.getMessage(), 
        Toast.LENGTH_SHORT).show(); 
     } 
    }// onClick 
    }); 
} 
} 

EditTextだけのTextViewとボタン

第二のLinearLayoutが含まれているのLinearLayoutを使用していますlatout tは

は、問題は私が知っている見つけた場合、それは正常に動作してください!

ああ、私はその理由を見つけ、あなたのマニフェスト

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
+0

私は内部ストレージを使用したい、外部ではない。私のファイルが存在し、openFileOutput(文字列名、intモード)が存在しない場合、ファイルを作成します。私はIO例外を取得していますが、 'while((length = fis.read(buffer))!= -1)'のFileNotFoundExceptionではありません。 –