Big loss of Image quality when decode from InputStream - android

I'm trying to retrieve an image from a URL and show it on an ImageView. The task seems quite easy and i can show the images in the ImageView but the quality is really bad.
Here's what I did so far:
Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inDither = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in,null,options);
The ImageView properties:
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
.../>
I also tried to directly use the decodeStream Without Options but the result is still the same.

The code is okay, you could also consider using dithering for better result. If the images you download are greatly smaller in pixels than the ImageView the result looks bad.
Solution is to have better quality images to download or change the ImageView to be smaller. You could adjust the ImageView width and height programmatically after you have decoded the Bitmap. If the Bitmap pixel size is lower than the ImageView, make the ImageView smaller.

Related

Glide loads wrong image size

I use simple:
Glide.with(this).load(logoPath).apply(RequestOptions.circleCropTransform()).into(businessIcon)
Size of logoPath on server is 48*48, when I put wrap_content attribute on ImageView dimensions, final image is 1280*1280.
What am I doing wrong?
There seems to be some misunderstanding in what Glide gets from the server.
It is just regular file, and it will be fetched as-is, it is extremely unlikely somebody could get higher resolution image by downloading.
(If you really can, you could make a lot of money )
So the actual problem is how this image file is displayed.
You could use ImageView attribute android:scaleType="center" to force no scaling to be performed.
You could also download the Image, convert it to Bitmap and get its attributes. Something simmilar to this.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(logoPath, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
And then use these values for fixed dimensions in Glides method .override()

Calling setImageBitmap not always work for ImageView

In my application the user selects a bitmap and then i use the following code:
BitmapFactory.Options op= new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(path,op);
imageView.setImageBitmap(bitmap);
Unfortunately, sometimes ImageView does not display anything
What can i do?
Sometimes bitmap is too large. Thats why imageview turns blank! I think that here you can find what you need: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
I found this problem for Android 8 and low. There is no difference use method setImageBitmap or setImageUri : ImageView will be blank sometimes. There is no any errors and you can get Bitmap from ImageView.
Solution.
Use width and height of Bitmap for ImageView no more then double screen size, better is to fit to the screen size.

Images scales defferently in setImageDrawable() and setImageBitmap()?

I am new in android development. I have an issue regarding the image scaling in my app. When i set images using setImageDrawable() images look proper like in screenshot:
But if i set image using setImageBitmap() method it does not look proper. Some black color occurs around it like in screenshot bellow:
I have tried it in multiple applications and i observed that in some applications the image size is becoming smaller than the size ofter setting image through setImageDrawable() or setImageResource().
the reason for setting images through setImageBitmap() is that i want to set images to the imageviews which are kept in assets folder and not in drawable.
Following is the code i used to create a bitmap and set it to imageview:
BitmapFactory.Options opts = new Options();
opts.inPurgeable = true;
try
{
Bitmap preview_bitmap = BitmapFactory.decodeStream(assetManager.open(drawableFolder+"/"+tmpArr[0]),null,opts);
view.setImageBitmap(preview_bitmap);
}
catch (Exception e)
{
LNLog.writeTOErrorToLog(e.getMessage());
}
So, please can anyone tell me where i'm i going wrong or missing something to set.
I tried to set Options but did not worked. I want to set images as in first screenshot that is how they looks by using setImageDrawable().
Thanx in advance.
Note: in screenshots images are referent in text on it only.IF i set hindi images i.e. the images on which text is in hindi language using setImageDrawable() then they looks proper as in like in screen shot one.So , no issue of image files.
You can get rid of the black borders by using
android:adjustViewBounds="true" in xml
or
imageView.setAdjustViewBounds(true) in Java.
EDIT:
Then try using drawable only
Drawable d = Drawable.createFromStream(assetManager.open(filePath), null);
view.setImageDrawable(d)
This worked for me :
try
{
view.setAdjustViewBounds(true);
int height = view.getDrawable().getIntrinsicHeight();
int width = view.getDrawable().getIntrinsicWidth();
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPurgeable = true;
opts.inScaled = true;
Bitmap preview_bitmap = BitmapFactory.decodeStream(assetManager.open("drawable-mdpi/FAQ_hi_in.png",null,opts);
Bitmap bmp = Bitmap.createScaledBitmap(preview_bitmap, width, height, true);
view.setImageBitmap(bmp);
}
In this code I have taken the height and width of actual image and then scaled my bitmap to that height and width. Then I converted this scaled bitmap to drawable and set it into my ImageView, thats it. It resolved my problem of black border around image.

Resizing a bitmap download from url to fit imageview space: lost of quality

I know there are a lot of questions around this subject, but I cannot find something which perfectly fits my problem so I open this question in order to, at least, gather here EVERY aspect of the problem.
Let's start! I have my imageview and this imageview has to have fixed dimensions:
<ImageView
android:id="#+id/my_image"
android:layout_width="250dp"
android:layout_height="170dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:contentDescription="#string/cell_item_image"
android:scaleType="fitXY" />
my bitmap comes from an url and is in the .jpg format, so my code is:
bitmap = BitmapFactory.decodeStream(conn.getInputStream());
its dimensions are 435x312 pixels, bigger than my screen density so scaling it should not lead to a loss of quality...how come I get an obfuscated image?
Is there a way to rescale bitmap and preseve quality?
Maybe it's that fitXY in the scale type? But what's the difference between scaling in code or in layout? I see many does that in code but, trying, I get exactly the same result
EDIT: read over the .jpg part
Try:
Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap source = BitmapFactory.decodeResource(a.getResources(), path, options);
If this doesn't work, try displaying the image without scaling (this way you can see what causes the blur, the loading of the image or the displaying/scaling)

android - query CAMERA folder images with width and height

is it possible to query for images from CAMERA folder with height and width?
I am querying it with only URL right now. I want to avoid loading image into Bitmap and I need to have width and height of it beforehand so I can figure out proper scaling. Right now some of the photos I load can be really large size and some can be tiny. If I set same scaling on them they don't look right. Small images get resized for no reason. Knowing width and height would solve this. Also I cannot sample load a bitmap because that causes memory issue when images are really large, like those taken with 3 megapixel camera.
Here is how I load them:
https://stackoverflow.com/questions/5710036/android-camera-folder-images-not-all-show-up
Thank you.
You should do decoding for the image without getting its whole data.
the reason it doesn't exist on the DB is that the user can always modify the images to have different dimensions than the original one.
in order to get just the minimal information of the bitmap, use something like:
public static BitmapFactory.Options getBitmapOptions(final String filePath) {
final BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, bitmapOptions);
return bitmapOptions;
}

Categories

Resources