从 MapBox 地图视图拍摄快照 (Android)

Take snapshoot from MapBox mapView (Android)

我想从 MapBox MapView 中获取图片,但只有 returns 带有 MapBox Logo 的透明图像。

Transparent snapshot

这是代码,谢谢!:

 public File getBitmapFromView(View view) {
        Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(returnedBitmap);
        Drawable bgDrawable =view.getBackground();
        if (bgDrawable!=null)
            bgDrawable.draw(canvas);
        else
            canvas.drawColor(Color.WHITE);
        view.draw(canvas);

        File file = new File(Utils.getAppFolder() + currentTrack.getDate().toString().replace(" ", "_").replace(":","_") + ".png");

        try{
            file.createNewFile();
        }catch(IOException e){
            Utils.logError("IOException: Exception in create new File: " + e.toString() );
        }

        FileOutputStream fileos = null;
        try{
            fileos = new FileOutputStream(file);

        }catch(FileNotFoundException e){
            Log.e("FileNotFoundException",e.toString());
        }

        returnedBitmap.compress(Bitmap.CompressFormat.PNG, 80, fileos);
        return file;
    }

首先添加保存文件的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

这是代码:

private void takeScreenshot() {
    Date now = new Date();
    android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);

    try {
        // image naming and path  to include sd card  appending name you choose for file
        String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";

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

        File imageFile = new File(mPath);

        FileOutputStream outputStream = new FileOutputStream(imageFile);
        int quality = 100;
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
        outputStream.flush();
        outputStream.close();

        openScreenshot(imageFile);
    } catch (Throwable e) {
        // Several error may come out with file handling or OOM
        e.printStackTrace();
    }
}

已解决!,

这是解决这个问题的解释和代码

https://github.com/mapbox/mapbox-gl-native/issues/3677

执行当前 MapView 状态的快照并通知侦听器。此方法是异步的,因此它不会阻塞调用线程。 OnSnapshotReady.onSnapshotReady 从非 UI 线程调用。

mapView.snapshot{bitmap->}