安卓取截屏编程安卓取截屏

2023-09-12 00:11:34 作者:日落收集铺

首先我写一个根应用程序,以便root权限都没有问题。我已经搜查,搜查,发现了大量的code,从来没有在这里工作对我来说是我所拼凑迄今八九不离十工作。当我说八九不离十我的意思是它使图像在我的/sdcard/test.png但该文件是0字节,显然不能进行查看。

 公共类截图延伸活动{

查看内容;

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.blank);
    内容= findViewById(R.id.blankview);
    getScreen();
}

私人无效getScreen(){
    点阵位图= content.getDrawingCache();
    档案文件=新的文件(/ SD卡/ test.png);
    尝试
    {
        file.createNewFile();
        FileOutputStream中的ostream =新的FileOutputStream(文件);
        bitmap.com preSS(比较pressFormat.PNG,100,ostream的);
        ostream.close();
    }
    赶上(例外五)
    {
        e.printStackTrace();
    }
}
}
 

我如何可以采取一个屏幕截图机器人通过code会大大AP preciated感谢您的帮助!

===编辑===

下面就是一切我使用的图像是由我的SD卡,不再0bytes但整个事情是黑的就没有什么了。我约束活动,我的搜索​​按钮,这样,当我在我的手机一些地方我长preSS搜索,它是应该采取一个屏幕快照,但我只是得到一个黑色的形象?一切都设置透明的所以我觉得应该抓住什么是在屏幕上,但我还是想继续让黑

清单

 <活动机器人:名称=。extras.ScreenShot
    机器人:主题=@安卓风格/ Theme.Translucent.NoTitleBar
    机器人:noHistory =真正的>
    <意向滤光器>
        <作用机器人:名称=android.intent.action.SEARCH_LONG_ $ P $干燥综合征/>
        <类机器人:名称=android.intent.category.DEFAULT/>
    &所述; /意图滤光器>
    < /活性GT;
 

XML

 < XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
  的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:方向=垂直
  机器人:layout_width =match_parent
  机器人:layout_height =match_parent
  机器人:后台=#00000000
  机器人:ID =@ + ID / screenRoot>
< / LinearLayout中>
 
通用的安卓智能电视截屏及图片目录文件获取方法

截图类

 公共类截图延伸活动{

查看内容;

@覆盖
保护无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.screenshot);
    内容= findViewById(R.id.screenRoot);
    ViewTreeObserver VTO = content.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(新OnGlobalLayoutListener(){
      @覆盖
      公共无效onGlobalLayout(){
        。content.getViewTreeObserver()removeGlobalOnLayoutListener(本);
        getScreen();
      }
    });
}

私人无效getScreen(){
    查看查看=内容;
    视图V = view.getRootView();
    v.setDrawingCacheEnabled(真正的);
    位图B = v.getDrawingCache();
    。字符串抽= Environment.getExternalStorageDirectory()的toString();
    文件mypath中=新的文件(抽,test.jpg放在);
    FileOutputStream中FOS = NULL;
    尝试 {
        FOS =新的FileOutputStream(mypath中);
        b.com preSS(Bitmap.Com pressFormat.JPEG,100,FOS);
        fos.flush();
        fos.close();
        MediaStore.Images.Media.insertImage(getContentResolver(),B,屏幕,屏幕);
    }赶上(FileNotFoundException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }赶上(例外五){
        // TODO自动生成的catch块
        e.printStackTrace();
    }
    完();
}
}
 

解决方案

在这里你去...我用这样的:

 视图V = view.getRootView();
v.setDrawingCacheEnabled(真正的);
位图B = v.getDrawingCache();
。字符串抽= Environment.getExternalStorageDirectory()的toString();
文件mypath中=新的文件(抽,的getString(R.string.free_tiket)+JPG);
FileOutputStream中FOS = NULL;
尝试 {
    FOS =新的FileOutputStream(mypath中);
    b.com preSS(Bitmap.Com pressFormat.JPEG,100,FOS);
    fos.flush();
    fos.close();
    MediaStore.Images.Media.insertImage(getContentResolver(),B,
                                         屏幕,屏幕);
}赶上(FileNotFoundException异常E){
    // TODO自动生成的catch块
    e.printStackTrace();
}赶上(例外五){
    // TODO自动生成的catch块
    e.printStackTrace();
}
 

v IZ根布局......只是点;)))

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();
    }
}
}

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

===EDIT===

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

Manifest

<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>

Screenshot class

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 root layout...just to point ;)))

相关推荐