I want to get Bitmap from drawable resource id, I found a solution to decode it using BitmapFactory decode method, but it's giving null.
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_tick);
Please check if you are using Vector drawable or normal png/jpeg image as a drawable, it gives null value if you try to decode vector drawable.
Use this code if you are trying to decode Vector drawable
public static Bitmap getBitmapFromVectorDrawable(Context context, int drawableId) {
Drawable drawable = ContextCompat.getDrawable(context, drawableId);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
drawable = (DrawableCompat.wrap(drawable)).mutate();
}
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
Also make sure if you have vector support enable in your app gradle
vectorDrawables.useSupportLibrary = true
Related
I'm using this function, it's working but give me only screenshot so if the web view more than page I can't get all content in bitmap
public Bitmap createBitmapFromView(View v) {
v.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT));
v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Bitmap bitmap = Bitmap.createBitmap(v.getMeasuredWidth(),
v.getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
v.draw(c);
return bitmap;
}
I have a problem with android, version 5.1. (lolipop).
When I tried to scale bitmap using Bitmap.createScaledBitmap, app was send to me an error:
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
I doesn't know why this is doing. On newest versions of android this working good.
ImageView imageView = this.menuPageModel.getBackground();
int id;
Bitmap bitmap;
Context context = appModel.getContext();
id = context.getResources().getIdentifier("background", "drawable", context.getPackageName());
bitmap = BitmapFactory.decodeResource(context.getResources(), id);
bitmap = Bitmap.createScaledBitmap(bitmap, appModel.getScreenWidth(), appModel.getScreenHeight(), false);
imageView.setLayoutParams(new android.view.ViewGroup.LayoutParams(appModel.getScreenWidth(), appModel.getScreenHeight()));
imageView.setImageBitmap(bitmap);
How to capture shadow or elevation of views in the layout of the activity in a screenshots.This code take a screenshot for the view but it's not showing the shadow of the viewsenter image description here
View screenView = parentMain;
screenView.buildDrawingCache();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getWidth() , screenView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
screenView.layout(0, 0, screenView.getLayoutParams().width, screenView.getLayoutParams().height);
screenView.draw(c);
screenView.setDrawingCacheEnabled(false);
fakeImgView.setImageBitmap(bitmap);
Even if we add the harware acceleration at activity level it does not provides any effect.
Appreciate any alternative approaches
the result
Try this.
CardView card = (CardView) findViewById(R.id.card);
Now just pass the card to captureScreenShot(). It returns the bitmap and save that bitmap saveImage().
You can pass any view Like RelativeLayout, LinearLayout etc any view can pass to captureScreenShot().
// Function which capture Screenshot
public Bitmap captureScreenShot(View view) {
/*
* Creating a Bitmap of view with ARGB_4444.
* */
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(bitmap);
Drawable backgroundDrawable = view.getBackground();
if (backgroundDrawable != null) {
backgroundDrawable.draw(canvas);
} else {
canvas.drawColor(Color.parseColor("#80000000"));
}
view.draw(canvas);
return bitmap;
}
// Function which Save image.
private void saveImage(Bitmap bitmap) {
File file = // Your Storage directory name + your filename
if (file == null) {
return;
}
try {
FileOutputStream fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Finally call this function like this.
saveImage(captureScreenShot(card));
Now set Your Image like this.
File file = new File(“yourImageFilePath”);
if(file.exists())
{
yourImageView.setImageURI(Uri.fromFile(file));
}
Note : If setImageURI() not working then you can use below code.
File file = new File(“yourImageFilePath”);
if(file.exists())
{
Bitmap bitmap = BitmapFactory.decodeFile(file.toString());
yourImageView.setImageBitmap(bitmap);
}
I'm trying to scale a bitmap to screen size and set it as wallpaper, but I've noticed on some devices(where the screen is a bit wider) the wallpaper is a little messy in height. Can someone help me scale the image in right way?
Here is my code:
int widthPx = getWindowManager().getDefaultDisplay()
.getWidth();
int heightPx = getWindowManager().getDefaultDisplay()
.getHeight();
// bmp = Bitmap.createScaledBitmap(bmp, widthPx, heightPx,
// true);
BitmapDrawable drawable = (BitmapDrawable) background
.getDrawable();
Bitmap bitmap = drawable.getBitmap();
try {
WallpaperManager myWallpaperManager = WallpaperManager
.getInstance(getApplicationContext());
Bitmap resizedBitmap = Bitmap.createScaledBitmap(
bitmap,
myWallpaperManager.getDesiredMinimumWidth(),
myWallpaperManager.getDesiredMinimumHeight(),
true);
myWallpaperManager.setWallpaperOffsetSteps(1, 1);
myWallpaperManager.suggestDesiredDimensions(widthPx,
heightPx);
myWallpaperManager.setBitmap(resizedBitmap);
Toast.makeText(BackgroundPreview.this,
getString(R.string.wallpaper_set),
Toast.LENGTH_SHORT).show();
finish();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(BackgroundPreview.this,
getString(R.string.error_setting_wallpaper),
Toast.LENGTH_SHORT).show();
}
I have a dialog with some TextViews and ImageViews inside. Now I want to share this with the Share Intent. Is this possible? Can I share a dialog or convert it first to a bitmap and then share it?
You can use this method.
public static Bitmap TakeBitmapFromDialog(View v, int width, int height) {
Bitmap b = Bitmap.createBitmap(width , height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
return b;
}
OR
simply use this.
View v1 = view.getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bm = v1.getDrawingCache();
BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
UPDATE:
if you don't want to use that then use this.
Bitmap cs = null;
view.setDrawingCacheEnabled(true);
view.buildDrawingCache(true);
cs = Bitmap.createBitmap(view.getDrawingCache());
Canvas canvas = new Canvas(cs);
view.draw(canvas);
canvas.save();
view.setDrawingCacheEnabled(false);
If you want to share that bitmap you need to insert in Media Images like,
String path = Images.Media.insertImage(getContentResolver(), cs,
"MyImage", null);
Uri file = Uri.parse(path);
Now for sharing,
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_STREAM, file);
startActivity(Intent.createChooser(sharingIntent,
"Share image using"));
Create a custom dialog separately using activity context and use it in a separate activity.