简体   繁体   English

如何以编程方式在android设备上拍摄屏幕快照?

[英]How to take screen shot on android device programmatically?

I want to take the screen shot of the device. 我想拍摄设备的屏幕截图。 But my coding is take on only my application screen only..I need to take screenshot view screen of the device..and tell how to run on service. 但是我的编码仅在我的应用程序屏幕上显示。我需要在设备的屏幕快照屏幕上显示..并告诉如何在服务上运行。 time interval should be 500ms for taking screen shot. 屏幕截图的时间间隔应为500ms。

Here is screen shot code: 这是屏幕截图代码:

Bitmap bitmap;
View v1 = MyView.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

You Can chek this 你可以che一下

View view =  findViewById(R.id.rellayout);
        view.getRootView();
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) 
        {
            File picDir  = new File(Environment.getExternalStorageDirectory()+ "/name");
            if (!picDir.exists())
            {
                picDir.mkdir();
            }
            view.setDrawingCacheEnabled(true);
            view.buildDrawingCache(true);
            Bitmap bitmap = view.getDrawingCache();
//          Date date = new Date();
            String fileName = "mylove" + ".jpg";
            File picFile = new File(picDir + "/" + fileName);
            try 
            {
                picFile.createNewFile();
                FileOutputStream picOut = new FileOutputStream(picFile);
                bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), (int)(bitmap.getHeight()/1.2));//Optional
                boolean saved = bitmap.compress(CompressFormat.JPEG, 100, picOut);
                if (saved) 
                {
                    Toast.makeText(getApplicationContext(), "Image saved to your device Pictures "+ "directory!", Toast.LENGTH_SHORT).show();
                } else 
                {
                    //Error
                }
                picOut.close();
            } 
            catch (Exception e) 
            {
                e.printStackTrace();
            }
            view.destroyDrawingCache();
        } else {


        }

if you are able to capture screen shot of your application then simply Create a Receiver and receive the event on what you want to capture screen shot eg. 如果您能够捕获应用程序的屏幕截图,则只需创建一个Receiver并接收有关您要捕获屏幕截图的事件,例如。

  <receiver android:name=".VolumeChangeReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.MEDIA_BUTTON" />
        </intent-filter>
        <intent-filter android:priority="100" >
            <action android:name="android.intent.action.MEDIA_BUTTON" />
            <action android:name="android.media.VOLUME_CHANGED_ACTION" />
        </intent-filter>
    </receiver>

now inside receiver you can right your code ie 现在在接收器中,您可以纠正您的代码,即

 Bitmap bitmap;
 View v1 = MyView.getRootView();
 v1.setDrawingCacheEnabled(true);
 bitmap = Bitmap.createBitmap(v1.getDrawingCache());
 v1.setDrawingCacheEnabled(false);

same you can do with power button it is all your wish on what action you want to capture screen shot, 使用电源按钮也可以做到这一点,这就是您想要捕获屏幕快照的所有操作,

try it might be helpful 尝试它可能会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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