BitmapFactory returning null Android - android

Ok so i download a image and send the input stream to this method witch has to decode it once to find the image size then calculate a scale value then create a mini version of the bitmap from the stream.... but i get errors in logcat that the bitmapFactory is returning null, anyone have a idea what could be wrong ?
public static Bitmap getSampleBitmapFromStream(InputStream is,
int reqWidth, int reqHeight) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FlushedInputStream(is), null, options);
// Find the correct scale value. It should be the power of 2.
int width_tmp = options.outWidth, height_tmp = options.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < reqWidth || height_tmp / 2 < reqHeight)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// decode with inSampleSize
options.inJustDecodeBounds = false;
options.inSampleSize = scale;
return BitmapFactory.decodeStream(new FlushedInputStream(is), null, options);
}

the reason is: the stream you used twice
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FlushedInputStream(is), null, options);
and again:
options.inJustDecodeBounds = false;
options.inSampleSize = scale;
return BitmapFactory.decodeStream(new FlushedInputStream(is), null, options);
so if you want avoid this, you need close the stream and reopen the stream again.

What about supplying the InputStream directly?
return BitmapFactory.decodeStream(is, null, options);

Related

NullPointerException rescaling bitmap from file Android

I'm facing this problem since a month with no solution:
I need to convert a file into a bitmap and rescaling it.
I'm using the following method, taken from android guide: Loading Large Bitmaps Efficiently
public Bitmap decodeSampledBitmapFromFile(String path, int reqSize) {
// First decode with inJustDecodeBounds=true to check dimensions
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inMutable = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
double ratio = (double) options.outHeight / options.outWidth;
boolean portrait = true;
if (ratio < 1) {
portrait = false;
}
double floatSampleSize = options.outHeight / (double)reqSize;
options.inSampleSize = (int) Math.floor(floatSampleSize);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap resultBitmap = BitmapFactory.decodeFile(path, options);
if(floatSampleSize % options.inSampleSize != 0){
if(portrait) {
resultBitmap = Bitmap.createScaledBitmap(resultBitmap, (int)(reqSize/ratio), reqSize, true);
} else {
resultBitmap = Bitmap.createScaledBitmap(resultBitmap, reqSize, (int)(ratio*reqSize), true);
}
}
return resultBitmap;
}
I've got the same NullPointerException but i can't reproduce it:
NullPointerException (#Utilities:decodeSampledBitmapFromFile:397) {AsyncTask #2}
NullPointerException (#Utilities:decodeSampledBitmapFromFile:399) {AsyncTask #3}
lines 397 and 399 are:
resultBitmap = Bitmap.createScaledBitmap(resultBitmap, (int)(reqSize/ratio), reqSize, true);
resultBitmap = Bitmap.createScaledBitmap(resultBitmap, reqSize, (int)(ratio*reqSize), true);
in this device models:
ST21a
U9500
M7
U8950-1
GT-I9305
Someone can help me to solve this?
is BitmapFactory.decodeFile returns null?
thank you so much
Try this hope works for you
public Bitmap decodeFile(String path, int reqSize) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, o);
// The new size we want to scale to
final int REQUIRED_SIZE = reqSize;
// Find the correct scale value. It should be the power of 2.
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE && o.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeFile(path, o2);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}

do we need two bitmaps for 2 different ImageViews

I need to know whether there is need to set two bitmaps for displaying images in two ImageViews. I am uploading images from gallery .
code:-
public void decodeFile(String filePath) {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 1024;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
bitmap = BitmapFactory.decodeFile(filePath, o2);
//first image i have uploaded by using first button
imgView1.setImageBitmap(bitmap);
imgView2.setImageBitmap(bitmap);
}
Although the question isn't very clear, from reading the comments I understand that you might be wanting to set different bitmaps to two ImageViews. If that is the case, here is how you do it:
firstBitmap = BitmapFactory.decodeFile(filePath, o2);
secondBitmap = BitmapFactory.decodeFile(someOtherPath, o2);

android how to get the path name of the drawable

How do I get the path name to the drawables in my resource folder? I am trying to get the image in my resource folder and pass it to the function to decode it.
the path "android.resource://com.myapp.example/"+ R.drawable.image" didnt works.
Bitmap bitmap;
File image = new File("android.resource://com.myapp.example/"+ R.drawable.image);
bitmap.decodeFile(image);
public Bitmap decodeFile(File f) {
try {
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = 70;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale++;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
}
return null;
}
BitmapFactory has function to decode resources:
Bitmap BitmapFactory.decodeResource (Resources res, int id, BitmapFactory.Options opts)
Example:
bitmap.decodeFile(R.drawable.image);
public Bitmap decodeFile(int resId) {
try {
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getContext().getResources(), resId, o);
// Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = 70;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale++;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeResource(getContext().getResources(), resId, o2);
} catch (Exception e) {
}
return null;
}
No, you cant get the path because the resource folder is compiled into the apk, but you can grab the resource like this:
Resources rsrc= getResources();
Drawable drawable= rsrc.getDrawable(R.drawable.datImage);
Bitmap bm = BitmapFactory.decodeResource(rsrc,drawable, opts);

How to rescale a bitmap file in android?

I currently have a decode bitmap method to optimize the size of the image file, however the input file won’t be a bitmap and will decode to bitmap afterward. So my problem is, how can I resize it if my input is a bitmap already? Thanks.
private Bitmap compressFile(File f) {
int REQUIRED_SIZE = 80;
try {
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth;// , height_tmp = o.outHeight;
int scale = 1;
while (REQUIRED_SIZE > 0) {
if (width_tmp <= REQUIRED_SIZE)
break;
width_tmp /= 2;
// height_tmp /= 2;
scale++;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
}
return null;
}
What I would like to change to is compressFile (Bitmap f)
You may use this too
Bitmap b = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
profileImage.setImageBitmap(Bitmap.createScaledBitmap(b, 120, 120, false));
or are you asking for:
bitmap.compress(Bitmap.CompressFormat.PNG, quality, outStream);
check this link for more info

displaying image full screen - loses quality android

I'm trying to display image full screen but seems that the image loses quality. I get the image from an URL, I download it and when I'm displaying the images, it looks pretty bad. The images that I'm getting are in high resolution so I do not know what can the problem be?
Here is my Layout where i'm displaying the image:
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/full_screen_banner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/app_name" />
and here is the java code:
imageViews.put(imageView, url);
Bitmap bitmap = memoryCache.get(url);
if (bitmap != null)
imageView.setImageBitmap(bitmap);
If you are using image loader just go to imageloader class then
Check these lines
private Bitmap decodeFile(File f) {
try {
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);
// Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = 100;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
}
return null;
}

Categories

Resources