我想使用android源代码在android4.0.3中捕获屏幕。它可以正确编译。运行时报错:Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1).
debug在cpp文件中发现调用composer->captureScreen
函数,返回NO_ERROR值,但是heap为null,w和h为0,所以调用heap->getBase()时出错。我对此感到困惑。
{
const char *file_path = env->GetStringUTFChars(file, NULL);
const String16 name("SurfaceFlinger");
sp<ISurfaceComposer> composer;
getService(name, &composer);
sp<IMemoryHeap> heap;
uint32_t w, h;
PixelFormat f;
status_t err = composer->captureScreen(0, &heap, &w, &h, &f,0,0,0,INT_MAX);///*for 4.1.3*/,0,0
if (err != NO_ERROR) {
LOGE("screen capture failed: %s\n", strerror(-err));
exit(0);
}
LOGD("screen capture success: w=%u, h=%u, pixels=%p\n",
w, h, heap->getBase());
LOGD("saving file as PNG in %s ...\n", file_path);
SkBitmap b;
b.setConfig(SkBitmap::kARGB_8888_Config, w, h);
b.setPixels(heap->getBase());
SkImageEncoder::EncodeFile(file_path, b,
SkImageEncoder::kPNG_Type, SkImageEncoder::kDefaultQuality);
return file;
}
编辑:我添加了
<uses-permission android:name="android.permission.READ_FRAME_BUFFER" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在清单中。