Obviously this is an expensive/time-consuming operation. Any way to improve this?
Bitmap bm = BitmapFactory.decodeStream((InputStream) new URL(
someUrl).getContent());
I'm guessing there's really no way to avoid this relatively intense operation, but wanted to see if anyone had any tweaks they could recommend (aside from caching the actual bitmap, which for whatever reasons simply isn't relevant here)
If you don't need the full resolution you can have it only read ever nth pixel, where n is a power of 2. You do this by setting inSampleSize on an Options object which you pass to BitmapFactory.decodeFile. You can find the sample size by just reading the meta-data from the file in a first pass by setting inJustDecodeBounds on the Options object. Aside from that - no, I don't think there's an easy way to make to go any faster than it already does.
Edit, example:
Options opts = new Options();
// Get bitmap dimensions before reading...
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, opts);
int width = opts.outWidth;
int height = opts.outHeight;
int largerSide = Math.max(width, height);
opts.inJustDecodeBounds = false; // This time it's for real!
int sampleSize = ??; // Calculate your sampleSize here
opts.inSampleSize = sampleSize;
Bitmap bmp = BitmapFactory.decodeFile(path, opts);
Related
I need to get the height and width of an Image which is stored on my Android Device. I already know how to get the file(image), I just need some code how to get the height and width from that file.
Of course, you can.
you can try this:
Bitmap bitmap = BitmapFactory.decodeFile("your image path");
int width = bitmap.getWidth();
int height = bitmap.getHeight();
note: You should have permission to access the file.
If you only need the dimensions of the image but not the decoded image itself, you can use BitmapFactory.Options with inJustDecodeBounds = true to do so:
BitmapFactory.Options decodeOptions = new BitmapFactory.Options();
decodeOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, decodeOptions);
//decodeOptions.outWidth and decodeOptions.outHeight now contain the image dimensions
This way the decode function will return null but set the out variables of the options object. This saves both CPU and memory which can be advantageous if you are processing lots of images at once.
I have taken and modified some code used to get a high quality anti-aliased small version of a larger (500x500) bitmap image. Apparently using a Matrix produces higher quality than employing createScaledBitmap(). The results were not very impressive.. and I suspected that perhaps I had made a mistake, in particularl I was unsure as to whether the options thing was actually being employed. So I changed the inSampleSize = 1; to inSampleSize = 50; expecting to see a dramatic drop in quality, but there was no change. So now I suspect options is being ignored. Can this code be rescued?
I was hoping to perhaps find some version of createBitmap which took bith a bitmap and an options argument, but could find none.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferQualityOverSpeed = true;
options.inJustDecodeBounds = false;
options.inDither = false;
options.inSampleSize = 1;
options.inScaled = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bmpSource = BitmapFactory.decodeResource(getResources(), R.drawable.bigicon);
Matrix matrix = new Matrix();
matrix.postScale(.1f,.1f);
Bitmap scaledBitmap = Bitmap.createBitmap(bmpSource, 0, 0, bmpSource.getWidth(), bmpSource.getHeight(), matrix, true);
well you should actually provide it as parameter. Change
Bitmap bmpSource = BitmapFactory.decodeResource(getResources(), R.drawable.bigicon);
with
Bitmap bmpSource = BitmapFactory.decodeResource(getResources(), R.drawable.bigicon, options);
here the documentation
Take a look of this Creating a scaled bitmap with createScaledBitmap in Android
This too: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
I'm trying to implement an image gallery in android.
The code based on http://www.mobisoftinfotech.com/blog/android/android-gallery-widget-example-and-tutorial/ and i've changed some details.
I'm using WeakReference and it seems, that when i've too many bitmaps, the garbage collector destroys my weakreferences. How can i handle this?
I get my bitmaps via this function:
public static WeakReference<Bitmap> getBitmap(String imageName, int width,
int height) {
String pathToImage = getPathToImage(imageName);
Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(pathToImage, options);
/*
* Calculate inSampleSize
*/
options.inSampleSize = calculateInSampleSize(options, width, height);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
WeakReference<Bitmap> scaledBitmap = new WeakReference<Bitmap>(
BitmapFactory.decodeFile(pathToImage, options));
return scaledBitmap;
}
And i've taken the Solution 320x480, so i think it is not this big...
When the gallery has more than 3 pictures, some of them aren't displayed.
Is the gallery-tutorial not that good? Are there other ways to implement this?
Thank you!
Instead of using soft references, you should take a look at the lrucache class (it became available in honeycomb, but is part of the android-support library.
You can read more about it here : http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html
It's pretty convenient : use this and you won't have to handle the memory yourself with weak references :-)
i am loading some bitmap from the gallery using the following code:
bitmap = (BitmapFactory.decodeFile(picturePath)).copy(Bitmap.Config.ARGB_8888, true);
bitmap = Bitmap.createScaledBitmap(bitmap, screenWidth, screenHeight, true);
bitmapCanvas = new Canvas(bitmap);
invalidate(); // refresh the screen
Question:
It seems that it takes so long time to load an image by first decode fully and copy, and then making scaling to fit for the screen width and height. It really actually does not need to load the pic with full density because I would not let the user to enlarge the imported image anyway.
In that way, are there any method to reduce the load time and RAM? (directly load a scaled-down image) How to further modify the above coding?
It may be worth trying RGB_565 instead of ARGB_8888 if you don't have transparency.
just have found the answer for this reducing RAM and load time and avoid outofmemory error from other similar questions.
//get importing bitmap dimension
Options op = new Options();
op.inJustDecodeBounds = true;
Bitmap pic_to_be_imported = BitmapFactory.decodeFile(picturePath, op);
final int x_pic = op.outWidth;
final int y_pic = op.outHeight;
//The new size we want to scale to
final int IMAGE_MAX_SIZE= (int) Math.max(DrawViewWidth, DrawViewHeight);
int scale = 1;
if (op.outHeight > IMAGE_MAX_SIZE || op.outWidth > IMAGE_MAX_SIZE)
{
scale = (int)Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE /
(double) Math.max(op.outHeight, op.outWidth)) / Math.log(0.5)));
}
final BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
//Import the file using the o2 options: inSampleSized
bitmap = (BitmapFactory.decodeFile(picturePath, o2));
bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
I'm trying to find the size of my image but not to load into memory. I use the flowing code
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeResource(a.getResources(), R.drawable.icon, o);
int width1 = o.outWidt;
int height1 = o.outHeight;
Now, I make some comparison:
Bitmap icon = BitmapFactory.decodeResource(a.getResources(), R.drawable.icon);
int width = icon.getWidth();
int height = icon.getHeight();
Why width, height is not equal to width1 and height1 ?
I'm almost certain this is because referencing that image from resources with decode the image comes pre scaled for density.
Checkout #1 here on the docs: http://developer.android.com/guide/practices/screens_support.html#DensityConsiderations
I'm not sure what is going on, but it may be that BitmapFactory applies density scaling when actually returning a bitmap (as described here), but not when it is just decoding its size. (If this is what's going on, I'd consider filing a bug report.)
You can test this theory by moving your image from the drawables directory to drawables-nodpi.