Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中从URL将位图加载到ImageView_Android_Url_Bitmap_Bitmapfactory - Fatal编程技术网

在android中从URL将位图加载到ImageView

在android中从URL将位图加载到ImageView,android,url,bitmap,bitmapfactory,Android,Url,Bitmap,Bitmapfactory,我使用以下方法从url检索位图并将其传递到imageview,但imageview未被更新 public static Bitmap LoadImageFromWebOperations(String url) { Bitmap bitmap; try { InputStream is = new URL(url).openStream(); bitmap = BitmapFactory.decodeStream(is); retu

我使用以下方法从url检索位图并将其传递到imageview,但imageview未被更新

 public static Bitmap LoadImageFromWebOperations(String url) {
    Bitmap bitmap;
    try {
        InputStream is = new URL(url).openStream();
       bitmap = BitmapFactory.decodeStream(is);
        return bitmap;
    } catch (Exception e) {
         return null;
    }
呼叫-

  mov1_poster.setImageBitmap(VPmovies.LoadImageFromWebOperations(mov_details[0][7]));
//doesn't work

  Toast.makeText(this,"url is \n"+mov_details[0][7],Toast.LENGTH_LONG).show();
   // shows the url of the image successfully (just to check the url is not null)
我做错什么了吗?请提供帮助。

在处理图像时使用,它会产生奇迹,而且很简单

public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    private ImageView imageView;
    private Bitmap image;

    public DownloadImageTask(ImageView imageView) {
        this.imageView = imageView;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            image = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            image = null;
        }
        return image;
    }

    @SuppressLint("NewApi")
    protected void onPostExecute(Bitmap result) {
        if (result != null) {
            imageView.setImageBitmap(result);
        }
    }
}
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

可能是重复的谢谢,非常有用。检查日志后,我发现NetworkOnMainThreadException是原因,当应用程序尝试在其主线程上执行网络操作时,会引发此异常。对于这一点,您的答案是正确的,在一个单独的(后台)线程中运行该操作。
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);