I would like to set background in LinearLayout from file, path is correct, but not works
Bitmap bitmap5 = BitmapFactory.decodeFile(data.getData().getPath());
BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap5);
mDrawingPad.setBackground(bitmapDrawable);
Related
I have a grid. Each item has 2 ImageViews. Loaded image is the same for both ImageViews, but in one ImageView I want to apply a transformation to bitmap.
Can I load original bitmap only one time and cache both original and transformed bitmap (for both Imageviews) ?
private DrawableRequestBuilder<String> mFullRequest;
private DrawableRequestBuilder<String> mBlurRequest;
...
//in my adapter constructor
mFullRequest = Glide.with(context)
.fromString()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.priority(Priority.HIGH)
.crossFade(R.anim.fade_in, 150);
mBlurRequest = Glide.with(context)
.fromString()
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.transform(new BlurredImageTransformation(context))
.crossFade();
.....
//in getview
final String imgUrl =.....
mFullRequest
.load(imgUrl)
.into(holder.campaignImage);
mBlurRequest
.load(imgUrl)
.override(Constants.MAX_BLURRED_IMAGE_WIDTH, (int) (Constants.MAX_BLURRED_IMAGE_WIDTH * ratio))
.into(holder.blurredImage);
For example ..
Bitmap myPic = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
Bitmap newPic = Bitmap.createScaledBitmap(myPic, 50, 50, true);
Canvas myCanv = new Canvas(newPic);
View myView = (View)findViewById(R.id.view1);
myView.draw(myCanv);
is something like the above feasible?
You really shouldn't do it like that. My recommendation is to do it like this
Bitmap myPic = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
myPic = Bitmap.createScaledBitmap(myPic,50,50, true);
BitmapDrawable draw = new BitmapDrawable(this.getResources(), myPic);
View myView = (View) findViewById (R.id.view1);
myView.setBackground(draw);
This way you pass your View a drawable as a background.
If you really want to set a Bitmap as your source then you should make an ImageView
I am trying to read an image from /mnt/sdcard/img.jpg into ImageView.
Bitmap bm = BitmapFactory.decodeFile("/mnt/sdcard/img.jpg");
imageView1.setImageBitmap(bm);
I have write external storage permission.
But ImageView is empty, LogCat don't get any errors,
How can I fix it?
Try
File file = new File("/mnt/sdcard/img.jpg");
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
imageview1.setImageBitmap(bitmap);
I'm trying to load a png image as a drawable from my device sd card.
I use the following function but it doesn't work:
public Drawable getDrawable()
{
return new BitmapDrawable(imagePath);
}
The image path is: mnt/sdcard/MyFolder/image.png
The app crashes when I try calling that method, how should I load my png image located in my sdcard and cast it into a Drawable object?
There is actually a BitmapDrawable constructor straight from file path. The method you are using is depricated. Try:
Drawable myDrawable = new BitmapDrawable(getResources(), pathName);
If this doesnt work, Try getting a bitmap and creating a drawable from it:
The bitmap can be created with decodeFile
You can use it like this:
Bitmap myBitmap = BitmapFactory.decodeFile(pathName);
Then you can use the bitmap for drawing etc.
to convert Bitmap to drawable use
Drawable myDrawable = new BitmapDrawable(getResources(), myBitmap);
Take a look Here (Bitmaps) and Here (Bitmap Drawables) for more info.
I am simple do like that
public Drawable getDrawableFromPath(String filePath) {
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
//Here you can make logic for decode bitmap for ignore oom error.
return new BitmapDrawable(bitmap);
}
I am getting the current wallpaper by using following code:
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
How can I create a bitmap from this?
like when I create a bitmap from res folder I use this
Resources res = getApplicationContext().getResources();
b = BitmapFactory.decodeResource(res, R.drawable.wall);
What code I should use to get current wallpaper into the bitmap so I can draw it on my canvas and use it as my live wallpaper background?
The Drawable fetched should really be a BitmapDrawable. You can verify this using instanceof if necessary.
That being the case, all you have to do is:
final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
final Bitmap wallpaperBitmap = ((BitmapDrawable) wallpaperDrawable).getBitmap();
EDIT: If it turns out that the Drawable is NOT a BitmapDrawable, you can use the following method to convert it (found in this answer, credit to André):
public static Bitmap drawableToBitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable)drawable).getBitmap();
}
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}