Reputation: 25
I am trying to set my ImageView.I have the url of the image and trying to make it bitmap and after set this bitmap to my ImageView.However, Bitmap result, argument of onPostExecute, is coming as null from download_Image function. It means ' BitmapFactory.decodeStream(is); ' is returning 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);
How can I deal with that problem?
Upvotes: 1
Views: 620
Reputation: 321
Use this code and comment if any problem`public class MainActivity extends 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);}}`
Upvotes: 1
Reputation: 1376
Write this in your gradle dependencies
dependencies {
compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'com.android.support:support-v4:19.1.0'
}
In your activtiy
Glide.with(activity.this).load(your image url).into(imageView);
Upvotes: 0