public class MainActivity extends Activity {
ImageView downloadedimg;
public class ImageDownloader extends AsyncTask<String,void,Bitmap> {
@Override
protected Bitmap doInBackground(String... urls) {
try{
URL url=new URL(urls[0]);
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
connection.connect();
InputStream inputStream=connection.getInputStream();
Bitmap mybitmap= BitmapFactory.decodeStream(inputStream);
return mybitmap;
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
}
public void downloadImage(View view) {
ImageDownloader task = new ImageDownloader;
Bitmap myImage;
try {
myImage = task.execute("https://www.google.co.in/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&cad=rja&uact=8&ved=0ahUKEwiOw5HvydXOAhVKqo8KHYHfAtIQjRwIBw&url=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FBart_Simpson&psig=AFQjCNFZSwEG2tjp15Km14uuzEsmZUZ_MQ&ust=1471974330385306");
downloadedimg.setImageBitmap(myImage);
}
catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
downloadedimg=(ImageView)findViewById(R.id.imageView);
}
在线 myImage = task.execute(“某些网址在线图片地址”);
编译器显示错误
不兼容的类型
必需:android.graphics.Bitmap
发现:android.os.AsyncTask“&lt;”java.lang.String,void,android.graphics.Bitmap&gt;
java的括号前面的双重代码不是声明的一部分
@edit感谢您之前的建议我搜索并找到了.get()方法 我使用任务(“”)。get()返回位图图像和编译没有错误的代码但现在当我点击下载图像按钮时发生的事情
答案 0 :(得分:0)
AsyncTask.execute不返回下载的对象。它不可能,AsyncTask的整个想法是它的异步。任何需要使用结果的代码都应放在onPostExecute(用于UI更改)或doInBackground(用于处理数据)的末尾。