2015-02-09 55 views
0

我想從android應用程序讀取文件。此文件位於通過OTG電纜連接到Android平板電腦的外部存儲器(pendrive)中。如何在Android手機中從外部存儲讀取文本文件

我搜查了很多關於「如何做到這一點」,但無法得到任何方式。你能幫我做到嗎?

+0

如果你知道確切的目錄,你爲什麼不就像你在你的代碼會讀嗎? – Eenvincible 2015-02-09 05:50:10

+0

爲您的清單添加權限 – 2015-02-09 06:07:55

+0

@RandykaYudhistira哪些權限適合連接USB通過OTG?我試過外部存儲讀寫,但我認爲它只適用於SD卡,實際上我想通過OTG訪問Pendrive並將文件從Sdcard移動到USB。 – 2015-09-25 08:22:51

回答

-1

請使用下面的代碼來自外部存儲讀取文件: -

String line, certstr = ""; 

try { 
    BufferedReader reader = new BufferedReader(new FileReader("/mnt/sdcard/file.txt")); 

     while((line = reader.readLine()) != null) 
     {    
      certstr += line; 
     }        
     reader.close(); 
} catch (FileNotFoundException e2) { 
    // TODO Auto-generated catch block 
    Toast.makeText(getApplicationContext(), "File not found in /mnt/sdcard/", 2).show(); 
    e2.printStackTrace(); 
    return false; 
} catch (IOException e2) { 
    // TODO Auto-generated catch block 
    e2.printStackTrace(); 
    return false; 
} 
+1

文本文件將在pendrive中。該pendrive將通過otg電纜連接到平板電腦。是適用於這種情況的相同的代碼? – 2015-02-09 06:50:31

相關問題