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.
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 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
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);
}
Main Activity
holder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String title = ((TextView) view.findViewById(R.id.recipe_title)).getText().toString();
String time = ((TextView) view.findViewById(R.id.time)).getText().toString();
String servings = ((TextView) view.findViewById(R.id.servings)).getText().toString();
String calories = ((TextView) view.findViewById(R.id.calories)).getText().toString();
ImageView thumbnail = (ImageView) view.findViewById(R.id.recipe_img);
Intent intent = new Intent(context, ActivityDetail.class);
intent.putExtra("RecipeTitle", title);
intent.putExtra("RecipeTotalTime", time);
intent.putExtra("RecipeServings", servings);
intent.putExtra("RecipeCalories", calories);
context.startActivity(intent);
}
});
Detail Activity
TextView time = (TextView)findViewById(R.id.time_detail);
TextView servings = (TextView)findViewById(R.id.servings_detail);
TextView calories = (TextView)findViewById(R.id.calories_detail);
ImageView thumbnail = (ImageView)findViewById(R.id.image_paralax);
time.setText(getIntent().getExtras().getString("RecipeTotalTime"));
servings.setText(getIntent().getExtras().getString("RecipeServings"));
calories.setText(getIntent().getExtras().getString("RecipeCalories"));
How can I send a thumbnail from the first Activity to second Activity. In the second `Activity, the thumbnail will be shown on an ImageView.
You should not try to send a image through a Intent. Rather it is better to send the Uri of the image.. And in the second activity load the image from the Uri.
I dont recommend you to pass bitmap to other activity, instead you should pass the string url and in the receiving activity you can load image from web or file.
if you really want to pass image using bitmap only then you have to Convert it to a Byte array before you add it to the intent, send it out, and decode.
Bitmap bmp = ((BitmapDrawable)thumbnail.getDrawable()).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Intent in1 = new Intent(this, Activity2.class);
in1.putExtra("image",byteArray);
Then in your receiving activity you will need to add following code to receive the byte array:
byte[] byteArray = getIntent().getByteArrayExtra("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
now you have a bitmap you can add setImageDrawable(new BitmapDrawable(mContext.getResources(), bmp));
Thats all you need to do.
It is not a good thing to send and image into an intent, you should try to send the uri of the image instead.
But if you really want to do it, so you can get the Bitmap drawable from the image view:
Bitmap bitmap = ((BitmapDrawable) thumbnail.getDrawable()).getBitmap();
Since the bitmap is a Parcelable you can send it directly as an Extra:
intent.putExtra("RecipeThumbnail", bitmap);
Yes, it is possible.. but not recommended
1)First you have to make bitmap and bytes array from that bitmap
Bitmap bitmap = ((BitmapDrawable)thumbnail.getDrawable()).getBitmap();
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] image=stream.toByteArray();
2) then you can pass it using intent
intent.putExtra("Thumbnail Image", image);
3) In receiving activity,
byte[] = getIntent.getExtra("Thumbnail Image");
Bitmap bmp = BitmapFactory.decodeByteArray(byte, 0, byteArray.length);
imageView.setImageBitmap(bmp);
Do it faster like a master :)
pass the id of the image, you have it in your resources, so why seralisin so much info if the activity b can just loading it by knowing the id...
Intent intent = new Intent(context, ActivityDetail.class);
...
intent.putExtra("imageId", R.id.recipe_img);
and on the other activity will be enough doing:
ImageView thumbnail = (ImageView) view.findViewById(yourREceivedImageId);
I'm trying to save a background from a ViewPagerParallax which can be found here : link
When i move, the background changes, and i want to take this "part" of the background and pass it to another activity.
To pass it from one to another activty i can :
Intent intent = new Intent(context, Activity2.class);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
//Bitmap bmp = pager.getSavedBitmap().getBitmap();
Bitmap bmp = pager.getBitmap();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
intent.putExtra("image",byteArray);
context.startActivity(intent);
byte[] byteArray = getIntent().getByteArrayExtra("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
#SuppressWarnings("deprecation")
Drawable d = new BitmapDrawable(bmp);
layout.setBackground(d);
But know to take the part of the background i want is difficult, i'm trying to take it from the onDraw method like this :
canvas.drawBitmap(saved_bitmap, src, dst, null);
if(canvas != null){
my_bitmap = new BitmapDrawable();
my_bitmap.draw(canvas);
}
But when i use getBitmap :
public Bitmap getBitmap(){
return my_bitmap.getBitmap();
}
the image is not scaled like it was in the first activity.
Doesn't this post have a similar issue? Android Save Canvas into Bitmap
Perhaps it might help.