I have an ImageView with visibility = View.VISIBLE and predefined width and height in XML.
When I set in my code ImageBitmap like
someImageView.setImageBitmap(bitmap);
how can I later on know is there some image bitmap already defined or it's still empty (null)?
So something like someImageView.getImageBitmap() == null ...I cannot test width, height or visibility because it's already predefined and it's the same thing if there is a bitmap or not.
Your imageView doesn't contain a bitmap if:
imageView.getDrawable() == null
Try this:
try{
bitmap = ((BitmapDrawable) someImageView.getDrawable()).getBitmap();
//re-use bitmap somehow?
}
catch(NullPointerException e){
//Bitmap dont exists
}
Related
I'm trying to make a screenshot of the rotated TextView which contain emoji icons. But on resulting bitmap i see that emoji are not rotated! Why is this happening? How can i make a screenshot with rotated emoji ?
What i expect:
And this is what i get:
I'm using this method to get screenshot of view:
layout.setDrawingCacheEnabled(true);
layout.buildDrawingCache();
Bitmap bitmap = null;
if (layout.getDrawingCache() != null)
bitmap = layout.getDrawingCache().copy(Bitmap.Config.ARGB_8888, false);
layout.setDrawingCacheEnabled(false);
layout.destroyDrawingCache();
Update:
as i figured, if I set textView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); then emoji will not be rotated even in the TextView (if you rotate TextView - they will not be rotated, they will be just moving around like a carousel), but still I don't really understand why is this happening, or rotation of emoji (on first picture) is only because of hardware acceleration?
ok, i couldnt find a way to solve this really annoying issue, so i had to hack it a bit.
imageviews works perfectly with rotation.
so i basically do all the manipulations with image view - and setting it's image out of the emoji text i want using this method:
private Bitmap generateBitmapFromText(int size, String res) {
TextView tv = new TextView(getContext());
tv.setText(res);
tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, 150f);
tv.setTextColor(0xffffffff);
tv.measure(size, size);
int questionWidth = tv.getMeasuredWidth();
int questionHeight = tv.getMeasuredHeight();
Bitmap bitmap = Bitmap.createBitmap(questionWidth, questionHeight, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
tv.layout(0, 0, questionWidth, questionHeight);
tv.draw(c);
return bitmap;
}
and then i call
Bitmap bitmap = generateBitmapFromText(stickersStartingSize, res);
imageview.setImageBitmap(bitmap);
In my project I used (Volley + NetworkImageView) to download some images and texts and showing them in a list view.. till here, I don't have any problem.
Now, I want to get bitmaps form NetworkImageView and I tried many methods like the following, but non of them worked for me.
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
Another method:
imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();
Non of them worked..
Any Help is appreciated,,
You cannot get the Bitmap reference as it is never saved in the ImageView.
however you can get it using :
((BitmapDrawable)this.getDrawable()).getBitmap();
beacuse when you set it with Volley u do this:
/**
* Sets a Bitmap as the content of this ImageView.
*
* #param bm The bitmap to set
*/
#android.view.RemotableViewMethod
public void setImageBitmap(Bitmap bm) {
// Hacky fix to force setImageDrawable to do a full setImageDrawable
// instead of doing an object reference comparison
mDrawable = null;
if (mRecycleableBitmapDrawable == null) {
mRecycleableBitmapDrawable = new ImageViewBitmapDrawable(
mContext.getResources(), bm);
} else {
mRecycleableBitmapDrawable.setBitmap(bm);
}
setImageDrawable(mRecycleableBitmapDrawable);
}
however if you set your default image or error image or any other image in any other way you may not get BitmapDrawable but NinePatchDrawable for example.
here is how to check:
Drawable dd = image.getDrawable();
if(BitmapDrawable.class.isAssignableFrom(dd.getClass())) {
//good one
Bitmap bb = ((BitmapDrawable)dd).getBitmap();
} else {
//cannot get that one
}
I have set an image in an image view. Now I want to save this state and need to know the image path of that image. Is there a way to do that?
UPDATE:
// Decode, scale and set the image.
Bitmap myBitmap = BitmapFactory.decodeFile(selectedImagePath);
Bitmap scaledBitmap = Bitmap.createScaledBitmap(myBitmap, NEW_WIDTH, NEW_HEIGHT, true);
myBitmap.recycle();
myBitmap = null;
mImageView.setImageBitmap(scaledBitmap);
Not directly, once an image is set on an ImageView it is turned into a Drawable and all else is forgotten. However, you could use tags for this purpose. Any view can have a tag associated with it that represents some metadata about that view. You could do something like:
ImageView iv; //Declared elsewhere
Uri imagePath = Uri.parse("...");
//Set the image itself
iv.setImageURI(imagePath);
//Add the path value as a tag
iv.setTag(imagePath);
//Sometime in the future, retrieve it
Uri path = (Uri)iv.getTag();
You might also consider creating a subclass of ImageView to encapsulate this extra function to help make your code a little easier to read and maintain.
HTH
I tried to get Bitmap attached to an ImageView, using ImageView.getDrawingCache(); But I found that the returned Bitmap was not the same as I'd like to get from the ImageView. It was always smaller than the real image.
I had known that, the method getDrawingCache() should not have the view if it is bigger than the screen as the visible portion of the view is only drawn and the cache holds only what is drawn.
Could I get the whole bitmap attached to a ImageView?
If you just want the Bitmap from a ImageView the following code may work for you:-
Bitmap bm=((BitmapDrawable)imageView.getDrawable()).getBitmap();
I think that's what you wanted.
If your drawble is not always an instanceof BitmapDrawable
Note: ImageView should be set before you do this.
Bitmap bitmap;
if (mImageView.getDrawable() instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();
} else {
Drawable d = mImageView.getDrawable();
bitmap = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
d.draw(canvas);
}
Your bitmap is stored in bitmap.
Voila!
Easiest way is to set tag in ImageView.
imageView.setImageBitmap(bitmap);
imageView.setTag(bitmap);
To get Tag from it
imageView.getTag();
Perhaps very simple, but not for me ;)
How do i change the Picture of a Framelayout in my sourcecode?
My first intention was:
mBackgroundDrawable = getResources().getDrawable(R.drawable.picture);
main.setBackgroundDrawable(mBackgroundDrawable);
but there i only can add pictures from my "RES/DRAWABLE-HDPI" folder.
I want to use my SD-Card as the destinated source.
Whats the right way?
Try this:
Bitmap bMap = BitmapFactory.decodeFile("/sdcard/test2.png");
image.setImageBitmap(bMap);
BitmapDrawable dr=new BitmapDrawable(bMap);
You'll need to convert your Bitmap to a BitmapDrawable before able to set it as a background.
val bitmap = BitmapFactory.decodeFile(this.file.absolutePath)
frameLayout.background = BitmapDrawable(context.resources, bitmap)