Set Background - Picture in Framelayout - android

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)

Related

How to set bitmap image to Button Background Image

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.

How I use the image of a screenshot?

I want to use the code of this link: How to take a screenshot and share it programmatically to take screenshots of my application. But I want the picture which is produced to use it to change my background, like this Layout.setBackgroundResource(resid).
How I can do this?
Where I will find the image's path to use it to change Background?
First you need the view you want a screen shot of:
View anyView = findViewById(R.id.anyView);
Then use this method to take a screenshot:
public Bitmap screenShot(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
view.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
like this:
Bitmap screenshot = screenShot(anyView);
Bitmaps can't be set as a background image so lets convert it to a drawable:
Drawable drawableScreenshot = new BitmapDrawable(getResources(), screenshot);
Now set it to be the View background:
anyView.setBackgroundDrawable(drawableScreenshot);

Is it possible to get the image path of an image in an image view?

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

How to get the whole bitmap attached to an ImageView?

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();

How to convert a Bitmap to Drawable in android?

How can I convert a Bitmap image to Drawable ?
Try this it converts a Bitmap type image to Drawable
Drawable d = new BitmapDrawable(getResources(), bitmap);
Sounds like you want to use BitmapDrawable
From the documentation:
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.
Having seen a large amount of issues with bitmaps incorrectly scaling when converted to a BitmapDrawable, the general way to convert should be:
Drawable d = new BitmapDrawable(getResources(), bitmap);
Without the Resources reference, the bitmap may not render properly, even when scaled correctly. There are numerous questions on here which would be solved simply by using this method rather than a straight call with only the bitmap argument.
Offical Bitmapdrawable documentation
This is sample on how to convert bitmap to drawable
Bitmap bitmap;
//Convert bitmap to drawable
Drawable drawable = new BitmapDrawable(getResources(), bitmap);
imageView.setImageDrawable(drawable);
I used with context
//Convert bitmap to drawable
Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);
1) bitmap to Drawable :
Drawable mDrawable = new BitmapDrawable(getResources(), bitmap);
// mImageView.setDrawable(mDrawable);
2) drawable to Bitmap :
Bitmap mIcon = BitmapFactory.decodeResource(context.getResources(),R.drawable.icon_resource);
// mImageView.setImageBitmap(mIcon);
If you have a bitmap image and you want to use it in drawable, like
Bitmap contact_pic; //a picture to show in drawable
drawable = new BitmapDrawable(contact_pic);
Just do this:
private void setImg(ImageView mImageView, Bitmap bitmap) {
Drawable mDrawable = new BitmapDrawable(getResources(), bitmap);
mImageView.setDrawable(mDrawable);
}
here's another one:
Drawable drawable = RoundedBitmapDrawableFactory.create(context.getResources(), bitmap);
For Kotlin users:
Kotlin has a function for converting Bitmap to BitmapDrawable:
Bitmap.toDrawable(resources)
covert bit map to drawable in sketchware app using code
android.graphics.drawable.BitmapDrawable d = new android.graphics.drawable.BitmapDrawable(getResources(), bitmap);

Categories

Resources