如何在android画布中将整个画布保存为jpeg图像?

Nit*_*kor 6 java android canvas image bitmap

我在画布上画了一些东西,我想将此图作为 jpeg 图像保存在我的存储中。我使用以下方法,它将图像sign.jpg保存在我的目录中,但它保存空白黑色图像,没有在其上绘制任何内容,其大小为14.49kb。

这是方法

public Bitmap savebit(){

        Bitmap  saveBitmap = Bitmap.createBitmap(this.getWidth(), this.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(saveBitmap);
        canvas.setBitmap(saveBitmap);
        this.draw(canvas);

        File file = new File(Environment.getExternalStorageDirectory() + "/sign.jpg");

        try {
            saveBitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(file));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return saveBitmap;
    }
Run Code Online (Sandbox Code Playgroud)

Soh*_*hid 5

只需将您的膨胀视图传递给以下函数即可

Bitmap bmp = viewToBitmap(abc);
Bitmap bmp = viewToBitmap(frameLayout);
Bitmap bmp = viewToBitmap(LinearLayout);
Bitmap bmp = viewToBitmap(AnyView);
Run Code Online (Sandbox Code Playgroud)
public Bitmap viewToBitmap(View view) {
        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        view.draw(canvas);
        return bitmap;
    }
Run Code Online (Sandbox Code Playgroud)