SignaturePadView GetImage throwing OutOfMemoryError excetion - android

I am using SignaturePadView. I want to save signatures as images. I am testing on the sample code provided here.
points = signature.Points;
var image = signature.GetImage(true);
I can get the Points but when I call GetImage java runtime OutOfMemoryError exception is thrown.
Any ideas?
Thanks.

I debugged the code for the SignaturePad. It looks like by default the bitmap is too big to fit into memory. I gave it a size and disabled cropping as a workaround.
var bitmap = signature.GetImage(new SizeF(200,200), false);

Related

App crashing on operation on Bitmap

We have been trying to set a picture in our app, using a bitmap fecthed from a server. Problem is, the app keeps crashing on every operation we use on this Bitmap. `
String jsonInString = img;
image = gson.fromJson(jsonInString, Bitmap.class);
}
`
Is the code where we get the bitmap.
Then if we try to resize the Bitmap or set an ImageView with it, the app closes out, providing no errors. We have looked at the unfiltered part of the log aswell.
We have tried setting the ImageView to null, among other things, but it seems the error is in the bitmap.

Android : Bitmap causing out of memory error

I have been going crazy with this android error and from browsing the previous posts about this error none of the solutions that were given helped me.
I am of course talking about the all powerful killer known as the 'android Out of memory on a (NUMBER)-byte allocation.' I am trying to create a photo uploader and after the 3rd maybe 4th image my app crashes. I know to stop the crash to use a catch exception however that is not my problem. I am here to ask the community is there any solution to fixing this byte allocation error ?
Here is a snippet of my code involving the bit map .
String post = editTextPost.getText().toString().trim();
// get the photo :
Bitmap image = ((BitmapDrawable)postImage.getDrawable()).getBitmap();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// compress the image to jpg format :
image.compress(Bitmap.CompressFormat.JPEG, 45, byteArrayOutputStream);
byte[] imageBytes = byteArrayOutputStream.toByteArray();
String encodeImage = Base64.encodeToString(imageBytes,Base64.DEFAULT);
// Recycle bitmap :
image.recycle();
image=null;
// send data :
sendPost p = new sendPost(this);
p.execute(post, encodeImage);
imageBytes=null;
Im not using any libraries and would like to keep it that way however if using a library is the only option I will use one. Hopefully someone can help.
Bitmaps won't completely recycle if they are attached to a View, for example to a ImageView, if you do imageView.setImageBitmap(bitmap), you need to clear it before doing imageView.setImageBitmap(null) and clearing any other reference to any view.
After you finish uplloading the image release the memory occupied by the "postImage" -
postImage.setImageDrawable(null);
This will release any memory occupied by the bitmap associated with postImage
General Answer:
When you upload files you need to open a stream to your server and to
your file at same time. read and write chunk by chunk of 1 MB for
example. this way you will not get an out of memory exception.

Android: difference between BitmapFactory.decodeResource() and BitmapFactory.decodeFile() result?

I'm noticing a crash (in an external native library that does some image processing) when I pass it the pixel data returned from bitmap.getPixels().
If I package the image in the app, in the drawables folder and load the Bitmap with
BitmapFactory.decodeResource()
then grab the pixel data with
bitmap.getPixels()
there's no crash, and everything works as expected. However, if I load the same image from the file system with
BitmapFactory.decodeFile()
then grab the pixels with
bitmap.getPixels()
and hand that off, the native lib crashes.
Is there a difference between the way these two calls process the image into a Bitmap?
Reading the Android sources There is one interesting diffrence: The decodeFile method may call a different native bitmap decoder if the passed file is an asset, while the decodeResource will never do this.
if (is instanceof AssetManager.AssetInputStream) {
bm = nativeDecodeAsset(((AssetManager.AssetInputStream) is).getAssetInt(),
outPadding, opts);
However, the crash is most likely a bug in your native code. Messing up the stackframe with bad pointers and/or buffer overruns typically results in weird crashes like this. Try to check all your native code that runs before the crash and see if you can spot any memory issues like that.

Make expression that can handle large bitmaps

Like many others I have the anoying VM budget problem. I am getting files from the sdcard and I decode it and comprimizes them using:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
The problem is that i get the bitmap each time i call the onCreate method. So after turning phone or starting the activity a couple of times it crashes. The only method I can find to avoid this is:yourBitmap.recycle(), but you can not catch a recycled Bitmap.
I need an expression that still manage to show the the bitmap even after it would normaly crashed on VM budget. I am showing the bitmap as a drawable, so the classic unbindDrawables solotion does not work. I need an expression that eighter comprimices the Bitmap to almost nothing, or that recycles and shows a new Bitmap of the same size.
try reading this:
http://developer.android.com/training/displaying-bitmaps/index.html
and watching this:
http://www.google.com/events/io/2011/sessions/memory-management-for-android-apps.html
also , about drawables, see that you don't have any references to them after they are not needed . drawables can cause memory leaks.

Android Bytes to Bitmap

I have this working just fine in our iPhone app, but am having problems in Android. I'm using the same urls/data in both apps. When I set my image in my ListView to the bitmap that came from the bytes, the image doesn't appear. The data is there. Here is the code where I assign the view:
if (camera.snapshot != null)
{
bMap = BitmapFactory.decodeByteArray(camera.snapshot, 0, camera.snapshot.length);
image.setImageBitmap(bMap);
}
This is where I convert the string data into bytes:
camera.snapshot = responseData.getBytes();
The images are PNG files. They come in about 4 times the size that I need them for the listview image but I would think they would size perfectly to the bounds I set the ImageView to be.
On iPhone I simply use NSData and then use a prebuilt method in ImageView to turn it into an image. It works perfectly! What am I missing here?
You probably need to use the 4-argument version of decodeByteArray: see http://developer.android.com/reference/android/graphics/BitmapFactory.html#decodeByteArray%28byte[],%20int,%20int,%20android.graphics.BitmapFactory.Options%29.
The options would depend on the type of PNG image, so that you might need to experiment with. For a generic PNG, maybe something like this?
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inDither = true;
opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
You can see http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html and http://developer.android.com/reference/android/graphics/Bitmap.Config.html for more detail.
Everything is fine here. So you need to debug to try and find where else is the issue. i.e. is Camera.snapshot = null ? i.e. you might not be getting the data properly. Or there could also be an issue in the layouts to show the imageview. Try setting a predefined image to imageview and see if it is shown. This way you would be able to track the problem.

Categories

Resources