Reputation: 3840
I have image with 543*6423 resolution, I want to display it in all devices. It is displaying in few of android phones which allows the high resolution. I tried with
android:hardwareAccelerated="false"
This is my java code for android client
File storagePath =Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS+
"abc.png");
InputStream is = this.getContentResolver().openInputStream(Uri.fromFile(storagePath));
Bitmap b = BitmapFactory.decodeStream(is, null, null);
is.close();
image.setImageBitmap(b);
This worked in my mobile (sony xperia) but few other phones are not displaying it. Please help me how can I display this image independent of screen resolution.
Thanks, Aman
Upvotes: 11
Views: 14861
Reputation: 5659
Instead of spending hours upon hours trying to write and debug all this downsampling code manually, why not try using Picasso? It is a popular image loading library and was made for dealing with bitmaps of all types and/or sizes. I have used this single line of code to remove my "bitmap too large...." problem:
Picasso.with(image.getContext()).load(storagePath).fit().centerCrop().into(image);
Upvotes: 0
Reputation: 1475
The solution in the blog is wrong by a power of 2...
Here is the solution:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// Set height and width in options, does not return an image and no resource taken
BitmapFactory.decodeStream(imagefile, null, options);
int pow = 0;
while (options.outHeight >> pow > reqHeight || options.outWidth >> pow > reqWidth)
pow += 1;
options.inSampleSize = 1 << pow;
options.inJustDecodeBounds = false;
image = BitmapFactory.decodeStream(imagefile, null, options);
The image will be scaled down at the size of reqHeight and reqWidth. As I understand inSampleSize only take in a power of 2 values.
Upvotes: 0
Reputation: 1346
Well, maybe I am too late to help you but this can hopefully help others. I've recently developed an open source library that can handle large image loading. The source code and samples are available at https://github.com/diegocarloslima/ByakuGallery
Upvotes: 0
Reputation: 146
Your image is probably too large to be displayed on most devices. So you have to load a scaled down version of the image. Look at Loading Large Bitmaps Efficiently to see how to do this and calculate an appropriate sample size.
If you need the display width / height (in case of a full screen image) you can use getResources().getDisplayMetrics()
.
Upvotes: 4