I'm using this AsyncTask to load an image from a URL and populate an ImageView with that image:
new DownloadImageTask((ImageView) findViewById(R.id.headerIV)).execute(imageURL);
and
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
I noticed that whenever I background my app and relaunch the activity, the ImageViews are emptied. Various checks on the ImageViews show that they are null in onCreate(), and so if I want to show images again, I have to call the AsyncTask again, which eventually leads to out-of-memory exceptions. So, I'm wondering: Does the activity recycle/empty my ImageViews for some reasons on onPause() based on that code block I'm using?
Try this !!!!
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
Bitmap mIcon11;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
try {
mIcon11 = BitmapFactory.decodeStream((InputStream) new URL(urls[0]).getContent());
} catch (Exception e) {
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
if (result != null) {
bmImage.setImageBitmap(result);
} else {
Toast.makeText(TestActivity.this, "Image Does Not exist or Network Error", Toast.LENGTH_SHORT).show();
}
}
}
Whenever your app comes to foreground the onResume() is called and the views are generated again, so that can be the reason for the null values.
You can use onResume() for the same. (For more info check out Activity life cycle)
Also as suggested by others, you can use Picasso or Universal Image Loader or other libraries for ease.
To start with you can refer below links:
https://www.simplifiedcoding.net/picasso-android-tutorial-picasso-image-loader-library/
http://javatechig.com/android/how-to-use-picasso-library-in-android
http://javatechig.com/android/universal-image-loader-library-in-android
Related
i seen this, and made how they say.
my app loads and shows .jpg, but don't show .png. He shows empty screen. (tested on versions 4.1 and 4.2)
if (isOnline()) {
new DownloadImageTask((ImageView) findViewById(R.id.imageViewSA))
.execute(img);
} else {
tv_info.setText("Включите интернет для загрузки");
}
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
you can use universal image loader library instead, so easy to integrate in your code, you can display images from URL to imageView like this
imageLoader.displayImage(url, imageView);
here's a good example of using UIL
and the library
good luck ;)
how to get tweet post with image and display it on list view?
i'm only getting text in retrieving tweet post on twitter.
this is what i wanted to do http://tinypic.com/r/2ztee84/8
and this is what I've done on my application http://tinypic.com/r/zkmknm/8
thanks in advance.
Well, it would help if you provided us with more code. But... Try to "parse" the image link from the tweet, and download it.
You could try this lib, which will download images asynchronously. Or you could try a native approach. Something like:
public class LoginActivity extends Activity implements OnClickListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
this.findViewById(R.id.userinfo_submit).setOnClickListener(this);
// Verify Code
LinearLayout view = (LinearLayout) findViewById(R.id.txt_verify_code);
view.addView(new VerifyCodeView(this));
// show The Image
new DownloadImageTask((ImageView) findViewById(R.id.imageView1))
.execute(“http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png”);
}
public void onClick(View v) {
startActivity(new Intent(this, IndexActivity.class));
finish();
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String… urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e(“Error”, e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
}
This code was taken from here.
And there's this related question, which might be an interesting read for you.
Here is my main activity-after OnCreate code is :
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
and I have one ImageView so I can show the url-picture in this ImageView with below code:
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
new DownloadImageTask(imageView).execute("http://url.com/background1.jpg");
what i need exactly, i need to add three image in this ImageView and in every 3 seconds, picture automatically changed to another link.
Picture 2 and after three second, Picture 3
How we can do this?
Thanks
Use this to change image after 3 sec
call this in your OnCreate()
handler=new Handler();
handler.postDelayed(myRunnable, 3000);
define it outside the OnCreate()
private Runnable myRunnable=new Runnable() {
#Override
public void run() {
// change your url to imgeview here some thing like
new DownloadImageTask(imageView).execute("http://url.com/picutre2.jpg");
handler.postDelayed(this, 3000);
}
}
Note : my suggestions is to cache the downloaded image so to avoid network usage
I have an image view in my Android app, where I have to set a simple image from url. I tried the below code, but it doesn't set the image from url.
try {
URL url = new URL("https://drive.google.com/...");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream stream = connection.getInputStream();
Bitmap teamBmpImage = BitmapFactory.decodeStream(stream);
teamImgView.setImageBitmap(teamBmpImage);
}
catch (Exception e) {
}
Could someone guide me to achieve this please?
UPDATED CODE: Which gives Nullpointer exception
public class AboutActivity extends ActionBarActivity {
ImageView teamImgView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
teamImgView = (ImageView) this.findViewById(R.id.teamImageView);
new DownloadImageTask(teamImgView).execute("http://docs.oracle.com/javase/tutorial/2d/images/examples/strawberry.jpg");
}
class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
#Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
//pd.show();
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
#Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
//pd.dismiss();
bmImage.setImageBitmap(result);
}
}
}
I guess you are executing your code on the MainThread, which leads to a NetworkOnMainThreadException in android. Try to execute your code asynchronous like in the example below
new AsyncTask<String, Integer, Bitmap>() {
#Override
protected Bitmap doInBackground(String... params) {
try {
URL url = new URL(params[0]);
return BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Bitmap bm) {
ImageView teamImgView = (ImageView) findViewById(R.id.teamImageView);
teamImgView.setImageBitmap(bm);
}
}.execute("https://drive.google.com/uc?....");
You can use Picasso library and here is a detailed tutorial on how to do this.
This is very simple example usage
Picasso.with(activityContext)
.load("https://drive.google.com/uc?....")
.placeholder(R.drawable.image_name)
.into(imageView);
As bojan says you can use Picasso library wich handles many common pitfalls of image loading on Android.
Picasso.with(context).load("http://myurl/myImage.png").into(imageView);
Picasso
Anyway, check out this threat too :)
How to load an ImageView by URL in Android?
Try following this link:
http://www.tutorialsbuzz.com/2014/11/android-volley-url-imageview.html
This will help you to load your image using Volley library which will do all the networking stuff on networking thread and set your image on main UI thread. It has also the LRUCache part which you can skip if you want.
I have a class that extends AsyncTask to get image from a URL:
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
I can load it to every ImageView I want, like that:
new DownloadImageTask(mRecipeImage).execute(ImageURL);
I an getting the image while the app is running (dynamically).
I want to set a Relative Layout's background to the image that I am getting from the web.
any ideas?
Please visit this link:
UniversalImageLoader
may it is help full to you.
It is better if you use an ImageLoader library like picasso, volley for downloading and setting Image.
code snippet for using picasso
Picasso.with(context).load("ur_image_url").into(bmImage);
code snippet for using Volley
String url = "http://i.imgur.com/7spzG.png";
// Retrieves an image specified by the URL, displays it in the UI.
ImageRequest request = new ImageRequest(url,
new Response.Listener() {
#Override
public void onResponse(Bitmap bitmap) {
bmImage.setImageBitmap(bitmap);
}
}, 0, 0, null,
new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
bmImage.setImageResource(R.drawable.image_load_error);
}
});
// Access the RequestQueue through your singleton class.
MySingleton.getInstance(this).addToRequestQueue(request);