0

当单击按钮时,我正在使用一种方法从 URL 获取图像,但是它花费了太多时间,大约 4-5 秒才能使用 3G 而不是 wi-fi 显示大小仅为 6 kb 的图像。

我的方法是这样的;

public static Bitmap getBitmapFromURL(String src) {
    try {
        Log.e("src",src);
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        Log.e("Bitmap","returned");
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        Log.e("Exception",e.getMessage());
        return null;
    }
}

接着;

    Bitmap bimage =  getBitmapFromURL("http://www.replikler.net/test2.jpg");
    ImageView image = (ImageView)findViewById(R.id.movie_image);
    image.setImageBitmap(bimage);

您知道从 url 获取图像的另一种方法(更快)吗?

4

1 回答 1

2
imageView.setImageDrawable(Drawable.createFromStream((InputStream)new URL(url).getContent(), "src"));
于 2012-07-11T20:56:30.713 回答