我正在尝试设置我的ImageView。我有图像的url并尝试将其设置为位图,然后将此位图设置为我的ImageView。但是,onPostExecute的位图结果,来自download_Image函数的null。这意味着' BitmapFactory.decodeStream(是); '正在返回null。
this.ImageView1 = (ImageView) infoWindow.findViewById(R.id.ImageView1);
ImageView1.setTag(URL);
new AsyncTask<ImageView, Void, Bitmap>(){
ImageView imageView = null;
@Override
protected Bitmap doInBackground(ImageView... imageViews) {
final Bitmap[] b = new Bitmap[1];
this.imageView = imageViews[0];
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
b[0] =download_Image((String) imageView.getTag());
}
});
return b[0];
}
@Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}
public Bitmap download_Image(String url) {
//---------------------------------------------------
URL newurl = null;
try {
newurl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
Bitmap mIcon_val = null;
try {
URLConnection urlConnection=newurl.openConnection();
InputStream is=urlConnection.getInputStream();
mIcon_val=BitmapFactory.decodeStream(is);
} catch (Exception e) {
e.printStackTrace();
}
return mIcon_val;
//---------------------------------------------------
}
}.execute(ImageView1);
我该如何处理这个问题?
答案 0 :(得分:1)
如果有任何问题,请使用此代码并发表评论`public class MainActivity扩展AppCompatActivity {
private ImageView imageView;
private String imageUrl ="image url";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView)findViewById(R.id.imageview);
new AsyncTask<ImageView, Void, Bitmap>(){
ImageView imageView = null;
@Override
protected Bitmap doInBackground(ImageView... imageViews) {
final Bitmap[] b = new Bitmap[1];
this.imageView = imageViews[0];
b[0] =download_Image(imageUrl);
return b[0];
}
@Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}
public Bitmap download_Image(String url) {
//---------------------------------------------------
URL newurl = null;
try {
newurl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
Bitmap mIcon_val = null;
try {
URLConnection urlConnection=newurl.openConnection();
InputStream is=urlConnection.getInputStream();
mIcon_val= BitmapFactory.decodeStream(is);
} catch (Exception e) {
e.printStackTrace();
}
return mIcon_val;
//---------------------------------------------------
}
}.execute(imageView);}}`
答案 1 :(得分:0)
将此内容写入您的gradle依赖项
dependencies {
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:support-v4:19.1.0'
}
在你的活动中
Glide.with(activity.this).load(your image url).into(imageView);