-1

我的网站上有一张图片,我想在我的 ImageView 上设置它。为此,我需要使用异步任务。我正在像下面那样做。但是 new getThumbnail().execute(stringThumbnail);正在向我抛出错误getThumbnail cannot be resolved to a type。我在这里做错了什么?

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); 
            }
         }
4

1 回答 1

2

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

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);
        }
    }

}

还要确保 btnThumbnail 是一个 imageView。前缀 btn 令人困惑并且还声明了权限

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

在 manifest.xml

于 2013-06-14T10:15:58.067 回答