【发布时间】:2016-07-08 09:23:14
【问题描述】:
您好,我需要从 url 下载图像,然后将该图像保存到我的 android 设备,但我在从 url 获取图像时遇到问题。这是我的代码。请帮忙。我已经附加了我的数据类以及我的主要活动类。
package muneeb.muneeba;
import android.graphics.*;
import java.net.*;
import java.io.*;
public class Picture
{
public String url;
public String description;
public String category;
public Picture(String url,String desc,String category){
this.url = url;
this.description = desc;
this.category = category;
}
public Bitmap getImage(){
Bitmap image = null;
try {
InputStream in = new URL(url).openStream();
image = BitmapFactory.decodeStream(in);
} catch (Exception e) {
e.printStackTrace();
}
return image;
}
}
package muneeb.muneeba;
import android.app.Activity;
import android.os.Bundle;
import java.net.*;
import java.util.*;
import java.io.*;
import android.widget.*;
import android.view.*;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.util.Xml;
public class WebPics extends Activity
{
ArrayList<Picture> pictures;
int position = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
load();
show();
}
private void load(){
String line = "";
TextView view = (TextView) findViewById(R.id.text);
try{
URL url = new URL("https://dl.dropboxusercontent.com/u/47451150/pictures.xml");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(10000);
connection.setConnectTimeout(15000);
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.connect();
StringBuilder content = new StringBuilder();
BufferedReader reader = new BufferedReader( new InputStreamReader( connection.getInputStream() ) );
while( (line = reader.readLine()) != null ){
content.append(line);
}
line = content.toString();
parse(line);
} catch(Exception ex) {
line = ex.getMessage();
ex.printStackTrace();
}
}
private void parse(String xml){
String category = "";
pictures = new ArrayList<Picture>();
try{
XmlPullParser parser = Xml.newPullParser();
parser.setInput(new StringReader(xml));
int event = parser.getEventType();
while(event != XmlPullParser.END_DOCUMENT){
if(event == XmlPullParser.START_TAG &&
parser.getName().equals("category") ) {
category = parser.getAttributeValue(null,"name");
}
if(event == XmlPullParser.START_TAG &&
parser.getName().equals("im") ){
String url = parser.getAttributeValue(null,"url");
String description = parser.getAttributeValue(null,"description");
pictures.add(new Picture(url,description,category));
}
event = parser.next();
}
} catch(Exception ex){ }
}
private void show(){
if(pictures != null){
Picture picture = pictures.get(position);
if (picture == null) return;
ImageView image = (ImageView) findViewById(R.id.image);
image.setImageBitmap(picture.getImage());
TextView text = (TextView) findViewById(R.id.text);
text.setText("Category:" + picture.category + "\n" + picture.description);
}
}
public void buttonClick(View view){
if(view.getId() == R.id.previous ){
position--;
if (position < 0) position = pictures.size() - 1;
}
if(view.getId() == R.id.next )
{
position = (position + 1) % pictures.size();
}
show();
}
}
【问题讨论】:
-
图片未显示。
-
您是否确认您正在获取位图数据?
-
是的,我的图片数组的大小为零,所以我没有得到任何图像
-
我建议您调试参数“url”,然后将该值复制并粘贴到浏览器中并验证它是正确的图像。
-
已经完成,图片正在浏览器中打开
标签: java android image url download