Android 从网站中提取图像
如何从网站中提取图像并显示在网格或列表视图中Android 从网站中提取图像,android,Android,如何从网站中提取图像并显示在网格或列表视图中 Bitmap bmImg; void downloadfile(String fileurl,ImageView img) { URL myfileurl =null; try { myfileurl= new URL(fileurl); } catch (MalformedURLException e) {
Bitmap bmImg;
void downloadfile(String fileurl,ImageView img)
{
URL myfileurl =null;
try
{
myfileurl= new URL(fileurl);
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
try
{
HttpURLConnection conn= (HttpURLConnection)myfileurl.openConnection();
conn.setDoInput(true);
conn.connect();
int length = conn.getContentLength();
int[] bitmapData =new int[length];
byte[] bitmapData2 =new byte[length];
InputStream is = conn.getInputStream();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize =
bmImg = BitmapFactory.decodeStream(is,null,options);
img.setImageBitmap(bmImg);
//dialog.dismiss();
}
catch(IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
// Toast.makeText(PhotoRating.this, "Connection Problem. Try Again.", Toast.LENGTH_SHORT).show();
}
}
尝试此操作。这说明了如何使用异步任务从远程服务器下载图像并在列表视图中显示下载的图像。下面的代码帮助您获取图像。如果您传递图像的url
public Drawable loadImageFromWebOperations(String url)
{
try
{
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGetRequest = new HttpGet(url);
HttpResponse response = client.execute(httpGetRequest);
InputStream is = response.getEntity().getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
此代码将返回可在ImageView中显示的绘图
imageView.setImageDrawable(d);
您可以向列表的每一行添加一个ImageView,并设置可绘制的。如果您想解析整个网站,我更喜欢使用,它可以与Android配合使用(已尝试),然后您可以搜索
元素以获取图像URL