Android java load images from url - java

I have a question
private final String images[] = {"http://developer.android.com/images/dialog_custom.png", "http://developer.android.com/images/dialog_progress_bar.png"};
how can i change this
to load all of the links from a file from my website?
like a .txt on my site just has like
http://link.com/1.png
http://link.com/2.png
http://link.com/3.png
http://link.com/4.png
http://link.com/5.png
http://link.com/6.png
http://link.com/7.png
http://link.com/8.png
http://link.com/9.png
http://link.com/10.png
so it will just load all those then later on i can add more links without having to update the app

Use a BufferedReader to readLine() to a String and then fetch the image...

Just put the space or "," or "/n" in file then get all data in a string one by one
String reader = "";
String separated ;
BufferedReader buffer = new BufferedReader(new FileReader("YourFileName.txt"));
while ((reader = buffer.readLine()) != null) {
separated = reader.split(" "); or reader.split(","); or reader.split("/n");
//Here at each itration separated will have your single link
}
buffer.close;

Try out the following links:
How to load an ImageView by URL in Android?
http://russenreaktor.wordpress.com/2009/08/11/android-imageloader-load-images-sequencially-in-the-background/
There are plenty of links that provide solutions--enjoy.

Related

Java, CSV, Getting error if do not click on Save button of the csv file

This is my code
public double myMethod(String name)
{
double result = 0.0;
String path = "/Users/T/Desktop/Training/MyFolder/";
int maxColumn = 0;
BufferedReader br = null;
ArrayList findMaxColumn = new ArrayList();
String line = "";
try
{
br = new BufferedReader(new FileReader(path+name));
while((line = br.readLine())!= null)
{
findMaxColumn.add(temp.split(",").length);
}
maxColumn = getMaxNumber(findMaxColumn);
CSVReader reader = new CSVReader(new FileReader(path+name));
List<String[]> myData = reader.readAll();
for(int i = 0 ; i < maxColumn;i++)
{
for (String[] lineData : myData)
{
String value= lineData[i];
The problem is, I have a csv file (generated from other method and stored in MyFolder) and when I run this code, I got an error "ArrayIndexOutOfBoundsException: 1" at String value= lineData[i]. But, if I open my csv file and click on save button (or make some changes for a value eg 0 to 1 etc) and close it before I run this code then it will be fine when I run it. That's weird!!! Could anyone explain to me why I have to open the csv file and make some changes(just click on save button or change a value to another) to ignore the problem and how to fix it?
Check that your encoding when you save the file is the same encoding with the one you use when you read the file. It may well be that you are saving, for example, in UTF8 and reading the file as it would be UTF16.
This fits what you describe (if opening and saving the file before reading it, then it works) as well as the ""ArrayIndexOutOfBoundsException: 1"" - showing that the "split" method did not find any separator (the comma) thus return a single string.
It would also help if using a debugger to check what is in your array just-to-be-added-to findMaxColumn after splitting. Easier to debug if you use a local variable to store the result of split before adding:
while((line = br.readLine())!= null)
{
String splitResult[]=temp.split(","); // easier to examine
findMaxColumn.add(splitResult);
}

How to access values of a line, while reading in a text file in Java

I am trying to load in two files at the same time but also access the first gps1 file. I want to access the gps1 file line-by-line and depending on the sentence type which I will explain later I want to do different stuff with that line and then move to the next line.
Basically gps1 for example has multiple lines but each line falls under a couple of catagories all starting with $GPS(then other characters). Some of these types have a time stamp which I need to collect and some types do not have a time stamp.
File gps1File = new File(gpsFile1);
File gps2File = new File(gpsFile2);
FileReader filegps1 = new FileReader(gpsFile1);
FileReader filegps2 = new FileReader(gpsFile2);
BufferedReader buffer1 = new BufferedReader(filegps1);
BufferedReader buffer2 = new BufferedReader(filegps2);
String gps1;
String gps2;
while ((gps1 = buffer1.readLine()) != null) {
The gps1 data file is as follows
$GPGSA,A,3,28,09,26,15,08,05,21,24,07,,,,1.6,1.0,1.3*3A
$GPRMC,151018.000,A,5225.9627,N,00401.1624,W,0.11,104.71,210214,,*14
$GPGGA,151019.000,5225.9627,N,00401.1624,W,1,09,1.0,38.9,M,51.1,M,,0000*72
$GPGSA,A,3,28,09,26,15,08,05,21,24,07,,,,1.6,1.0,1.3*3A
Thanks
I don't really understand the problem you are facing but anyway, if you want to get your lines content you can use a StringTokenizer
StringTokenizer st = new StringTokenizer(gps1, ",");
And then access the data one by one
while(st.hasMoreToken)
String s = st.nextToken();
EDIT:
NB: the first token will be your "$GPXXX" attribute

read data from a file in java

I have a text file in the following format:
Details.txt
The file is a .txt file. I want to read course title from this file and print corresponding textbook and instructor information. But i am not sure about what process to follow ? storing the information in an array won't be efficient! How should i proceed ? NOTE: I can't change the info in the file, it should not be changed!! obviously the file will be read by the following code:
File newFile=new File("C:/details");
but how should i extract the data from this file according to the labels course title, textbook and instructor!?
First read the file correctly line by line, and search for your entered course title, lets consider "Java"
Now you hit your title and you know you need 3 consecutive lines from your file as all information related to that title are there.
if(str.startsWith(title)); { // for title = "Java"
line1 = 1st line // contains ISBN and First Name
line2 = 2nd line // Title and Last Name
line3 = 3rd line // Author and Department
line4 = 4th line // Email
break; // this will take you out of while loop
}
Now on those four lines do string operations and extract your data as you need and use it.
I am home so I can't give you exact code. But if you follow this it will solve your issue. Let me know if any problem you got while doing this.
Follow this to get some info on String operations
Use String Tokenizer and separate each string and then store them in a Linked List or Array List. Have Separate List for each title like course title, instructor etc. and then print them
//Find the directory for the SD Card using the API
//*Don't* hardcode "/sdcard"
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard,"file.txt");
//Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
}
catch (IOException e) {
//You'll need to add proper error handling here
}
//Find the view by its id
TextView tv = (TextView)findViewById(R.id.text_view);
//Set the text
tv.setText(text);
you can use FileUtils.readFileToString(new File(""C:/details.txt");
Now you can extract the required data based on your wish
Use Scanner class
Scanner s=new Scanner(new File("C:/Details.txt"));
while(s.hasNext())
{
System.out.println(s.nextLine());
}
if you want in work by word then use String Tokenizer
see this article

Textscreen in Codename One, how to read text file?

I want to add a help screen to my Codename One App.
As the text is longer as other strings, I would like put it in a separate file and add it to the app-package.
How do I do this? Where do I put the text file, and how can I easily read it in one go into a string?
(I already know how to put the string into a text area inside a form)
In the Codename One Designer go to the data section and add a file.
You can just add the text there and fetch it using myResFile.getData("name");.
You can also store the file within the src directory and get it using Display.getInstance().getResourceAsStream("/filename.txt");
I prefer to have the text file in the filesystem instead of the resource editor, because I can just edit the text with the IDE. The method getResourceAsStream is the first part of the solution. The second part is to load the text in one go. There was no support for this in J2ME, you needed to read, handle buffers etc. yourself. Fortunately there is a utility method in codename one. So my working method now looks like this:
final String HelpTextFile = "/helptext.txt";
...
InputStream in = Display.getInstance().getResourceAsStream(
Form.class, HelpTextFile);
if (in != null){
try {
text = com.codename1.io.Util.readToString(in);
in.close();
} catch (IOException ex) {
System.out.println(ex);
text = "Read Error";
}
}
The following code worked for me.
//Gets a file system storage instance
FileSystemStorage inst = FileSystemStorage.getInstance();
//Gets CN1 home`
final String homePath = inst.getAppHomePath();
final char sep = inst.getFileSystemSeparator();
// Getting input stream of the file
InputStream is = inst.openInputStream(homePath + sep + "MyText.txt");
// CN1 Util class, readInputStream() returns byte array
byte[] b = Util.readInputStream(is);
String myString = new String(b);

Why do I loose new line character when I load text from a java servlet to JTextPane?

I try to load content of a text file that contains some text in multiple lines using java servlet.
When I test servlet in browser it works fine. Text is loaded with new line chars.
But when I load it to a string in my swing application and then use textpane.setText(text); new lines are gone. I tried many solutions I found int the net, but still can't get it right.
Servlet Code:
Reading text from file (simplified):
File file = new File(path);
StringBuilder data = new StringBuilder();
BufferedReader in = new BufferedReader(new FileReader(file));
String line;
while ((line = in.readLine()) != null) {
data.append(line);
data.append("\n");
}
in.close();
Sending text:
PrintWriter out = response.getWriter();
out.write(text));
Is it some platform issue? Servlet was writen and compiled on Linux, but I run it on Windows (on JBoss). Textfiles are also stored on my machine.
Instead of data.append("\n") use
data.append(System.getProperty("line.separator"));

Categories