android 黑屏拍照,Android拍摄表面视图的屏幕截图显示黑屏

我试图通过代码拍摄我的游戏的屏幕截图,并通过意图分享它.我可以做这些事情,但屏幕截图总是显得黑色.以下是与分享屏幕截图相关的代码:

View view = MainActivity.getView();

view.setDrawingCacheEnabled(true);

Bitmap screen = Bitmap.createBitmap(view.getDrawingCache(true));

.. save Bitmap

这是在MainActivity:

view = new GameView(this);

view.setLayoutParams(new RelativeLayout.LayoutParams(

RelativeLayout.LayoutParams.FILL_PARENT,

RelativeLayout.LayoutParams.FILL_PARENT));

public static SurfaceView getView() {

return view;

}

而视图本身:

public class GameView extends SurfaceView implements SurfaceHolder.Callback {

private static SurfaceHolder surfaceHolder;

...etc

这就是我如何绘制一切:

Canvas canvas = surfaceHolder.lockCanvas(null);

if (canvas != null) {

Game.draw(canvas);

...

好的,根据一些答案,我已经构建了:

public static void share() {

Bitmap screen = GameView.SavePixels(0, 0, Screen.width, Screen.height);

Calendar c = Calendar.getInstance();

Date d = c.getTime();

String path = Images.Media.insertImage(

Game.context.getContentResolver(), screen, "screenShotBJ" + d

+ ".png", null);

System.out.println(path + " PATH");

Uri screenshotUri = Uri.parse(path);

final Intent emailIntent = new Intent(

android.content.Intent.ACTION_SEND);

emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

emailIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);

emailIntent.setType("image/png");

Game.context.startActivity(Intent.createChooser(emailIntent,

"Share High Score:"));

}

Gameview包含以下方法:

public static Bitmap SavePixels(int x, int y, int w, int h) {

EGL10 egl = (EGL10) EGLContext.getEGL();

GL10 gl = (GL10) egl.eglGetCurrentContext().getGL();

int b[] = new int[w * (y + h)];

int bt[] = new int[w * h];

IntBuffer ib = IntBuffer.wrap(b);

ib.position(0);

gl.glReadPixels(x, 0, w, y + h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib);

for (int i = 0, k = 0; i < h; i++, k++) {

for (int j = 0; j < w; j++) {

int pix = b[i * w + j];

int pb = (pix >> 16) & 0xff;

int pr = (pix << 16) & 0x00ff0000;

int pix1 = (pix & 0xff00ff00) | pr | pb;

bt[(h - k - 1) * w + j] = pix1;

}

}

Bitmap sb = Bitmap.createBitmap(bt, w, h, Bitmap.Config.ARGB_8888);

return sb;

}

屏幕截图仍然是黑色.也许我正在保存的方式有问题吗?

我尝试了几种不同的方法来采取屏幕截图,但是没有一个是有效的:

上面的代码显示的是最常见的代码.但它似乎不起作用

这是使用SurfaceView的问题吗?如果是这样,为什么view.getDrawingCache(true)甚至存在,如果我不能使用它,我该如何解决这个问题?

工作代码:

public static void share() {

// GIVES BLACK SCREENSHOT:

Calendar c = Calendar.getInstance();

Date d = c.getTime();

Game.update();

Bitmap.Config conf = Bitmap.Config.RGB_565;

Bitmap image = Bitmap.createBitmap(Screen.width, Screen.height, conf);

Canvas canvas = GameThread.surfaceHolder.lockCanvas(null);

canvas.setBitmap(image);

Paint backgroundPaint = new Paint();

backgroundPaint.setARGB(255, 40, 40, 40);

canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(),

backgroundPaint);

Game.draw(canvas);

Bitmap screen = Bitmap.createBitmap(image, 0, 0, Screen.width,

Screen.height);

canvas.setBitmap(null);

GameThread.surfaceHolder.unlockCanvasAndPost(canvas);

String path = Images.Media.insertImage(

Game.context.getContentResolver(), screen, "screenShotBJ" + d

+ ".png", null);

System.out.println(path + " PATH");

Uri screenshotUri = Uri.parse(path);

final Intent emailIntent = new Intent(

android.content.Intent.ACTION_SEND);

emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

emailIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);

emailIntent.setType("image/png");

Game.context.startActivity(Intent.createChooser(emailIntent,

"Share High Score:"));

}

谢谢.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值