gridcell = (Button) row.findViewById(R.id.calendar_day_gridcell);
gridcell.setText("Day 1");
URL url = new URL("http://172.16.4.29:81/pht/2013/9/18/3027_2013_9_18_12_14_56_b.JPG");
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
How can I set bitmap bmp image to button gridcells background Image?
You can use following code to do that..
BitmapDrawable bdrawable = new BitmapDrawable(context.getResources(),bitmap);
then just set bitmap with below function
button.setBackground(bdrawable);
you need to check current version of Android also
Button bProfile; // your Button
Bitmap bitmap; // your bitmap
if(android.os.Build.VERSION.SDK_INT < 16) {
bProfile.setBackgroundDrawable(new BitmapDrawable(getResources(), bitmap));
}
else {
bProfile.setBackground(new BitmapDrawable(getResources(),bitmap));
}
Hey First learn about Handling bitmaps: http://developer.android.com/training/displaying-bitmaps/process-bitmap.html
You should not process bitmaps on the UI thread at all.
Once you have attained Bitmap use ImageView's method setImageBitmap(Bitmap bitmap).
To fetch images through network, You can also use libraries like picasso, Universal Image Loader, Volley to attain what you are wanting.
I always and will always recommended you strongly using Image Button.
http://developer.android.com/reference/android/widget/ImageButton.html
I hope that helps you, Shaun.
Related
I need to take an image from imageView and make it as a bitmap.
String s = getIntent().getStringExtra("ImageUri");
mImageViewFilter = (ImageView) findViewById(R.id.imageViewFilter);
Glide.with(this)
.load(s)
.into(mImageViewFilter);
Bitmap bitmap = ((BitmapDrawable)mImageViewFilter.getDrawable()).getBitmap();
You can see that into mImageViewFilter I am loading a photo with use of its URI and Glide library.
My next step is to take this photo which is in mImageViewFilter and make it as a bitmap. After the last line of code there is an exception. What am I doing wrong?
String s = getIntent().getStringExtra("ImageUri");
mImageViewFilter = (ImageView) findViewById(R.id.imageViewFilter);
To load image into ImageView use below code
Glide.with(this).load(s).into(mImageViewFilter);
So in order to get bitmap from ImageView (mImageViewFilter )
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
OR
You can add this before the code where you are trying to get Bitmap out of the ImageView
mImageViewFilter.setDrawingCacheEnabled(true);
In order to get the Bitmap out of the ImageView using DrawingCache, you first need to enable ImageView to draw image cache.
Bitmap bitmap = mImageViewFilter.getDrawingCache();
Actually you doing right thing. But the problem is Image loading is Asynchronous call.
And you called getBitmap() in right next line which will be called sequentially.
And it will give you null anyway cause image is not loaded yet .
Solution
if you want to do it in next direct step .You can use direct Bitmap callback.
Glide.with(this)
.asBitmap()
.load(path)
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
imageView.setImageBitmap(resource);
// Use resource as bitmap save it for later.
}
});
First take it as bitmap, then take it again to apply to our ImageView :
Bitmap bitmap = Glide.with(this) //taking as bitmap
.load(s)
.asBitmap()
.into(100, 100) //width and height
.get();
Glide.with(this) //apply to imageview
.load(s)
.into(mImageViewFilter);
The straightforward way to obtain a bitmap from any View including ImageView is to get the drawing cache from it.
mImageViewFilter.buildDrawingCache();
Bitmap bitmap = mImageViewFilter.getDrawingCache();
But the above method may give you a low quality image.
I think you should directly decode the bitmap from the Uri using BitmapFactory
InputStream in = context.getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(in);
in.close();
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
}
ALL,
As suggested here I need to make a drawable out of my bitmap. But when I tried to use:
Drawable d = new Drawable( my_bmp);
it shows that this constructor is deprecated in favour of:
Drawable(Bitmap bmp, int resourceId)
How else I can make a drawable out of bitmap?
Thank you.
You can use
Drawable d = new BitmapDrawable(getResources(), my_bmp);
A Drawable that wraps a bitmap and can be tiled, stretched, or
aligned. You can create a BitmapDrawable from a file path, an input
stream, through XML inflation, or from a Bitmap object.
BitmapDrawable(Resources res, Bitmap bitmap)
Create drawable from a bitmap, setting initial target density based on
the display metrics of the resources.
Also look a the public constructors #
http://developer.android.com/reference/android/graphics/drawable/BitmapDrawable.html
try this to load Bitmap as Bitmap Drawable
Create ImgDrawableFromFile(Resources res, String file_name) like below
Drawable d = null;
public Drawable ImgDrawableFromFile(Resources res, String file_name) {
myBitmap = null;
File imgFile = new File(
"/data/data/yourpkgname/app_my_sub_dir/images/" + file_name);
if (imgFile.exists()) {
myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
if (myBitmap != null) {
d = new BitmapDrawable(res, myBitmap);
return d;
} else {
return null;
}
}
return null;
}
Now called this function like
img.setBackgroundDrawable(ImgDrawableFromFile(getResources(), "1.jpg")); // pass your saved file name as second argument
A little late to the party but I see a clear misunderstanding. The answers provided are indeed correct:
Drawable d = new BitmapDrawable(getResources(),bitmap);
In this case getResources() is a method available in the Context or Activity and is only used to find out the screen density and adjust the Drawable accordingly.
So Igor, even if you are loading the image from the cloud, you can still call getResources().
Happy coding
BitmapDrawable(Bitmap bitmap) constructor has been deprecated
you can use
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
source
you can use the BitmapDrawable class to get a Drawable from a bitmap
Bitmap Drawable reference
If Mauro Banze was late to the party then I don't really know what I'm still doing here. But for the ones that come here desperately looking for a solution, here is what I did:
I stumbled over somewhat the same issue when bringing a Project from JavaFX to Android. I wanted to reuse most of my "data" classes which only manage and provide my data and have no affect on the UI.
Enough story telling, here we go:
Problem:
At some point we need a Drawableto get out Bitmap on the screen.
We want to download (from the cloud, the internet, or wherever) this Bitmap and save it for later usage as a Drawable
We can't do that in a non-Context or non-Activity class, as we need the Resources.
So why don't we just save it as a Bitmap until we need it to be drawn (which certainly will happen in a Context or Activity class).
I have created a class called BitmapImage. It looks like this:
public class BitmapImage {
private final Bitmap bitmap;
public BitmapImage (Bitmap bitmap) {
this.bitmap = bitmap;
}
public Bitmap getBitmap() {
return bitmap;
}
public Drawable getDrawable(Resources res) {
return new BitmapDrawable(res,getBitmap());
}
This very simple class "saves" the Bitmap. Thus, rather then working with a Drawable you work with the BitmapImage until you really desperately need the Drawable.
At that point, you should be in your Activity or Context and there you can call Drawable foo = anyBitmapImage.getDrawable(getResource()).
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
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)