Android 中的黑色屏幕截图 [英] Screenshot Black in Android

查看:83
本文介绍了Android 中的黑色屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究如何在 android 中以编程方式截取屏幕截图,但是当它截取屏幕截图时,我得到了一个工具栏和黑屏,而不是屏幕上实际显示的内容.

I've been working out how to take a screenshot programmatically in android, however when it screenshots I get a toolbar and black screen captured instead of what is actually on the screen.

我还尝试在我为谷歌地图创建的自定义 InfoWindow 布局中对特定的 TextView 进行屏幕截图.但这会在下面的第二行创建一个空指针异常.

I've also tried to screenshot a particular TextView within the custom InfoWindow layout I created for the google map. But that creates a null pointer exception on the second line below.

TextView v1 = (TextView)findViewById(R.id.tv_code);
v1.setDrawingCacheEnabled(true);

无论如何,是否可以在不安装android屏幕截图库的情况下实际截取屏幕上的内容,或者在自定义InfoWindow布局中截取TextView

Is there anyway to either actually screenshot what is on the screen without installing android screenshot library or to screenshot a TextView within a custom InfoWindow layout

这是我的截图方法:

/**
 * Method to take a screenshot programmatically
 */
private void takeScreenshot(){
    try {
        //TextView I could screenshot instead of the whole screen:
        //TextView v1 = (TextView)findViewById(R.id.tv_code);

        View v1 = getWindow().getDecorView().getRootView();
        v1.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);


        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
        File f = new File(Environment.getExternalStorageDirectory()
                + File.separator + "test.jpg");

        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
        fo.flush();
        fo.close();

        MediaStore.Images.Media.insertImage(getContentResolver(), f.getAbsolutePath(), f.getName(), f.getName());
        Log.d("debug", "Screenshot saved to gallery");

        Toast.makeText(HuntActivity.this,"Code Saved!",Toast.LENGTH_LONG).show();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

我已将方法更改为来源提供的方法

I have changed the method to the one provided from the source

如何以编程方式拍摄/合并 Google 地图 v2 和 xml 布局的屏幕截图?

但是它没有截图任何东西.

However it does not screenshot anything.

public void captureMapScreen() {
    GoogleMap.SnapshotReadyCallback callback = new GoogleMap.SnapshotReadyCallback() {

        @Override
        public void onSnapshotReady(Bitmap snapshot) {
            try {
                View mView = getWindow().getDecorView().getRootView();
                mView.setDrawingCacheEnabled(true);
                Bitmap backBitmap = mView.getDrawingCache();
                Bitmap bmOverlay = Bitmap.createBitmap(
                        backBitmap.getWidth(), backBitmap.getHeight(),
                        backBitmap.getConfig());

                Canvas canvas = new Canvas(bmOverlay);
                canvas.drawBitmap(backBitmap, 0, 0, null);
                canvas.drawBitmap(snapshot, new Matrix(), null);

                FileOutputStream out = new FileOutputStream(
                        Environment.getExternalStorageDirectory()
                                + "/"
                                + System.currentTimeMillis() + ".jpg");

                bmOverlay.compress(Bitmap.CompressFormat.JPEG, 90, out);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    mMap.snapshot(callback);
}

推荐答案

我已经想通了!

/**
 * Method to take a screenshot programmatically
 */
private void takeScreenshot(){
    GoogleMap.SnapshotReadyCallback callback = new GoogleMap.SnapshotReadyCallback() {
        @Override
        public void onSnapshotReady(Bitmap bitmap) {
            Bitmap b = bitmap;
            String timeStamp = new SimpleDateFormat(
                    "yyyyMMdd_HHmmss", Locale.getDefault())
                    .format(new java.util.Date());

            String filepath = timeStamp + ".jpg";

            try{
                OutputStream fout = null;
                fout = openFileOutput(filepath,MODE_WORLD_READABLE);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
                fout.flush();
                fout.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            saveImage(filepath);
        }
    };
    mMap.snapshot(callback);
}

/**
 * Method to save the screenshot image
 * @param filePath  the file path
 */
public void saveImage(String filePath)
{
    File file = this.getFileStreamPath(filePath);

    if(!filePath.equals(""))
    {
        final ContentValues values = new ContentValues(2);
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
        values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
        final Uri contentUriFile = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        Toast.makeText(HuntActivity.this,"Code Saved to files!",Toast.LENGTH_LONG).show();
    }
    else
    {
        System.out.println("ERROR");
    }
}

我已经修改了此链接中的代码,因此它不会共享,而只是保存图像.

I have adapted the code from this link so it doesn't share and instead just saves the image.

截取 GoogleMap Android API V2 的屏幕截图

感谢大家的帮助

这篇关于Android 中的黑色屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆