Android - 无法在真机上从SD卡读取TXT文件?

发布于 2024-08-30 22:15:52 字数 2493 浏览 6 评论 0原文

当我在虚拟 android (1.5) 中运行下面的代码时,它运行良好,TextSwitcher 显示 /sdcard/documents/ 中每个 txt 文件的前 80 个字符,但是当我在 Samsung Galaxy i7500 (1.6) 上运行它时,没有内容在 TextSwitcher 中,但是在 LogCat 中,有 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 写入这些文件的内容!

有什么想法吗?

When I run the code bellow in the virtual android (1.5) it works well, TextSwitcher shows first 80 chars from each txt file from /sdcard/documents/ , but when I run it on my Samsung Galaxy i7500 (1.6) there are no contents in TextSwitcher, however in LogCat there are FileNames of txt files.

My Code:

      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();
  }

And I am able to write contents of those files though LogCat!

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘寄 2024-09-06 22:15:52

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

Problem solved: Android is based on Linux kernel and it has file permissions. I guess my app didnt have permissions over those files in /sdcard/documents/.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文