Creating bitmap from string path - android

So i'm creating bitmap, using this code:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inMutable = true;
return BitmapFactory.decodeFile(photoPath, options);
My problem is, why is it taking so much time creating it on some mobile ?
is there any way to create bitmap without wasting a lot of time waiting ?
Does any guys meet this issue? Any help would be greatly appreciated.

1) Use a scaled down version of image so that you can avoid memory as well as execution time wastage
2) Try processing the bitmaps asynchronously.
You can refer to http://developer.android.com/training/displaying-bitmaps/index.html

Try Below Code Snippet:
public static Bitmap decodeFile(String photoPath){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(photoPath, options);
options.inJustDecodeBounds = false;
options.inDither = false;
options.inPurgeable = true;
options.inInputShareable = true;
options.inPreferQualityOverSpeed = true;
return BitmapFactory.decodeFile(photoPath, options);
}

Related

BitmapFactory.decodeFile returns null with inJustDecodeBounds set to false

In my android app I am trying to create a Bitmap file from path.
Here is the path
localImagePath = "/storage/emulated/0/Hootout/HootImages/Profilepic/user_profile_photo.jpg"
Here is code for creating bitmap.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(localImagePath, options);
The bitmap file always turns out to be null.
What other things can I try?
First, create a bitmap from file path
File imgFile = new File("/storage/emulated/0/Hootout/HootImages/Profilepic/user_profile_photo.jpg");
if(imgFile.exists()){
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath(),bmOptions);
bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true);
imageView.setImageBitmap(bitmap);
}
I think you are giving the wrong file path.
Let me know if any error again.

OutOfMemoryError in BitmapFactory.decodeFile()

When I pick image from gallery if image size bigger than 3 Mb android the OutOfMemoryError.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, options);
This text from logs. Please help me, becouse "deadline")
E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:623)
at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:378)
at com.DriverNotes.AndroidMobileClientTest.ProfileActivity.onActivityResult(ProfileActivity.java:104)
at android.app.Activity.dispatchActivityResult(Activity.java:5456)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3402)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3449)
at android.app.ActivityThread.access$1200(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1328)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
OutofMemory occurs when your app exceeds memory allocated in heap. The bitmap is too large to fit in memory ie heap. In such a case you run out of memory. You need to scale down the bitmap and then use the same.
For that check this link
try this code may help you,
public static Bitmap decodeFile(File f,int WIDTH,int HIGHT){
try {
//Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//The new size we want to scale to
final int REQUIRED_WIDTH=WIDTH;
final int REQUIRED_HIGHT=HIGHT;
//Find the correct scale value. It should be the power of 2.
int scale=1;
while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT)
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;
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap bm = BitmapFactory.decodeFile(path,options);
Try this.
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inDither=false; //Disable Dithering mode
bmOptions.inPurgeable=true; //Tell to gc that whether it needs free memory, the Bitmap can be cleared
bmOptions.inInputShareable=true; //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
bmOptions.inTempStorage=new byte[32 * 1024];
Bitmap mainBitmap = BitmapFactory.decodeFile(filePath, bmOptions);

Using BitmapFactory.Options();

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

How can I use `InSampleSize` for pictures that I get from the camera intent? (Bitmap)

I've got this piece of code but it works only when the user has to choose the picture from the gallery:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
mPhoto = BitmapFactory.decodeFile(path, options);
image.setImageBitmap(mPhoto);
So I have a bitmap file, mPhoto. It has an image in it.
I need to apply the options on that photo.
How can I do that?
I've done it like that:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 10;
mPhoto = BitmapFactory.decodeFile(mUri.getPath(), options);

Differences between BitmapFactory.decodeResource and BitmapFactory.decodeStream

I've got an if-else scenario where the if-path uses this code:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inPurgeable = true;
options.inInputShareable = true;
bitmap = BitmapFactory.decodeResource(getResources(), resourceId, options);
And the else-path uses this code:
InputStream is = getContentResolver().openInputStream(Uri.fromFile(file));
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inPurgeable = true;
options.inInputShareable = true;
bitmap = BitmapFactory.decodeStream(is, null, options);
Now to me, reading the short description from the API, testing and Googling I can't quite figure out the difference, but there seems to be a suble one.
The first piece of code scales to fit the width of the screen when applied to a ImageView (within a RelativeLayout, within a ScrollView). The second piece of code does not, which is my problem. It seems to loose it's aspect ratio somehow as well, because if I apply:
imageView.setAdjustViewBounds(true);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
It manages to fit it to the width, but messes up the height (making it shorter than it should be). Any input would be greatly appreciated!
(I've already tried to change the first piece of code to see if I had messed something else up, but this rewrite of the if-path gave the same error:)
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inPurgeable = true;
options.inInputShareable = true;
bitmap= BitmapFactory.decodeStream(getResources().openRawResource(resourceId), null, options);

Categories

Resources