Вопрос

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top