Android 谷歌地图快照截图是黑色的

问题描述 投票:0回答:1

我有一个问题,对于我正在做的一些测试,我需要对使用 Google 地图的应用程序进行屏幕截图。当我尝试截取屏幕截图或快照时,所有元素都会出现在屏幕截图中,但 Google 地图所在的位置除外,它显示为黑色。

在此输入图片描述

我尝试关注这些其他文章,我发现他们提到了类似的错误:

<截取 GoogleMap Android API V2 的屏幕截图 >

<谷歌地图截图黑色>

这是我的按钮代码和屏幕截图:

click_screenshot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(MainActivity.this, "bot_screenshot", Toast.LENGTH_SHORT).show();


               

                supportMapFragment.getMapAsync(new OnMapReadyCallback() {
                    @Override
                    public void onMapReady(@NonNull GoogleMap googleMap) {
                        Toast.makeText(MainActivity.this, "Test Snapshot Google Maps 2 X", Toast.LENGTH_SHORT).show();
                        GoogleMap.SnapshotReadyCallback callback = new GoogleMap.SnapshotReadyCallback() {
                            Bitmap bitmap;
                            @Override
                            public void onSnapshotReady(Bitmap snapshot) {
                                //Toast.makeText(MainActivity.this, "Test Snapshot Google Maps 2 Y", Toast.LENGTH_SHORT).show();
                                bitmap = snapshot;
                                try {
                                    Toast.makeText(MainActivity.this, "Test Snapshot Google Maps 2 Z", Toast.LENGTH_SHORT).show();

                                    Bitmap screenshot = capturescreenshot(linearLayout1);
                                    imageView_scre.setImageBitmap(screenshot);
                                    storeimageasjpeg(screenshot);

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }/**/
                               

                            }
                        };

                        googleMap.snapshot(callback); //si funciona


                    }
                });


 public Bitmap capturescreenshot(View view){
        Toast.makeText(MainActivity.this, "capturescreenshot", Toast.LENGTH_SHORT).show();
        Bitmap returnBitnap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(returnBitnap);
        Drawable bgdrawable = view.getBackground();
        if (bgdrawable != null){
            bgdrawable.draw(canvas);
        }
        else {
            canvas.drawColor(Color.WHITE);
        }
        view.draw(canvas);
        return returnBitnap;
    }

    public void storeimageasjpeg(Bitmap bitmap){
        Toast.makeText(MainActivity.this, "storeimageasjpeg", Toast.LENGTH_SHORT).show();
        OutputStream outst;
        try {
                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
                    ContentResolver contentResolver = getContentResolver();
                    ContentValues contentValues = new ContentValues();
                    contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME,"Image_"+".jpg");
                    contentValues.put(MediaStore.MediaColumns.MIME_TYPE,"image/jpeg");
                    contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES+File.separator+"TestFolder");
                    Uri imageUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);

                    outst = contentResolver.openOutputStream(Objects.requireNonNull(imageUri));
                    bitmap.compress(Bitmap.CompressFormat.JPEG,100,outst);
                    Objects.requireNonNull(outst);

                    //Toast.makeText(MainActivity.this, "Image is saved!", Toast.LENGTH_SHORT).show();

                }
        }
        catch (Exception e){
            e.printStackTrace();
        }

你能告诉我如何纠正它并使其可见吗?

android google-maps google-maps-android-api-2 android-mapview
1个回答
0
投票

删除

capturescreenshot()
方法。

然后,替换:

                        GoogleMap.SnapshotReadyCallback callback = new GoogleMap.SnapshotReadyCallback() {
                            Bitmap bitmap;
                            @Override
                            public void onSnapshotReady(Bitmap snapshot) {
                                //Toast.makeText(MainActivity.this, "Test Snapshot Google Maps 2 Y", Toast.LENGTH_SHORT).show();
                                bitmap = snapshot;
                                try {
                                    Toast.makeText(MainActivity.this, "Test Snapshot Google Maps 2 Z", Toast.LENGTH_SHORT).show();

                                    Bitmap screenshot = capturescreenshot(linearLayout1);
                                    imageView_scre.setImageBitmap(screenshot);
                                    storeimageasjpeg(screenshot);

                                } catch (Exception e) {
                                    e.printStackTrace();
                                }/**/
                               

                            }
                        };

与:

                        GoogleMap.SnapshotReadyCallback callback = new GoogleMap.SnapshotReadyCallback() {
                            @Override
                            public void onSnapshotReady(Bitmap snapshot) {
                                try {
                                    imageView_scre.setImageBitmap(snapshot);
                                    storeimageasjpeg(snapshot);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        };

传递到

snapshot
onSnapshotReady()
图像是您的图像。 您链接到的问题之一的答案涵盖了这一点。

© www.soinside.com 2019 - 2024. All rights reserved.