2017-03-02 3 views
-1

私の電話のダウンロードフォルダにあるcsvファイルへのFileInputStreamを作成する次のコードがあります。外部記憶域のファイルの長さを読み取りますが、fileNotFoundExceptionを取得します

ファイルの長さをログに記録できますが、FileNotFoundExceptionがスローされています。

正しい長さが出力されていると、どうすればいいですか?

おかげ

String fileName = "Download/file1.csv"; 
     String path = Environment.getExternalStorageDirectory()+"/"+fileName; 
     File file = new File(path); 

     Log.e(TAG, "file length = " + file.length()); 


     FileInputStream fin = null; 
     try { 
      // create FileInputStream object 
      fin = new FileInputStream(file); 

      byte fileContent[] = new byte[(int)file.length()]; 

      // Reads up to certain bytes of data from this input stream into an array of bytes. 
      fin.read(fileContent); 
      //create string from byte array 
      String s = new String(fileContent); 
      Log.e(TAG, "fileData = " + s); 
     } 
     catch (FileNotFoundException e) { 

      Log.e(TAG, "File not found" + e); 
     } 
     catch (IOException ioe) { 

      Log.e(TAG, "Exception while reading file " + ioe); 
     } 
     finally { 
      // close the streams using close method 
      try { 
       if (fin != null) { 
        fin.close(); 
       } 
      } 
      catch (IOException ioe) { 
       System.out.println("Error while closing stream: " + ioe); 
       Log.e(TAG, "Error while closing stream: " + ioe); 
      } 
     } 

。私はそれを認識してんだけど、私は23 <ターゲットならば、私は、実行時にパーミッションを要求するコードを記述する必要はありませんという印象の下で午前@Selvin

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



03-02 11:16:21.922 10112-10112/cftagwriter.carefreegroup.com.cftagwriter E/WriteTagActivity: file length = 523 
03-02 11:16:21.922 10112-10112/cftagwriter.carefreegroup.com.cftagwriter E/WriteTagActivity: File not foundjava.io.FileNotFoundException: /storage/emulated/0/Download/file1.csv: open failed: EACCES (Permission denied) 
+1

。 – turtleboy

+0

@Selvin AndyGeekyを見てください。http://stackoverflow.com/questions/33030933/android-6-0-open-failed-eacces-permission-denied – turtleboy

+0

あなたのアプリに権限があるかどうかチェックしましたか? – Selvin

答えて

0
//Create folder first 
String dirPath = Environment.getExternalStorageDirectory()+"/Download"; 
     File dir = new File(dirPath); 
     dir.mkdir(); 

//Create File 
     File file = new File(dir,"file1.csv"); 
+1

このコードは質問に答えるかもしれませんが、どのようにして問題が解決されたのか、なぜそれが解決されるのかについての追加の文脈を提供すると、回答の長期的価値が向上します。 –

関連する問題