0

i am working on image gallery app in which i am loading images from internet so i have added many image URLs in a text file now i want to add these URLs from text file to string array.

i don't know how to add text from file to my code (public static final String[] IMAGES = new String[] {};)

this is the sample of my code:

in this code i added URl manually but i want to add URL from text file to 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";
    }
}

2 Answers2

1

Ok, no problem.

I advise you to use a list instead of an array because an array has a fixed size while an arraylist has an unlimited size.

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();
          }
       }
     }
}
user2274060
  • 896
  • 5
  • 18
  • 35
-1

This is the link explaining what you want : 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();
        }
    }
user2274060
  • 896
  • 5
  • 18
  • 35
  • bro i am new here, can you help me with sample adjusted code with my code. how do i implement it with my code – android overflow Aug 14 '14 at 20:07
  • It's done. I rectify my example a few minutes ago. A added your "extra" class – user2274060 Aug 14 '14 at 20:52
  • now i am getting new problem: `The method putExtra(String, boolean) in the type Intent is not applicable for the arguments (String, List)` **here:** `public void onImagePagerClick(View view) { Intent intent = new Intent(this, ImagePagerActivity.class); intent.putExtra(Extra.IMAGES, IMAGES); startActivity(intent); }` – android overflow Aug 14 '14 at 20:55