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 ;)
Related
there is an IP camera and I want to download a snapshot image.
this is the url : http://test:11223344!!#85.105.152.138:69/ISAPI/Streaming/channels/2/picture
as you can see; port is 69 not 80
this is my code:
ImageView img = findViewById(R.id.imgSnap);
new DownloadImageTask(img).execute("http://test:11223344!!#85.105.152.138:69/ISAPI/Streaming/channels/2/picture");
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 webImage = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
webImage = BitmapFactory.decodeStream(in);
} catch (Exception e) {
e.printStackTrace();
}
return webImage;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
I got FileNotFoundException. but I think the reason is port 69.
How can I get the image?
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
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);
I want to know is there a way to increase/decrease the resolution of a downloaded image? I am getting the image from the web using an URL. But the image is displayed with really low resolution so is it possible to increase it?
I am getting the image using the following code:
class GetImage extends AsyncTask<String, Void, Bitmap>{
ImageView bmImage;
public GetImage (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)
{
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result)
{
bmImage.setImageBitmap(result);
}
}
You can't increase the resolution of a photo you've downloaded. That is CSI territory. However, you can scale it up, so that it covers a larger area, if that's what you're after. (the image is still blurred)
The way you do that is usually using something like Bitmap.createScaledBitmap()
I'm trying to display an image from a url in a "InfoWindowAdapter"
,I have the following code, but does not show me the image
....
mMap = getMap();
mMap.setInfoWindowAdapter(new InfoWindowAdapter() {
...
#Override
public View getInfoContents(Marker marker) {
View v = getActivity().getLayoutInflater().inflate(
R.layout.info_window_layout, null);
String image_url = "http://api.androidhive.info/images/sample.jpg";
new DownloadImageTask(imgEquipo).execute(image_url);
return v;
}
});
call to AsyncTask to get image
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);
} }
}
tranks for your help.
It won't work because the view is returned before you get the image, and they are not synchronized.
Take a look at this approach:
I my using google mapV2 and i m downloading image from google place api and want to display in the popup
The trick is to update the InfoWindow after you get the image.