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.
Related
I was wondering about, how to implement Glide along with using Bitmap compression in Kotlin and thought that any of the code below would work. But unfortunately, the app closes as soon as I add an image into any of the imageView for the fourth time.
Here's the code which I tried to implement
val selectedImage = data?.data
val bitmap = MediaStore.Images.Media.getBitmap(contentResolver, selectedImage)
//method 1
Glide.with(this).asBitmap().load(compressBitmap(bitmap,5)).into(imageView!!)
//method 2
var bitmapDrawable = BitmapDrawable( resources , compressBitmap(bitmap,5))
Glide.with(this).load(bitmapDrawable).into(imageView!!)
What would be the correct code, if i'm somewhere wrong here. Thankyou in advance
Try using Recycler View or another component to load the images in. They will handle the load.
I want to take a picture in my App, so if I click on my ImageView/QuickBagde on my Fragment.
I'm able to take a picture, after that, I crop it and it return to my App. This is not a big deal and it's working well.
But how can I refresh the ImageView/QuickBadge to show up the taken picture?
I tried with:
//get the returned data
Bitmap thePic = data.getExtras().getParcelable("data");
profilePic.setImageBitmap(thePic);
But it doesn't work.
you can try this
Bitmap bitmap=BitmapFactory.decodeFile(imagepath);
imageview.setImageBitmap(bitmap);
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);
I'm working on a project that needs to use a large image as a map. The image is about 95MB and has a resolution of 12100 x 8000 pixels.
I don't need the whole image at once, I just need a detail of 1000 x 1000 Pixel (it's not always the same detail, just grabbing the same part is not a solution I can use). So I can't just sample it down with the BitmapOptions.
I looked around and found the idea to create a FileInputStream (the image is on the SD-Card) and then I can just load the detail with decodeStream(InputStream is, Rect outPadding, BitmapFactory.Options opts). That way I wouldn't load the whole thing into the memory. I tried it, but it's just crashing when I try to load the image. Here's my code:
FileInputStream stream = null;
try {
stream = new FileInputStream(path);
} catch(Exception e) {
Log.e("inputstream",e.toString());
}
Rect rect = new Rect(a,b,c,d);
return BitmapFactory.decodeStream(stream, rect, null);
When I try to load the image, the activity closes and LogCat tells me java.lang.outOfMemoryError. Why does it crash? I thought with the stream it should work on the image "on-the-fly", but the only explication I have for the Error is the it trys to load the hole image into the memory. Does anybody have an idea how I can load the detail out of the image, or why this idea doesn't work?
It crashed because all these 95M are sucked into memory for processing. This call will not ignore parts of the stream - it will put the whole thing to memory and then try to manipulate it. The only solution you can have is to have some sort of server side code that does the same sort of manipulation or if you don't want to do it on server - provide thumbnails of your large image. And I would strongly advise against pulling whole 95M at any time anyways
Does BitmapRegionDecoder not help (I realise its level 10)?
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.