【问题标题】:add url from text file to string array (Android)将文本文件中的 url 添加到字符串数组 (Android)
【发布时间】:2014-08-14 19:52:33
【问题描述】:

我正在开发图片库应用程序,我在其中从互联网加载图像,所以我在文本文件中添加了许多图像 URL,现在我想将这些 URL 从文本文件添加到字符串数组。

我不知道如何将文件中的文本添加到我的代码中 (public static final String[] IMAGES = new String[] {};)

这是我的代码示例:

在此代码中,我手动添加了 URl,但我想将 URL 从文本文件添加到 public static final String[] IMAGES = new String[]{};

public final class Constants {

    public static final String[] IMAGES = new String[] {                        
            // Light images
            "http://tabletpcssource.com/wp-content/uploads/2011/05/android-logo.png",
            "http://simpozia.com/pages/images/stories/windows-icon.png",
            "http://radiotray.sourceforge.net/radio.png",
            "http://www.bandwidthblog.com/wp-content/uploads/2011/11/twitter-logo.png",
            "http://weloveicons.s3.amazonaws.com/icons/100907_itunes1.png",
            "http://weloveicons.s3.amazonaws.com/icons/100929_applications.png",
            "http://www.idyllicmusic.com/index_files/get_apple-iphone.png",
            "http://www.frenchrevolutionfood.com/wp-content/uploads/2009/04/Twitter-Bird.png",
            "http://3.bp.blogspot.com/-ka5MiRGJ_S4/TdD9OoF6bmI/AAAAAAAAE8k/7ydKtptUtSg/s1600/Google_Sky%2BMaps_Android.png",
            "http://www.desiredsoft.com/images/icon_webhosting.png",
            "http://goodereader.com/apps/wp-content/uploads/downloads/thumbnails/2012/01/hi-256-0-99dda8c730196ab93c67f0659d5b8489abdeb977.png",
            "http://1.bp.blogspot.com/-mlaJ4p_3rBU/TdD9OWxN8II/AAAAAAAAE8U/xyynWwr3_4Q/s1600/antivitus_free.png",
            "http://cdn3.iconfinder.com/data/icons/transformers/computer.png",
            "http://cdn.geekwire.com/wp-content/uploads/2011/04/firefox.png?7794fe",
            "https://ssl.gstatic.com/android/market/com.rovio.angrybirdsseasons/hi-256-9-347dae230614238a639d21508ae492302340b2ba",
            "http://androidblaze.com/wp-content/uploads/2011/12/tablet-pc-256x256.jpg",
            "http://www.theblaze.com/wp-content/uploads/2011/08/Apple.png",
            "http://1.bp.blogspot.com/-y-HQwQ4Kuu0/TdD9_iKIY7I/AAAAAAAAE88/3G4xiclDZD0/s1600/Twitter_Android.png",
            "http://3.bp.blogspot.com/-nAf4IMJGpc8/TdD9OGNUHHI/AAAAAAAAE8E/VM9yU_lIgZ4/s1600/Adobe%2BReader_Android.png",
            "http://cdn.geekwire.com/wp-content/uploads/2011/05/oovoo-android.png?7794fe",
            "http://icons.iconarchive.com/icons/kocco/ndroid/128/android-market-2-icon.png",
            "http://thecustomizewindows.com/wp-content/uploads/2011/11/Nicest-Android-Live-Wallpapers.png",
            "http://c.wrzuta.pl/wm16596/a32f1a47002ab3a949afeb4f",
            "http://macprovid.vo.llnwd.net/o43/hub/media/1090/6882/01_headline_Muse.jpg",           
    };

    private Constants() {
    }

    public static class Config {
        public static final boolean DEVELOPER_MODE = false;
    }

    public static class Extra {
        public static final String IMAGES = "com.nostra13.example.universalimageloader.IMAGES";
        public static final String IMAGE_POSITION = "com.nostra13.example.universalimageloader.IMAGE_POSITION";
    }
}

【问题讨论】:

标签: android streamreader filereader arrays


【解决方案1】:

好的,没问题。

我建议你使用列表而不是数组,因为数组的大小是固定的,而数组列表的大小是无限的。

public final class Constants {

   public static List<String> IMAGES = new ArrayList<String>();

   private Constants() {
   }

   public static class Config {
       public static final boolean DEVELOPER_MODE = false;
   }



   public static class Extra {
        public static final String IMAGES = "com.nostra13.example.universalimageloader.IMAGES";
       public static final String IMAGE_POSITION = "com.nostra13.example.universalimageloader.IMAGE_POSITION";
    }

    public void parseFile(){
      try {

          String sCurrentLine;

          br = new BufferedReader(new FileReader("C:\\myFile.txt"));

          while ((sCurrentLine = br.readLine()) != null) {
              IMAGES.add(sCurrentLine);
          }
          br.close();
      } 
      catch (IOException e) {
         e.printStackTrace();
      } 
      finally {
          try {
              if (br != null)br.close();
          } 
          catch (IOException ex) {
                   ex.printStackTrace();
          }
       }
     }
}

【讨论】:

  • 感谢此代码,但我是否用此代码替换我的代码
  • 兄弟在这里需要帮助我将我的代码更改为您的代码,但我收到了Class Extra 的错误,您没有在代码中定义,请查看我的代码Class Extra
【解决方案2】:

这是解释你想要什么的链接:http://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/

    try {

        String sCurrentLine;

        br = new BufferedReader(new FileReader("C:\\myFile.txt"));

        int i = 0;

        while ((sCurrentLine = br.readLine()) != null) {
              IMAGES[i] = sCurrentLine;
              i++;
        }
        br.close();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

【讨论】:

  • 兄弟我是新来的,你能帮我用我的代码调整示例代码吗?我如何用我的代码实现它
  • 完成了。几分钟前我纠正了我的例子。 A 添加了您的“额外”课程
  • 现在我遇到了新问题:The method putExtra(String, boolean) in the type Intent is not applicable for the arguments (String, List&lt;String&gt;) 这里: public void onImagePagerClick(View view) { Intent intent = new Intent(this, ImagePagerActivity.class); intent.putExtra(Extra.IMAGES, IMAGES); startActivity(intent); }
猜你喜欢
  • 2019-12-17
  • 2013-09-24
  • 2016-06-14
  • 2017-08-03
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多