首先,我正在编写根应用程序,因此根权限不成问题。我搜索并搜索了很多代码,这些代码从来没有为我工作,这是我拼凑到目前为止,有些作品。当我说sorta我的意思是它使我/ sdcard/test.png上的图像,但该文件是0字节,显然无法查看。Android以编程方式拍摄屏幕截图
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的屏幕截图将不胜感激,谢谢!
===编辑===
以下是一切我使用的图像在我的SD卡是由并且不再0bytes但整个事情是黑色的就没有什么了。我已经将活动绑定到了我的搜索按钮,所以当我在手机上的某处我长时间按下搜索按钮时,它应该会拍摄一个屏幕截图,但我只是得到一个黑色图像?一切设置透明的所以我觉得应该抓住什么是在屏幕上,但我只是不断收到黑
清单
<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();
}
}
ķ感谢您的回答要看看你的问题明天此刻只是太累了。我还有另外一个问题,但是当我这样做时,怎么不叫根?并且这段代码是否仅仅是为了布局的屏幕截图而已,因为我想基本上在屏幕上显示屏幕上的内容,当它长时间按下时,它会将搜索键绑定到屏幕上。 – user577732
好的。它的工作很好。但是,如果我使用该代码来拍摄相机视图的屏幕截图,那么它不会起作用。 –