Android 以编程方式截取屏幕截图 [英] Android take screen shot programmatically

查看:36
本文介绍了Android 以编程方式截取屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我正在编写一个 root 应用程序,因此 root 权限没有问题.我搜索并搜索并发现了很多从未对我有用过的代码,这是我迄今为止拼凑起来的,并且可以正常工作.当我说 sorta 时,我的意思是它在我的/sdcard/test.png 上制作了一个图像,但是该文件是 0 字节,显然无法查看.

First off i am writing a root app so root permissions are no issue. I've searched and searched and found a lot of code that never worked for me here is what i've pieced together so far and sorta works. When i say sorta i mean it makes an image on my /sdcard/test.png however the file is 0 bytes and obviously can't be viewed.

public class ScreenShot extends Activity{

View content;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.blank);
    content = findViewById(R.id.blankview);
    getScreen();
}

private void getScreen(){
    Bitmap bitmap = content.getDrawingCache();
    File file = new File("/sdcard/test.png");
    try 
    {
        file.createNewFile();
        FileOutputStream ostream = new FileOutputStream(file);
        bitmap.compress(CompressFormat.PNG, 100, ostream);
        ostream.close();
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }
}
}

有关如何通过代码在 android 中截屏的任何帮助将不胜感激,谢谢!

Any help on how i can take a screen shot in android via code would be greatly appreciated thank you!

===编辑===

以下是我使用的所有内容,图像是在我的 sdcard 上制作的,不再是 0bytes,但整个东西都是黑色的,上面什么也没有.我已将活动绑定到我的搜索按钮,所以当我在手机上的某个地方时,我长按搜索,它应该进行屏幕截图,但我只得到一个黑色图像?一切都设置为透明,所以我认为它应该抓住屏幕上的任何内容,但我只是不断变黑

The following is everything i'm using the image is made on my sdcard and is no longer 0bytes but the entire thing is black there is nothing on it. I've bound the activity to my search button so when i'm some where on my phone i long press search and it is supposed to take a screen shot but i just get a black image? Everything is set transparent so i'd think it should grab whatever is on the screen but i just keep getting black

清单

<activity android:name=".extras.ScreenShot"
    android:theme="@android:style/Theme.Translucent.NoTitleBar"
    android:noHistory="true" >
    <intent-filter>
        <action android:name="android.intent.action.SEARCH_LONG_PRESS" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    </activity>

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#00000000"
  android:id="@+id/screenRoot">    
</LinearLayout>

截图类

public class ScreenShot extends Activity{

View content;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.screenshot);
    content = findViewById(R.id.screenRoot);
    ViewTreeObserver vto = content.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
      @Override
      public void onGlobalLayout() {
        content.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        getScreen();
      }
    });
}

private void getScreen(){
    View view = content;
    View v = view.getRootView();
    v.setDrawingCacheEnabled(true);
    Bitmap b = v.getDrawingCache();             
    String extr = Environment.getExternalStorageDirectory().toString();
    File myPath = new File(extr, "test.jpg");
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(myPath);
        b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
        MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen");
    }catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finish();
}
}

推荐答案

给你...我用过这个:

Here you go...I used this:

View v = view.getRootView();
v.setDrawingCacheEnabled(true);
Bitmap b = v.getDrawingCache();
String extr = Environment.getExternalStorageDirectory().toString();
File myPath = new File(extr, getString(R.string.free_tiket)+".jpg");
FileOutputStream fos = null;
try {
    fos = new FileOutputStream(myPath);
    b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    fos.flush();
    fos.close();
    MediaStore.Images.Media.insertImage( getContentResolver(), b, 
                                         "Screen", "screen");
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

v iz 根布局...只是为了指出 ;)))

v iz root layout...just to point ;)))

这篇关于Android 以编程方式截取屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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