Android 使用AsynchTask将图像从url设置为ImageView
我的网站上有一个图像,我想在ImageView上设置它。为此,我需要使用异步任务。我是这样做的。但是Android 使用AsynchTask将图像从url设置为ImageView,android,android-asynctask,android-imageview,android-image,Android,Android Asynctask,Android Imageview,Android Image,我的网站上有一个图像,我想在ImageView上设置它。为此,我需要使用异步任务。我是这样做的。但是 new getThumbnail().execute(stringThumbnail)向我抛出错误getThumbnail无法解析为类型。我在这里做错了什么 final ImageView thumbnail = (ImageView) findViewById(R.id.btnThumbnail); String stringThumbnail = "myImage.jpg"; new get
new getThumbnail().execute(stringThumbnail)
向我抛出错误getThumbnail无法解析为类型
。我在这里做错了什么
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);
}
}
final ImageView缩略图=(ImageView)findViewById(R.id.btnThumbnail);
String stringThumbnail=“myImage.jpg”;
新建getThumbnail().execute(stringThumbnail);
类getThumbnail扩展异步任务{
受保护的Void doInBackground(字符串…数据){
字符串thumb=数据[0];
试一试{
位图位图=BitmapFactory.decodeStream((InputStream)新URL(“http://mySite.com/images/“+thumb.getContent());
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回null;
}
受保护的void onPostExecute(位图img){
//TODO:检查此项。异常
//TODO:对提要做些什么
缩略图.setImageBitmap(img);
}
}
问题出在异步任务中,请像这样使用它
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);
}
}
}
公共类MainActivity扩展活动{
私有ImageView缩略图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mThumbnail=(ImageView)findViewById(R.id.btnThumbnail);
String stringThumbnail=“myImage.jpg”;
新建getThumbnail().execute(stringThumbnail);
}
类getThumbnail扩展异步任务{
受保护位图doInBackground(字符串…数据){
字符串thumb=数据[0];
位图=空;
试一试{
Log.d(“测试”,“背景中的do”);
位图=位图工厂
.decodeStream((InputStream)新URL(
"http://mySite.com/images/“+拇指)
.getContent());
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
返回位图;
}
受保护的void onPostExecute(位图img){
Log.d(“测试”、“执行后”);
mThumbnail.setImageBitmap(img);
}
}
}
还要确保btnThumbnail是一个imageView。前缀btn容易混淆,并且还声明了权限
<uses-permission android:name="android.permission.INTERNET"/>
在manifest.xml中,您的代码片段不可理解。而且,如果您从doInBackground返回“null”,那么如何在onPostExecute中获得位图。请看Anirudha Agasher的回答。解释得很准确。