How to increase/decrease the resolution of a downloaded image - android

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()

Related

ImageView emptied on subsequent activity launches

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

Display image from String Imageurl [duplicate]

This question already has answers here:
Load image from url
(17 answers)
Closed 7 years ago.
I have image url in string as
String imgurl=days.getString("icon_url");
getting this url jsonarray which is saved in shared preference
how to show this string image url into ImageView?
Any help???
You can either use UniversalImageLoader or picasso Library to load image in imageview from url.
Add internet permission in your AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
And to download image using AsyncTask use following code:
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 to use above code to load image from url in imageview use following code:
new DownloadImageTask(mImageView).execute(imgurl);

load .png from url's in the existing imageview

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 ;)

Change background image for TextView at runtime

I'm developing my first app and I need to display a larger image of a thumbnail that I click on to be set as the background for a text in a different fragment.
I'm trying to change the background image for a textview using
textView.setBackground(getResources().getDrawable(R.drawable.user_placeholder_image));
In order to minimize the bandwidth usage, I was hoping to download the backdrop image only when the thumbnail is clicked. In other words, is there a way to store resources to the drawable folder at runtime? If not, then is there any other alternative?
Thanks for the help!
You can not store them in your resources at runtime. However you can always store them as bitmaps. Take a look at my example.
//OnClick
new DownloadImageTask((ImageView) findViewById(R.id.imageView1))
.execute("your url");
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);
}
}
Instead of setting the image to your ImageView, you could modify the code to store it.
You can not store them in your resources at runtime. But you can store them in SD card or as a bitmap. Later you can load the bitmap or image using any techniques.

android setting layout background to iamgeview

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);

Categories

Resources