Cocos Creator 从下载图片到手机本地相册

在游戏中难免会有使用保存图片到本地的功能

比如二维码等 但是creator的方法是保存在他的游戏目录里 如果手机没有root是无法查看的

这个方法就是 把图片下载到游戏目录 然后在通过目录查找图片 然后把它保存到手机相册

Cocos creator的 Ts 方法 

ImgUrl为你要下载的图片URL  

 SaveToLocal(ImgUrl:string){

        let fileName="textureName";
        let fileType=".png";
        let filePath:string=null;
        let xhr=new XMLHttpRequest();
        xhr.onreadystatechange=()=>{
            if(xhr.readyState===4 && xhr.status ===200){
                if(xhr.response&&cc.sys.isNative){
                    let rootPath=jsb.fileUtils.getWritablePath();
                    filePath = rootPath + fileName +fileType;
                    let u8a=new Uint8Array(xhr.response);
                    jsb.fileUtils.writeDataToFile(u8a,filePath);
//JS调用JAVA saveTextureToLocal 方法 参数为 filePath 也就是路径
                    jsb.reflection.callStaticMethod("org/cocos2dx/javascript/AppActivity", "saveTextureToLocal", "(Ljava/lang/String;)V", filePath);
//提示已经保存  这是单独写的提示文字的方法 你们可以忽略
                    ManagerNotice.getInstance().show('已保存到相册, 尽快去分享哦');
                }
            }

        },
        xhr.responseType='arraybuffer';
        xhr.open("GET",ImgUrl,true);
        xhr.send();
    }

下边是安卓 的方法 写在  AppActivity  脚本里  

saveTextureToLocal  方法名一定要和JS里调用的一样

 //动态获取内存存储权限
    public static void verifyStoragePermissions() {
        // Check if we have write permission
        int permission = ActivityCompat.checkSelfPermission(app,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);

        if (permission != PackageManager.PERMISSION_GRANTED) {
            // We don't have permission so prompt the user
            //ActivityCompat.requestPermissions(app, PERMISSIONS_STORAGR,REQUEST_EXTERNAL_STORAGE);
        }
    }

    // 获取 路径中的图片 保存到本地  
    public static void saveTextureToLocal( String pngPath) {
        Log.d("图片地址",pngPath);
//先调用上边的方法 获取一下权限  有的时候会说你没有权限
        verifyStoragePermissions();
//从路径中读取 照片
        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())));

    }
发布了40 篇原创文章 · 获赞 36 · 访问量 21万+

猜你喜欢

转载自blog.csdn.net/yzx5452830/article/details/88745130
今日推荐