2010-04-29 52 views
0

当我在虚拟机器人(1.5)中运行代码时,它运行良好,TextSwitcher显示来自/ sdcard/documents /的每个txt文件的前80个字符,但是当我在Samsung Galaxy i7500(1.6)上运行它时,在TextSwitcher中没有内容,但是在LogCat中有txt文件的FileName。Android - 无法从真实机器上的SDcard读取TXT文件?

我的代码:

 public void getTxtFiles(){ 
//Scan /sdcard/documents and put .txt files in array File TxtFiles[] 
    String path = Environment.getExternalStorageDirectory().toString()+"/documents/"; 
    String files; 
    File folder = new File(path); 
    if(folder.exists()==false){if (!folder.mkdirs()) { 
     Log.e("TAG", "Create dir in sdcard failed"); 
     return; 
    }} 
    else{ 
    File listOfFiles[] = folder.listFiles(); 

    for (int i = 0; i < listOfFiles.length; i++) 
    { 

    if (listOfFiles[i].isFile()) 
    { 
    files = listOfFiles[i].getName(); 
     if (files.endsWith(".txt") || files.endsWith(".TXT")) 
     { 
      if((files.length()-1)>i){resizeArray(TxtFiles, files.length()+10);} 
      TxtFiles[i]=listOfFiles[i]; 
      System.out.println(TxtFiles[i]); 
     } 
      } 
     }} 
    } 


private void updateCounter(int Pozicija) { 
if(Pozicija<0){Toast.makeText(getApplicationContext(), R.string.LastTxt, 5).show(); 
mCounter++;} 
else if(TxtFiles[mCounter]!=null){ 
    TextToShow = getContents(TxtFiles[mCounter]); 
    if(TextToShow.length()>80)TextToShow=TextToShow.substring(0, 80); 
    mSwitcher.setText(TextToShow); 
    System.out.println(Pozicija); 
    } 
else mCounter--; 
} 

    static public String getContents(File aFile) { 
    //...checks on aFile are elided 
    StringBuilder contents = new StringBuilder(); 

    try { 
     //use buffering, reading one line at a time 
     //FileReader always assumes default encoding is OK! 
     BufferedReader input = new BufferedReader(new FileReader(aFile)); 
     try { 
     String line = null; //not declared within while loop 
     /* 
     * readLine is a bit quirky : 
     * it returns the content of a line MINUS the newline. 
     * it returns null only for the END of the stream. 
     * it returns an empty String if two newlines appear in a row. 
     */ 
     while ((line = input.readLine()) != null){ 
      contents.append(line); 
      contents.append(System.getProperty("line.separator")); 
     } 
     } 
     finally { 
     input.close(); 
     } 
    } 
    catch (IOException ex){ 
     ex.printStackTrace(); 
    } 

    return contents.toString(); 
    } 

而且我能写,虽然logcat的这些文件的内容!

任何想法?

+0

你可以发布相关的logcat输出吗? – 2010-04-29 16:02:53

+0

当然:http://pastebin.com/FJBAzgq9 – JustMe 2010-04-29 18:12:24

回答

1

问题解决:Android基于Linux内核,具有文件权限。我想我的应用程序没有权限在/ sdcard/documents /中的这些文件。