OOM Error when loading Drawable - android

My Image has the size: 2480px x 3508px. I know that this is a very big image for android and so I try to resize it.
I'm using the PhotoView library too by the way.
Here i start resize the Image:
Drawable resizedDrawable = resize(getResources().getDrawable(R.drawable.handskizze), 50, 50);
skizze.setImageDrawable(resizedDrawable);
mAttacher = new PhotoViewAttacher(skizze);
The method which resizes the drawable:
private Drawable resize (Drawable image, int dstWith, int dstHeight) {
Bitmap b = ((BitmapDrawable)image).getBitmap();
Bitmap bitmapResized = Bitmap.createScaledBitmap(b,dstWith,dstHeight,false);
return new BitmapDrawable(getResources(),bitmapResized);
}
The Error:
at de.example.app.TouchMoveImage.onCreate(TouchMoveImage.java:70)
So why i can't resize the Image? I know that is it to big for android. But it crashes before it starts resizing.

Related

How to get Bitmap with actual size of it, in Bitmap variable irrespective of deivce I am using

I have a jpg image of size 1080*1080 in my drawable folder, when I am storing it in a Bitmap variable the size of the bitmap is not showing as 1080*1080, it is showing based on the device I am using, I want to draw the actual image with size 1080*1080 on a canvas, any idea how to get the bitmap with actual size
Bitmap bitmap = BitmapFactory.decodeResource(activity.getResources(), R.drawable.template_image);
int w = bitmap.getWidth();
int h = bitmap.getHeight();
The variable w and h are not showing 1080*1080, it's different on different devices
put your image file in drawable-nodpi This folder tells android to avoid scaling your image file on any pixel density:
More Info Medium Article
If you need to only retrieve image size without showing inside image view:
BitmapFactory.Options options = new BitmapFactory.Options();
//Just Retreive info about the bitmap without loading inside memory to avoid OutOfMemory.
options.inJustDecodeBounds(true)
Bitmap bitmap = BitmapFactory.decodeResource(activity.getResources(), R.drawable.template_image, options);
int w = options.outWidth;
int h = options.outHeight;

Avoid bitmap scaling with different canvas size

I am trying to implement overlay transformation for Picasso library.
So, I am loading image into image view and want to display overlay using bitmpap
First of all I am fill canvas with transparent color
Then I am trying to draw drawable over
#Override
public Bitmap transform(Bitmap source){
Bitmap workingBitmap=Bitmap.createBitmap(source);
Bitmap mutableBitmap=workingBitmap.copy(Bitmap.Config.ARGB_8888,true);
Canvas canvas=new Canvas(mutableBitmap);
canvas.drawColor(getResources().getColor(R.color.gray_transparent));
BitmapFactory.Options options=new BitmapFactory.Options();
options.inScaled=false;
Bitmap b=BitmapFactory.decodeResource(getResources(),R.drawable.ic_gif_white_24dp,options);
canvas.drawBitmap(b,source.getWidth()/2,source.getHeight()/2,paint);
source.recycle();
return mutableBitmap;
}
The problem is that I see different overlay image size when canvas size is different. How I can avaid bitmap scaling depends on canvas size? I want to see same GIF size with different canvases size.
On first image canvas has w = 1280, h = 854, second image w = 480, h = 270
UPDATE
I found solution to scale bitmap depends on canvas size, it works well, but I wish to find out better solution
BitmapDrawable drawable = (BitmapDrawable) getResources().getDrawable(R.drawable.ic_gif_white_24dp);
Bitmap drawableBitmap = drawable.getBitmap();
double size = source.getWidth() * 0.15;
Bitmap scaled = Bitmap.createScaledBitmap(drawableBitmap, (int) size, (int) size, true);

Displaying SVG file on android - zero sized drawable?

I am using the svg-android library to load and display SVG images:
ImageView iv = (ImageView)findViewById(R.id.imgLoginLogo);
// Set the background color to white
iv.setBackgroundColor(Color.WHITE);
// Parse the SVG file from the resource
SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.android);
// Get a drawable from the parsed SVG and set it as the drawable for the ImageView
Drawable d = svg.createPictureDrawable();
iv.setImageDrawable(d);
Problem is the drawable I get has zero dimensions, so it is not visible.
What is wrong here ?
Thanks
Shimon
I use this to create a drawable:
public static Bitmap createBitmap(InputStream in, int w, int h) {
com.larvalabs.svgandroid.SVG svg = new SVGBuilder().readFromInputStream(in).build();
Bitmap bitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Picture pic = svg.getPicture();
canvas.drawPicture(pic, new Rect(0, 0, w, h));
return bitmap;
}
Notice that you must care about recycling the bitmap after done with it.

How to get the size of bitmap after displaying it in ImageView

I have a imageview
<ImageView
android:id="#+id/imgCaptured"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#drawable/captured_image" />
I capture a image from camera, converted that image into bitmap.
Bitmap thumbnail;
thumbnail = MediaStore.Images.Media.getBitmap(getActivity()
.getContentResolver(), imageUri);
when i get the resolution of this bitmap before displaying it in my above imageview, like
Log.i("ImageWidth = " + thumbnail.getWidth(), "ImageHeight = "
+ thumbnail.getHeight());
its returning me ImageWidth = 2592 ImageHeight = 1936
after this i displayed this bitmap in my above imageview as imgCaptured.setImageBitmap(thumbnail); then i go size of my imageview as
Log.i("ImageView Width = " + imgCaptured.getWidth(),
"ImageView Height = " + imgCaptured.getHeight());
this returned me ImageView Width = 480 ImageView Height = 720
now my question is that
How can i get the size of that bitmap after displaying it in my imageview.
I know this can be done by using this
image.buildDrawingCache();
Bitmap bmap = image.getDrawingCache();
but this will create a new bitmap of size equal to that of imageview.
I also want to know that, Does image resized automatically after displaying it in the imageview. if yes then is there any way to display the image in imageview without resizing the image.
Edit
Actually i have captured an image of 2592x1936. I displayed this image in my imageView, did some other operations on this image . now i want to save this image with same 2592x1936 resolution. is it possible?
Thanks in advance.
After you display a Bitmap in a ImageView, The ImageView will create a BitmapDrawable object to draw it in ImageView's Canvas. So you can invoke ImageView.getDrawable() method to get the reference of the BitmapDrawable, and get the Bounds by invoke Drawable.getBounds(Rect rect) method. through the bounds, you can compute the width and height of the Bitmap drawn in ImageView
Drawable drawable = ImageView.getDrawable();
//you should call after the bitmap drawn
Rect bounds = drawable.getBounds();
int width = bounds.width();
int height = bounds.height();
int bitmapWidth = drawable.getIntrinsicWidth(); //this is the bitmap's width
int bitmapHeight = drawable.getIntrinsicHeight(); //this is the bitmap's height

Bizarre ImageView Bitmap Handling

I've encountered totally bizarre behavior in the way Android loads bitmaps into ImageView. For example, I have a 500x313 image file called urimg_01.jpg. With this code:
img.setImageResource(R.drawable.urimg_01);
Bitmap bitmap = ((BitmapDrawable) img.getDrawable()).getBitmap();
Log.v(TAG,"---------------------> bitmap width = "+bitmap.getWidth());
Log.v(TAG,"---------------------> bitmap height = "+bitmap.getHeight());
the ImageView bitmap is 750x470. (I have a Nexus S with 480x800 display.)
With this code, which reads a copy of the same file located in getFilesDir():
Log.v(TAG,"image file is "+filelist[0].getAbsolutePath());
FileInputStream fis = new FileInputStream(filelist[0]);
FileDescriptor fd = fis.getFD();
Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fd);
if (bitmap != null) {
Log.v(TAG,"---------------------> bitmap width = "+bitmap.getWidth());
Log.v(TAG,"---------------------> bitmap height = "+bitmap.getHeight());
img.setImageBitmap(bitmap);
fis.close();
}
the ImageView bitmap is 500x313, as expected.
In the case of setImageResource(), where the devil is it getting 750x470 from?? And how do I get it to use the correct dimensions for resources in drawable?
Your resource is being scaled up because it's probably stored in a medium density folder and you're using a high density device. See Bitmap getWidth returns wrong value for details.
here you get your bitmap
Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fd);
now scale it according to device height and width
Display display=this.getWindowManager().getDefaultDisplay();
int w=display.getWidth();
int h=display.getHeight();
Bitmap newBitmap=Bitmap.createScaledBitmap(oldBitmap, w, h, false);
//now setImage to background with new Bitmap
One important thing "this" should be activity context
:) cheers

Categories

Resources