Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

android - Setting image from url to ImageView with AsynchTask

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); 
            }
         }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...