0

i am working on an image gallery app in which i am loading images from URL so i have saved some image URLs in a text file and i am trying to read URLs from text file to ArrayList<String> but i am not able to load images in my app.

i tried this: but not works images are not loading

package com.nostra13.example.universalimageloader;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public final class Constants {

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

    public void parseFile() {

        BufferedReader br = null;
        try {

              String sCurrentLine;

              br = new BufferedReader(new FileReader("link.txt"));

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

    }

    public static final String[] IMAGES = LIST.toArray(new String[LIST.size()]);

    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";
    }
}

but if i add URLs manually like this: it works.

public static final String[] IMAGES = new String[] {
    "https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcTybItEfE2Xu-Or72BHw8uZf19_mV2Kr8cuuU8kKYrVbeZPXIeX-Q",
"https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcTybItEfE2Xu-Or72BHw8uZf19_mV2Kr8cuuU8kKYrVbeZPXIeX-Q",
"https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcTybItEfE2Xu-Or72BHw8uZf19_mV2Kr8cuuU8kKYrVbeZPXIeX-Q",
"https://encrypted-tbn3.gstatic.com/images?q=tbn:ANd9GcTybItEfE2Xu-Or72BHw8uZf19_mV2Kr8cuuU8kKYrVbeZPXIeX-Q",
    };

but i want to add text from text file (sdcard/file.txt) instead of manually adding.

2 Answers2

0

Well, did you try and use Log.d() to post the lines of text that is being read from the file?

My best guess is that it's not reading the text file correctly, or perhaps formatting it wrongly (missing spaces perhaps, so it thinks its 1 big string?)

Moonbloom
  • 7,738
  • 3
  • 26
  • 38
  • Well, try what i just said and post the result. I literally can't do anything to help you if you don't try. – Moonbloom Aug 15 '14 at 16:10
0

Try with this code:

public void parseFile() {
    File sdcard = Environment.getExternalStorageDirectory();

    File file = new File(sdcard,"link.txt");

    BufferedReader br = null;
    try {
          String sCurrentLine;
          br = new BufferedReader(new FileReader(file));

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

}

The call to Environment.getExternalStorageDirectory() will return the path of SD card, and the path of declared variable file is relative to the root of SD card. If the file on a folder, you should declare:

File file = new File(sdcard,"data/com.myapplication.example/images/link.txt");
Jose Rodriguez
  • 9,753
  • 13
  • 36
  • 52