安卓编程吧 关注:34,859贴子:335,667
  • 2回复贴,共1

预读网络图片的尺寸大小

只看楼主收藏回复

当控件加载网络图片时,可能会因为图片过大而导致加载时间过长,利用BitmapFactory.Option实现加载前对图片大小的读取。
public void LoadImage(View view)
{
new DownloadImageTask().execute(http://yourimageurl.png);
}
private class DownloadImageTask extends AsyncTask<String,Void,String>
{
protected String doInBackground(String...urls)
{
return loadImageFromNetwork(urls[0]);
}
protected void onPostExecute(String result)
{
TextView textView=(TextView)findViewById(R.id.textView);
textView.setText(result);
}
}
private String loadImageFromNetwork(String url)
{
try {
URL m_url=new URL(url);
HttpURLConnection con=(HttpURLConnection)m_url.openConnection();
InputStream in=con.getInputStream();
BitmapFactory.Options options=new BitmapFactory.Options();
options.inJustDecodeBounds=true;
BitmapFactory.decodeStream(in,null,options);
int height=options.outHeight;
int width=options.outWidth;
String s="高度是" + height + "宽度是" + width;
return s;
} catch (Exception e)
{
e.printStackTrace();
return null;
}
}


1楼2014-03-08 23:33回复
    加精,顶帖


    来自Android客户端3楼2014-06-15 13:49
    回复