转换框架布局成图像,并保存布局、并保存、框架、图像

2023-09-06 16:08:31 作者:苦口甜心

任何一个可以请帮我知道如何捕捉的FrameLayout 的内容为图像并将其保存到内部或外部存储。

can any one please help me to know how to capture contents of a FrameLayout into an image and save it to internal or external storage.

推荐答案

试试这个转换期(的FrameLayout)为位图:

try this to convert a view (framelayout) into a bitmap:

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;
}

然后,保存您的位图到一个文件中:

then, save your bitmap into a file:

try {
        FileOutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory() + "/path/to/file.png");
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
        output.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

不要忘了写存储器的权限设置成你的Andr​​oidManifest.xml:

don't forget to set the permission of writing storage into your AndroidManifest.xml:

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