如何停止在Ionic App上自动下载和保存图片和视频

时间:2018-06-28 14:25:22

标签: angular ionic3

我有一个聊天应用程序,可以发送和接收媒体(照片和视频),因此当我打开消息页面时,它将自动下载和备份媒体。

当我打开一个离子页面时,我想停止媒体的下载和自动备份,我希望它像在WhatsApp中那样发生,我们通过单击它来手动下载图像。

有可能以离子形式做到吗?如果是这样,该怎么做?

请帮助。 谢谢

1 个答案:

答案 0 :(得分:0)

在服务器上获取邮件,然后查看

this.chatService.getHistoriqueMsg(msgListData).then((result) => {
            let data = JSON.parse(JSON.stringify(result));
            if (data.status == "success") {
                if (data.result.messages.length > 0) {
                    data.result.messages.forEach(element => {

                        if (element.typemessage == "IMAGE") {
                            let image = JSON.parse(element.message);
                            let url = encodeURI(this.constant.urlmsgImage + image.filepath);
                            //Save image to galery
                            let album = 'EBMChat';
                            this.photoLibrary.requestAuthorization().then((auto) => {
                                this.photoLibrary.saveImage(url, album).then((imgSave) => {

                                    const message: ChatMessage = {
                                        id: element.id,
                                        from: element.from,
                                        ufrom_username: element.ufrom_username,
                                        ufrom_avatar: (element.ufrom_avatar == null || element.ufrom_avatar == undefined) ? "./assets/imgs/default-profile-pic.png" : this.constant.urlServerAvatar + element.ufrom_avatar,
                                        to: element.to,
                                        uto_username: element.uto_username,
                                        uto_avatar: (element.uto_avatar == null || element.uto_avatar == undefined) ? "./assets/imgs/default-profile-pic.png" : this.constant.urlServerAvatar + element.uto_avatar,
                                        message: this.constant.urlmsgImage + image.filepath,
                                        sent: element.sent,
                                        read: element.read,
                                        direction: element.direction,
                                        typemessage: element.typemessage,
                                        deleteto: element.deleteto,
                                        deletefrom: element.deletefrom,
                                        ownership: element.from == this.user.userid ? 'mine' : 'other',
                                        imgSend: (element.read == "1") ? "./assets/ebm/ic_msg_lu.png" : "./assets/ebm/ic_sent_state.png"
                                    };

                                    this.msgList.push(message);
                                }).catch(err => {console.log(err)})
                            }).catch(err => {console.log(err)})

                        } else {
                            let emoji = require('node-emoji');
                            const message: ChatMessage = {
                                id: element.id,
                                from: element.from,
                                ufrom_username: element.ufrom_username,
                                ufrom_avatar: (element.ufrom_avatar == null || element.ufrom_avatar == undefined) ? "./assets/imgs/default-profile-pic.png" : this.constant.urlServerAvatar + element.ufrom_avatar,
                                to: element.to,
                                uto_username: element.uto_username,
                                uto_avatar: (element.uto_avatar == null || element.uto_avatar == undefined) ? "./assets/imgs/default-profile-pic.png" : this.constant.urlServerAvatar + element.uto_avatar,
                                message: emoji.emojify(element.message),
                                sent: element.sent,
                                read: element.read,
                                direction: element.direction,
                                typemessage: element.typemessage,
                                deleteto: element.deleteto,
                                deletefrom: element.deletefrom,
                                ownership: element.from == this.user.userid ? 'mine' : 'other',
                                imgSend: (element.read == "1") ? "./assets/ebm/ic_msg_lu.png" : "./assets/ebm/ic_sent_state.png"
                            };

                            this.msgList.push(message);
                        }
                    });

                    const currentGroupedMessages = _.groupBy(this.msgList, (message) => {
                        return this.timeStamp(message.sent);
                    });
                    // Transform dictionary into an array since Angular's view engine doesn't know how
                    // to iterate through it
                    this.groupedMessages = Object.keys(currentGroupedMessages).map((timestamp: string) => {
                        return {
                            timestamp: timestamp,
                            messages: currentGroupedMessages[timestamp],
                            today: this.timeStamp(Math.round(new Date().getTime() / 1000)) === timestamp
                        };
                    });

                }
            } 
        })