Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/180.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 使用线程加载图像formURL_Android - Fatal编程技术网

Android 使用线程加载图像formURL

Android 使用线程加载图像formURL,android,Android,如何在android中使用线程从http加载图像? 我想在等待加载图像时显示布局 现在,我使用这段代码从http显示我的img,在显示结果之前,它会显示一个黑色的空白屏幕几次: private Drawable ImageOperations(Context ctx, String url, String saveFilename) { try { InputStream is = (InputStream) this.fetch(url)

如何在android中使用
线程
从http加载图像?
我想在等待加载图像时显示布局

现在,我使用这段代码从http显示我的img,在显示结果之前,它会显示一个黑色的空白屏幕几次:

        private Drawable ImageOperations(Context ctx, String url, String saveFilename) {
        try {
            InputStream is = (InputStream) this.fetch(url);
            Drawable d = Drawable.createFromStream(is, "src");
            return d;
        } catch (MalformedURLException e) {
            return null;
        } catch (IOException e) {
            return null;
        }
    }

    public Object fetch(String address) throws MalformedURLException,IOException {
        URL url = new URL(address);
        Object content = url.getContent();
        return content;
    }

有人能帮我吗?

您应该将ImageView对象传递给您的方法。方法作为线程运行。

请解决您的问题。即使你的英语不是最好的,你也可以稍微修改一下你的语言(拼写错误、空格缺失、句子开头用大写字母、结尾用“.”等等)。那么
是什么意思?这是不是暗示你在问问题?我相信这没必要……谢谢你的建议,是的,我知道我的建议要少很多。不断学习
Use this for load images from http

ImageView img_item;
String image_url = "here url"
    new Thread(new Runnable() 
    {
    public void run() 
        {
        try 
        {
            final Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(image_url).getContent());
            img_item.post(new Runnable()
            {
              public void run() 
              {
                if(bitmap !=null)
                {
                   img_item.setImageBitmap(bitmap);
                }   
              }
            }); 
        } catch (Exception e)
        {
        // TODO: handle exception
        }
    }
  }).start();