简体   繁体   中英

Android, Java: Share a dialog

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

使用活动上下文分别创建一个自定义对话框,并在单独的活动中使用它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM