creator版本2.4.4 截图且保存在相册 安卓 (搬砖菜鸟)

先看了文档
https://docs.cocos.com/creator/manual/zh/render/camera.html#截图

image
点进去范例
assets/cases/07_capture_texture/textureRenderUtils.js
assets/cases/07_capture_texture/capture_to_native.js
这2个文件拷贝进项目中
capture_to_native.js直接挂在场景上
新建一个用于截图的摄像机挂载上去
image
下图红框内的代码是官方示例里面没有的,官网里面示例只保存到本地,要在相册中看到,还需要调用原生方法

原生代码如下

public static void saveTextureToLocal( String pngPath) {

    Bitmap bmp = BitmapFactory.decodeFile(pngPath)//从路径中读取 照片
    // fileName ==textureName  尽量和JS保存的一致
    String fileName = "textureName";
    File file = new File(pngPath);
    try {
        FileOutputStream fos = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.flush();
        fos.close();
        Log.d("保存成功",pngPath );

    } catch (FileNotFoundException e) {
        Log.d("保存错误1",e.toString());

        e.printStackTrace();
    } catch (IOException e) {
        Log.d("保存错误2",e.toString());

        e.printStackTrace();
    }

    // 其次把文件插入到系统图库
    try {
        MediaStore.Images.Media.insertImage(AppActivity.getContext().getApplicationContext().getContentResolver(),
                file.getAbsolutePath(), fileName, null);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    // 最后通知图库更新
    AppActivity.getContext().getApplicationContext().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(file.getAbsolutePath())));

}

把代码贴在AppActivity.java脚本里就行了
还需要动态申请两项权限
在AndroidManifest.xml中静态添加

image

这样子就OK了

参考文档
https://blog.csdn.net/yzx5452830/article/details/88745130?utm_medium=distribute.pc_relevant.none-task-blog-2~default~searchFromBaidu~default-1.pc_relevant_baidujshouduan&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~searchFromBaidu~default-1.pc_relevant_baidujshouduan

https://blog.csdn.net/qq_37980878/article/details/82351389

7赞

mark~

大佬666

mark~

mark~

链接找不到了,可以提供一下Demo吗