Issue in loading images from web
I am new in android and had developed an app which get images from the website and display it. I got it working in emulator but not in real phones. In some device, it will crash or take very long loading period. Can anyone please help me or guide me in improving it as i'm not sure whether the way i loads the images is correct or not.
Here are the code i use to get the images from the web and display accordingly.
if (xmlURL.length() != 0) {
try {
URL url = new URL(xmlURL);
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
/* Get the XMLReader of the SAXParser we created. */
XMLReader xr = sp.getXMLReader();
/*
* Create a new ContentHandler and apply it to the
* XML-Reader
*/
xr.setContentHandler(myExampleHandler);
/* Parse the xml-data from our URL. */
xr.parse(new InputSource(url.openStream()));
/* Parsing has finished. */
/*
* Our ExampleHandler now provides the parsed data to
* us.
*/
ParsedExampleDataSet parsedExampleDataSet = myExampleHandler.getParsedData();
} catch (Exception e) {
}
}
if (s.equalsIgnoreCase("wallpapers")) {
Context context = helloAndroid.this.getBaseContext();
for (int j = 0; j <= myExampleHandler.filenames.size() - 1; j++) {
if (myExampleHandler.filenames.elementAt(j).toString() != null) {
helloAndroid.this.ed = myExampleHandler.thumbs.elementAt(j)
.toString();
if (helloAndroid.this.ed.length() != 0) {
Drawable image = ImageOperations(context,
helloAndroid.this.ed, "image.jpg");
file_info = myExampleHandler.filenames
.elementAt(j).toString();
author = "\nby "
+ myExampleHandler.authors.elementAt(j)
.toString();
switch (j + 1) {
case 1:
ImageView imgView1 = new ImageView(context);
imgView1 = (ImageView) findViewById(R.id.image1);
if (image.getIntrinsicHeight() > 0) {
imgView1.setImageDrawable(image);
} else
imgView1
.setImageResource(R.drawable.empty_wallpaper);
tv = (TextView) findViewById(R.id.filename1);
tv.setText(file_info);
tv = (TextView) findViewById(R.id.author1);
tv.setText(author);
imgView1
.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Perform action on click
Intent myIntent1 = new Intent(
helloAndroid.this,
galleryFile.class);
Bundle b = new Bundle();
b.putString("fileID",myExampleHandler.fileid.elementAt(0).toString());
b.putString("page", "1");
b.putString("family", s);
b.putString("fi",myExampleHandler.folder_id.elementAt(folder).toString());
b.putString("kw", keyword);
myIntent1.putExtras(b);
startActivityForResult(
myIntent1, 0);
}
});
break;
case 2:
ImageView imgView2 = new ImageView(context);
imgView2 = (ImageView) findViewById(R.id.image2);
imgView2.setImageDrawable(image);
tv = (TextView) findViewById(R.id.filename2);
tv.setText(file_info);
tv = (TextView) findViewById(R.id.author2);
tv.setText(author);
imgView2
.set开发者_运维百科OnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Perform action on click
Intent myIntent1 = new Intent(
helloAndroid.this,
galleryFile.class);
Bundle b = new Bundle();
b.putString("fileID",myExampleHandler.fileid.elementAt(1).toString());
b.putString("page", "1");
b.putString("family", s);
b.putString("fi",myExampleHandler.folder_id.elementAt(folder).toString());
b.putString("kw", keyword);
myIntent1.putExtras(b);
startActivityForResult(
myIntent1, 0);
}
});
break;
case 3:
//same code
break;
}
}
}
}
}
private Drawable ImageOperations(Context ctx, String url,
String saveFilename) {
try {
InputStream is = (InputStream) this.fetch(url);
Drawable d = Drawable.createFromStream(is, "src");
return d;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public Object fetch(String address) throws MalformedURLException,
IOException {
URL url = new URL(address);
Object content = url.getContent();
return content;
}
Yes, you can get logcat from your device are you aware of how to connect to usb and push the file over via adb commands?
精彩评论