从URL设置图像的ImageView与AsynchTask [英] Setting image from url to ImageView with AsynchTask

查看:273
本文介绍了从URL设置图像的ImageView与AsynchTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的网站上的图像,我想在我的ImageView设置。做我需要使用非同步任务。我做的象下面这样。但
新getThumbnail()执行(stringThumbnail); 抛出我的错误 getThumbnail不能被解析为一个类型。我是什么这里做错了吗?

 最后ImageView的缩略图=(ImageView的)findViewById(R.id.btnThumbnail);
字符串stringThumbnail =myImage.jpg这个参数;
新getThumbnail()执行(stringThumbnail);        类getThumbnail扩展的AsyncTask<弦乐,太虚,太虚> {            保护无效doInBackground(字符串...数据){
                串拇指=数据[0];
                尝试{
                  位图位图= BitmapFactory.de codeStream((InputStream的)新的URL(http://mySite.com/images/+拇指).getContent());                }赶上(MalformedURLException的E){
                  e.printStackTrace();
                }赶上(IOException异常五){
                  e.printStackTrace();
                }
                返回null;
            }            保护无效onPostExecute(位图图片){
                // TODO:检查this.exception
                // TODO:做一些配合饲料
                thumbnail.setImageBitmap(IMG);
            }
         }


解决方案

问题是在异步任务中使用它像这样

 公共类MainActivity延伸活动{    私人ImageView的mThumbnail;
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        mThumbnail =(ImageView的)findViewById(R.id.btnThumbnail);
        字符串stringThumbnail =myImage.jpg这个参数;
        新getThumbnail()执行(stringThumbnail);    }    类getThumbnail扩展的AsyncTask<弦乐,太虚,位图> {        保护位图doInBackground(字符串...数据){
            串拇指=数据[0];
            位图位图= NULL;
            尝试{
                Log.d(TEST,做背景);
                位= BitmapFactory
                        由Matchi.com提供回到codeStream((InputStream的新的网址
                                http://mySite.com/images/+拇指)
                                .getContent());            }赶上(MalformedURLException的E){
                e.printStackTrace();
            }赶上(IOException异常五){
                e.printStackTrace();
            }
            返回位图;
        }        保护无效onPostExecute(位图图片){
            Log.d(TEST,后执行);
            mThumbnail.setImageBitmap(IMG);
        }
    }}

也确保btnThumbnail是ImageView的。 preFIX BTN混乱,也宣布允许

 <使用许可权的android:NAME =android.permission.INTERNET对/>

在manifest.xml

I have an image on my website, that I want to set on my ImageView. Do to that I needed to use an asynch task. I'm doing it like below. But new getThumbnail().execute(stringThumbnail); is throwing me the error getThumbnail cannot be resolved to a type. What am I doing wrong in here?

final ImageView thumbnail = (ImageView) findViewById(R.id.btnThumbnail);
String stringThumbnail = "myImage.jpg";
new getThumbnail().execute(stringThumbnail);        

        class getThumbnail extends AsyncTask<String, Void, Void> {

            protected Void doInBackground(String... data) {
                String thumb = data[0];
                try {
                  Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL("http://mySite.com/images/" + thumb).getContent());

                } catch (MalformedURLException e) {
                  e.printStackTrace();
                } catch (IOException e) {
                  e.printStackTrace();
                }
                return null;
            }

            protected void onPostExecute(Bitmap img) {
                // TODO: check this.exception 
                // TODO: do something with the feed
                thumbnail.setImageBitmap(img); 
            }
         }

解决方案

The problem is in async task use it like this

public class MainActivity extends Activity {

    private ImageView mThumbnail;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mThumbnail = (ImageView) findViewById(R.id.btnThumbnail);
        String stringThumbnail = "myImage.jpg";
        new getThumbnail().execute(stringThumbnail);

    }

    class getThumbnail extends AsyncTask<String, Void, Bitmap> {

        protected Bitmap doInBackground(String... data) {
            String thumb = data[0];
            Bitmap bitmap = null;
            try {
                Log.d("TEST", "do in background");
                bitmap = BitmapFactory
                        .decodeStream((InputStream) new URL(
                                "http://mySite.com/images/" + thumb)
                                .getContent());

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return bitmap;
        }

        protected void onPostExecute(Bitmap img) {
            Log.d("TEST", "post execute");
            mThumbnail.setImageBitmap(img);
        }
    }

}

also make sure that btnThumbnail is a imageView. Prefix btn is confusing and also declare the permission

<uses-permission android:name="android.permission.INTERNET"/>

in the manifest.xml

这篇关于从URL设置图像的ImageView与AsynchTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆