将框架布局转换为图像并保存 [英] Convert frame layout into image and save it

查看:28
本文介绍了将框架布局转换为图像并保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能帮助我知道如何将 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();
    }

别忘了在你的AndroidManifest.xml中设置写入存储的权限:

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

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

这篇关于将框架布局转换为图像并保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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