gpt4 book ai didi

android - 如何在android中截取应用程序屏幕的屏幕截图?

转载 作者:太空宇宙 更新时间:2023-11-03 11:11:55 24 4
gpt4 key购买 nike

我正在开发一个应用程序,我必须在其中截取应用程序屏幕的屏幕截图现在我使用下面的代码它不起作用。我是空位图图片

Process sh = Runtime.getRuntime().exec("su", null,null);
OutputStream os = sh.getOutputStream();
os.write(("/system/bin/screencap -p " + "/sdcard/"+ "bs_score_img" +".png").getBytes("ASCII"));
os.flush();
os.close();
sh.waitFor();

String screenShot = Environment.getExternalStorageDirectory().toString()+"/bs_score_img.png";
Log.i("TAG:Score: screenShot path=", screenShot);
Bitmap bmp = BitmapFactory.decodeFile(new File(screenShot).getAbsolutePath());

最佳答案

试试这个:

Process sh = Runtime.getRuntime().exec("su", null,null);

OutputStream os = sh.getOutputStream();
os.write(("/system/bin/screencap -p " + "/sdcard/img.png").getBytes("ASCII"));
os.flush();

os.close();
sh.waitFor();

then read img.png as bitmap and convert it jpg as follows

Bitmap screen = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+
File.separator +"img.png");

//my code for saving
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
screen.compress(Bitmap.CompressFormat.JPEG, 15, bytes);

//you can create a new file name "test.jpg" in sdcard folder.

File f = new File(Environment.getExternalStorageDirectory()+ File.separator + "test.jpg");
f.createNewFile();
//write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
// remember close de FileOutput

fo.close();

Google 有一个库,您可以使用它在不 root 的情况下截取屏幕截图,我试过了,但我确信它会尽快耗尽内存。

尝试 http://code.google.com/p/android-screenshot-library/

或者试试这个:

View v1 = L1.getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bm = v1.getDrawingCache();
BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
image = (ImageView) findViewById(R.id.screenshots);
image.setBackgroundDrawable(bitmapDrawable);

关于android - 如何在android中截取应用程序屏幕的屏幕截图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22979394/

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