Android:Screencap直接到字节 – 跳过保存到文件
我使用以下代码在root设备上截取屏幕截图:
sh = Runtime.getRuntime().exec("su", null,null); os = sh.getOutputStream(); os.write(("/system/bin/screencap -p " + "/sdcard/img.jpg" + "\n").getBytes("ASCII")); os.flush(); os.close(); sh.waitFor(); myBitmap = BitmapFactory.decodeFile("/sdcard/img.jpg");
由于我想经常截取屏幕截图,我不想先将其保存到文件然后读取字节。 是否有更好的方法直接获取屏幕截图位图? 我只是想消除这种不必要的延迟。 非常感谢!
更新:
可以在此处查看screencap.cpp代码。
另外,它提到了这个:
"usage: %s [-hp] [-d display-id] [FILENAME]\n" " -h: this message\n" " -p: save the file as a png.\n" " -d: specify the display id to capture, default %d.\n" "If FILENAME ends with .png it will be saved as a png.\n" "If FILENAME is not given, the results will be printed to stdout.\n"
请帮帮我。 谢谢!
除非您使用的是8.1 Pixel2 XL,否则会出现安全例外情况。
import android.graphics.Bitmap; import android.graphics.BitmapFactory; import java.io.IOException; import java.io.OutputStreamWriter; public class BitmapScreencap { public final static BitmapScreencap Get = new BitmapScreencap(); private BitmapScreencap() { } public Bitmap Screen() { try { Process process = Runtime.getRuntime().exec("su"); OutputStreamWriter outputStream = new OutputStreamWriter(process.getOutputStream()); outputStream.write("/system/bin/screencap -p\n"); outputStream.flush(); Bitmap screen = BitmapFactory.decodeStream(process.getInputStream()); outputStream.write("exit\n"); outputStream.flush(); outputStream.close(); return screen; } catch (IOException e) { e.printStackTrace(); } return null; } }
放置在代码中的任何位置以获取位图
BitmapScreencap.Get.Screen();
- 无法在android中的服务中启动计时器
- java.lang.OutOfMemoryError:在OOM之前分配分配失败
- 适用于Android的语音生物识别技术
- Retrofit2将身体贴为Json
- 找不到适合ArrayAdapter的构造函数(MainListActivity.GetBlogPostsTask,int,String )
- 如果类实现了更新的接口,Android推荐的安全支持更新api的方法有错误。 为什么?
- 从Activity上下文外部调用startActivity()需要FLAG_ACTIVITY_NEW_TASK标志。 这真的是你想要的吗?
- 如何从doinbackground()执行其他线程
- Android中Thread.currentThread()。getId()和Process.myTid()之间的区别