2016-09-20 6 views
0

PCのファイルブラウザ経由でアンドロイド電話のsdcardにテキストファイルを作成します。それから私はアンドロイドアプリでそれを読み取ろうとします。私はFileNotFoundException - no such path or directory existエラーが発生します。ここでSDCardからの読み込み中にFileNotFoundExceptionが発生しました

は私のコードは

private String readFromFile(Context context) { 

     StringBuilder text = new StringBuilder(); 
     String state = Environment.getExternalStorageState(); 

     if (Environment.MEDIA_MOUNTED.equals(state)) { 
      File sdcard = Environment.getExternalStorageDirectory(); 
      File file = new File(sdcard,"sample.txt"); 
      //Read text from file 
      try { 
       //File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "sample.txt"); 
       BufferedReader br = new BufferedReader(new FileReader(file));//Error occurs here 
       String line; 
       Toast.makeText(context, "success!", Toast.LENGTH_LONG).show(); 
       while ((line = br.readLine()) != null) { 
        text.append(line); 
        text.append('\n'); 
       } 
       br.close(); 
      } 
      catch (IOException e) { 
       e.getMessage(); 
       Toast.makeText(context, "Error!", Toast.LENGTH_LONG).show(); 
       //You'll need to add proper error handling here 
      } 
     } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { 
// We can only read the media 
     } else { 
// Something else is wrong. It may be one of many other states, but all we need 
// to know is we can neither read nor write 
     }  
     return text.toString(); 
    } 

sample.txtです(私はトリプルチェックした)SDカードのルートディレクトリに存在しています。それはいくつかのテキストを持っています。

私はマニフェストファイルに外部記憶権を読み込みました。

のAndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.bulsy.graphapp"> 
    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

私もそれがエラーを示す動作しませんでした、その後、アプリ切断形のPCを実行してみました。簡単な問題かもしれません。誰かが私がどこに間違っていたか説明できますか?

+2

あなたのセカンダリストレージ(sdカード)ではなく、sample.txtが使用可能かどうかを再度確認してください。 –

+0

これはsdcardに正しくなければなりませんか?私はBufferedReaderが '/ storage/sdcard0/sample.txt'から' Filenotfound'例外を読み込もうとしている間にデバッグしています –

答えて

1

試してください。ログに以下を印刷して、パスが何であるかを教えてください:sdcard.getPath()

また、urファイルを見るには、ESファイルエクスプローラのようなアプリを使い、完全なパスを取得してください。彼らは一致しますか?

パスが一致した場合、この置き換え:これはどこに問題があるuは排除するのに役立ちます

File file = new File("full_path_of_the_file"); 

:これにより

File file = new File(sdcard,"sample.txt"); 

を。
幸運。

+0

パスは/storage/sdcard0/sample.txtです –

+0

以下を試してみてください。 'getExternalStorageDirectory()'を 'getExternalStorageDirectory(null)'に置き換えてください。違いがあるかどうかを確認してください –

+0

パスが違っていました! ESエクスプローラは、それが '/ storage/sdcard1/sample.txt'だと言います!それは働いた!あなたが言ったように私はそれを変更したとき!乾杯! –

関連する問題