gpt4 book ai didi

Android以编程方式截屏

转载 作者:IT王子 更新时间:2023-10-28 23:49:08 25 4
gpt4 key购买 nike

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

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 中截屏的帮助将不胜感激,谢谢!

===编辑===

以下是我正在使用的所有内容图像是在我的 sdcard 上制作的,不再是 0bytes,但整个东西都是黑色的,上面什么都没有。我已经将 Activity 绑定(bind)到我的搜索按钮,所以当我在手机上的某个位置时,我长按搜索,它应该截屏但我只是得到一个黑色图像?一切都设置为透明,所以我认为它应该捕获屏幕上的任何东西,但我只是不断变黑

list

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

最佳答案

给你...我用这个:

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 根布局...只是指出 ;)))

关于Android以编程方式截屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7762643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com