机器人:采取截图和分享 [英] android:take screenshot and share it

查看:123
本文介绍了机器人:采取截图和分享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做Android的应用程序中,我不得不采取截图我的活动之一邮件作为附件。我想我的链接堆栈溢出,但没有找到任何有关。我的问题是取当前网页的屏幕截图,然后通过邮寄或电子邮件或蓝牙或Twitter或Facebook分享。我也试过的如何以编程方式采取在Android?链接的截图,但无法找到任何有关。任何有关这种帮助将AP preciated。 我的code是如下:

 活动AV = Hello.this; //其中,Hello是活动名称

 @覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    MenuInflater充气= getMenuInflater();
    inflater.inflate(R.menu.menuselected1,菜单);
    返回true;
}

@覆盖
公共布尔onOptionsItemSelected(菜单项项){
    //处理项目选择
    开关(item.getItemId()){
        案例R.id.ScreenShot:

                尝试{
                    takeScreenShot(AV); // AV是实例打招呼
                    }
                赶上(例外五){
           的System.out.println(E);
        }

     返回true;

        默认:
            返回super.onOptionsItemSelected(项目);
    }
}

私有静态无效savePic(位​​图B,串strFileName){
    FileOutputStream中FOS = NULL;
    尝试 {
    FOS =新的FileOutputStream(strFileName);
    如果(NULL!= FOS){
    b.com preSS(Bitmap.Com pressFormat.PNG,90,FOS);
    的System.out.println(b为:+ b)的;
    fos.flush();
    fos.close();
    }
    }赶上(FileNotFoundException异常E){
    e.printStackTrace();
    }赶上(IOException异常E){
    e.printStackTrace();
    }
    }

    公共静态无效的拍摄(活动一,字符串二){
    // savePic(takeScreenShot(a)中,SD卡/ xx.png);
    savePic(takeScreenShot(A),B);
    }


私有静态位图takeScreenShot(活动活动){
    查看查看= activity.getWindow()getDecorView()。
    view.setDrawingCacheEnabled(真正的);
    view.buildDrawingCache();
    位图B1 = view.getDrawingCache();
    矩形框=新的矩形();
    。activity.getWindow()getDecorView()getWindowVisibleDisplayFrame(帧)。
    INT statusBarHeight = frame.top;
    INT宽度= activity.getWindowManager()getDefaultDisplay()的getWidth()。
    INT高= activity.getWindowManager()。getDefaultDisplay()
    .getHeight();
    //位图B = Bitmap.createBitmap(B1,0,25,320,455);
    位图B = Bitmap.createBitmap(B1,0,statusBarHeight,宽度,高度
      -  statusBarHeight);
    view.destroyDrawingCache();
    返回b;
        }
 

解决方案

试试这个采取当前活动的截图:

Android 2.2的:

 私有静态位图takeScreenShot(活动活动)
{
    查看查看= activity.getWindow()getDecorView()。
    view.setDrawingCacheEnabled(真正的);
    view.buildDrawingCache();
    位图B1 = view.getDrawingCache();
    矩形框=新的矩形();
    。activity.getWindow()getDecorView()getWindowVisibleDisplayFrame(帧)。
    INT statusBarHeight = frame.top;
    INT宽度= activity.getWindowManager()getDefaultDisplay()的getWidth()。
    INT高= activity.getWindowManager()getDefaultDisplay()的getHeight()。

    位图B = Bitmap.createBitmap(B1,0,statusBarHeight,宽度,高度 -  statusBarHeight);
    view.destroyDrawingCache();
    返回b;
}
私有静态无效savePic(位​​图B,串strFileName)
{
    FileOutputStream中FOS = NULL;
    尝试
    {
        FOS =新的FileOutputStream(strFileName);
        如果(NULL!= FOS)
        {
            b.com preSS(Bitmap.Com pressFormat.PNG,90,FOS);
            fos.flush();
            fos.close();
        }
    }
    赶上(FileNotFoundException异常E)
    {
        e.printStackTrace();
    }
    赶上(IOException异常E)
    {
        e.printStackTrace();
    }
}
 

I am making an application in android in which I have to take screenshot of one of my activities and mail it as attachment. I tried my links on stack overflow but couldn't find anything relevant. My problem is take screenshot of current page and then share it via mail or email or Bluetooth or twitter or Facebook. I also tried How to programatically take a screenshot on Android? link but couldn't find anything relevant. Any help regarding this will be appreciated. My code is as follows:

Activity av=Hello.this; //where Hello is Activity name

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menuselected1, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    switch (item.getItemId()) {
        case R.id.ScreenShot:

                try{
                    takeScreenShot(av); // av is instance of hello
                    }
                catch (Exception e) {
           System.out.println(e);
        }

     return true;

        default:
            return super.onOptionsItemSelected(item);
    }
}

private static void savePic(Bitmap b, String strFileName) {
    FileOutputStream fos = null;
    try {
    fos = new FileOutputStream(strFileName);
    if (null != fos) {
    b.compress(Bitmap.CompressFormat.PNG, 90, fos);
    System.out.println("b is:"+b);
    fos.flush();
    fos.close();
    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    public static void shoot(Activity a,String b) {
    //savePic(takeScreenShot(a), "sdcard/xx.png");
    savePic(takeScreenShot(a), b);
    }


private static Bitmap takeScreenShot(Activity activity) {
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap b1 = view.getDrawingCache();
    Rect frame = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
    int statusBarHeight = frame.top;
    int width = activity.getWindowManager().getDefaultDisplay().getWidth();
    int height = activity.getWindowManager().getDefaultDisplay()
    .getHeight();
    // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
    Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height
     - statusBarHeight);
    view.destroyDrawingCache();
    return b;
        }

解决方案

Try this for taking screenshot of current Activity:

Android 2.2 :

private static Bitmap takeScreenShot(Activity activity)
{
    View view = activity.getWindow().getDecorView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap b1 = view.getDrawingCache();
    Rect frame = new Rect();
    activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
    int statusBarHeight = frame.top;
    int width = activity.getWindowManager().getDefaultDisplay().getWidth();
    int height = activity.getWindowManager().getDefaultDisplay().getHeight();

    Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height  - statusBarHeight);
    view.destroyDrawingCache();
    return b;
}
private static void savePic(Bitmap b, String strFileName)
{
    FileOutputStream fos = null;
    try
    {
        fos = new FileOutputStream(strFileName);
        if (null != fos)
        {
            b.compress(Bitmap.CompressFormat.PNG, 90, fos);
            fos.flush();
            fos.close();
        }
    }
    catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}

这篇关于机器人:采取截图和分享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆