如何从android中的url加载图像

时间:2014-07-28 01:54:37

标签: android url bitmap imageview

我有很多图像,我使用位图和URL来显示图像,但它不起作用。这是我的代码:

主要活动:

public class MainActivityB extends Activity {
    String[] tap;
    ViewPager viewPager;
    PagerAdapter adapter;
    String value;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity_b);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            value = extras.getString("str");
        }
        if (value.equals("0") == true) {
            tap = new String[] {
                    "http://image10.bizrate-images.com/resize?sq=60&uid=2216744464",
        "http://i.imgur.com/CH7a9Bd.jpg" };
            viewPager = (ViewPager) findViewById(R.id.pager);
            adapter = new ViewPagerAdapter(MainActivityB.this, tap);
            viewPager.setAdapter(adapter);
        }
    }
}

Class ViewPagerAdapter:

public class ViewPagerAdapter extends PagerAdapter {
    Context context;
    String[] tap;
    LayoutInflate inflater;

    public ViewPagerAdapter(Context context, String[] tap) {
        this.context = context;
        this.tap = tap;
    }
    @Override
    public int getCount() {
        return tap.length;
    }
    @Override
    public boolean isViewFromObject(View v, Object object) {
        return v == ((RelativeLayout) object);
    }
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imgflag;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.viewpager_item, container,
                false);
        imgflag = (ImageView) itemView.findViewById(R.id.imageView);
        try {
            URL url = new URL(tap[position]);
            Bitmap bmp = BitmapFactory.decodeStream(url.openConnection()
                    .getInputStream());
            imgflag.setImageBitmap(bmp);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        ((ViewPager) container).addView(itemView);
        return itemView;
    }
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // Remove viewpager_item.xml from ViewPager
        ((ViewPager) container).removeView((RelativeLayout) object);
    }

}

1 个答案:

答案 0 :(得分:0)

final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inSampleSize = calculateInSampleSize(options, 2048, 2048);
options.inJustDecodeBounds = false;

Bitmap bitmap = loadBitmap(url, options);
imageview.setImageBitmap(bitmap );

public static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth, int reqHeight) {
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 2;
    if (height > reqHeight || width > reqWidth) {
         final int heightRatio = Math.round((float) height/ (float) reqHeight);
         final int widthRatio = Math.round((float) width / (float) reqWidth);
         inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }
    return inSampleSize;
}

public static Bitmap loadBitmap(String URL, BitmapFactory.Options options) {
    Bitmap bitmap = null;
    InputStream in = null;
    try  {
        in = OpenHttpConnection(URL);
        bitmap = BitmapFactory.decodeStream(in, null, options);
        in.close();
    } catch (IOException e1) {
    }
    return bitmap;
}

private static InputStream OpenHttpConnection(String strURL)throws IOException {
    InputStream inputStream = null;
    URL url = new URL(strURL);
    URLConnection conn = url.openConnection();

    try {
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setRequestMethod("GET");
        httpConn.connect();

        if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            inputStream = httpConn.getInputStream();
        }
    } catch (Exception ex) {
    }
    return inputStream;
 }

请尝试。谢谢。