I'm tryimng to see if I can creat dynamic images by creating a bitmap and using setPixel. The program crashes when I call setPixel,
Bitmap bm= createBitmap (50,50, Bitmap.Config.RGB_565);
// program crashes here
bm.setPixel(25,25,0xffffff);
// add a test viue
ImageView mImage= new ImageView(this);
mImage.setImageBitmap(bm);
LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);
layout.addView(mImage);
I figure it is something simple, but cannot figure it out.
Ted
Can you paste the error message for more information ?
You're using Bitmap.Config.RGB_565, did you have tried with Bitmap.Config.ARGB_8888 instead ?
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.
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.
my code is this
ll.setBackground(Drawable.createFromPath(new ImageLoader().fullPath+"/desiredFilename.png"));
ll is the object of my linear layout ,is this the correct method
Bitmap bitmap = BitmapFactory.decodeFile(new ImageLoader().fullPath+"/desiredFilename.png");
Resources res=getResources();
BitmapDrawable bitmapDrawable = new BitmapDrawable(res,bitmap);
ll.setBackground(bitmapDrawable);
i also used this code but dosent work
shows error noSuchMethods
try this way
ll.setBackgroundDrawable(bitmapDrawable);
instead of
ll.setBackground(bitmapDrawable);
I'm using Robolectric to test android code, and found there are some tests failed because there are some methods don't have shadow method.
In my logic code, I retrieved a bitmap from another activity:
Bitmap bitmap = getBitmapFromResult(data);
BitmapDrawable drawable = new BitmapDrawable(getResources(), bitmap);
imageView.setImageDrawable(drawable);
And later in another method, I get the bitmap from the imageView, and save it to file:
BitmapDrawable drawable = (BitmapDrawable)imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
// save to file
bitmap.compress(...);
But the last line throws NullPointerException when testing.
I debugged into the code, and found when the test code running with robolectric, the drawable is a ShadowBitmapDrawable contains a null bitmap. So drawable.getBitmap() return a null which causes the exception.
Then I enabled logging in my test code:
Robolectric.logMissingInvokedShadowMethods();
Found one line in console which I think causes my test code failed:
No Shadow method found for BitmapDrawable.<init>(android.content.res.Resources, android.graphics.Bitmap)
What can I do now? Is it possible to add a shadow method to fix it and how to do that?
Clone the source from github: https://github.com/pivotal/robolectric/
Add a shadow method to ShadowBitmapDrawable.java:
public void __constructor__(android.content.res.Resources res, Bitmap bitmap) {
this.bitmap = bitmap;
}
Then compile it and package it to a jar. It's fixed.
I was trying to set a bitmap image to a canvas using setBitMap ,at that time I got an IllegalStateException.This canvas have some images on it currently, I am trying to replace it.
Any one have any idea why this happened?
Code Snippet
editBm = Bitmap.createBitmap(951, 552, Bitmap.Config.ARGB_8888);
Canvas mCanvas=new Canvas(editBm);
eBit=LoadBMPsdcard(filePath); ---->returns a bitmap when the file path to the file is provided
Log.i("BM size", editBm.getWidth()+"");
mCanvas.setBitmap(eBit);
I am not getting any NullPointer errors and the method LoadBMPsdcard() is working good.
Please let me know about any ideas you have ...
Thanks in advance
Happy Coding
IllegalStateException could be thrown because you're loading a Bitmap (eBit) and use mCanvas.setBitmap(eBit) without checking if the bitmap is mutable. This is requiered to draw on the Bitmap. To make sure your Bitmap is mutable use:
eBit=LoadBMPsdcard(filePath);
Bitmap bitmap = eBit.copy(Bitmap.Config.ARGB_8888, true);
canvas.setBitmap(bitmap);
Try to use drawBitmap instead of the setBitmap. It looks like you've already set a bitmap to draw into by passing it to the canvas constructor, so now you just need to draw everything onto it.
Canvas.setBitmap() throws IllegalStateException if and only if Bitmap.isMutable() returns true. Bitmap.createBitmap() builds an immutable Bitmap instance only, in all of its forms. To create a mutable bitmap you either use new Bitmap(), or Bitmap.copy(true), depending on whether you have a source bitmap that you want to start with. A typical block for me looks like:
Bitmap image = ...
Canvas c = new Canvas(image.isMutable()?image:image.copy(true));
...
This assumes, of course, that you don't mind clobbering the source Bitmap (which I generally don't but that's by no means universal).