• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *  http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import common from '@ohos.app.ability.common';
17import fs from '@ohos.file.fs';
18import photoAccessHelper from '@ohos.file.photoAccessHelper';
19import promptAction from '@ohos.promptAction';
20import Logger from '../../utils/Logger';
21import dataSharePredicates from '@ohos.data.dataSharePredicates';
22
23
24@Component
25export struct SecSaveButton {
26  @State backColor: Resource = $r('app.color.button_default_bg_color1');
27  @State textColor: Resource = $r('app.color.button_default_bg_color');
28  @State image: Resource = $r('app.media.save');
29  @Link saveImage: Resource;
30  private screenDensity: number = 0;
31
32  promptAction(message: string | Resource) {
33    try {
34      promptAction.showToast({
35        message: message,
36        duration: 6000,
37      });
38    } catch (error) {
39      Logger.error(`showToast args error code is ${error.code}, message is ${error.message}`);
40    }
41  }
42
43  async saveMedia() {
44    try {
45      const context = getContext(this);
46      // 得到图片内容
47      try {
48        context.resourceManager.getMediaContent($r('app.media.banner').id, async (error, value) => {
49          if (error != null) {
50            Logger.error("error is " + error);
51          } else {
52            let media = value.buffer;
53            let helper = photoAccessHelper.getPhotoAccessHelper(context);
54            let uri = await helper.createAsset(photoAccessHelper.PhotoType.IMAGE, 'png');
55            // 通过uri打开媒体库文件
56            let file = await fs.open(uri,  fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
57            Logger.info('openFile success, fd: ' + file.fd);
58            // 写到媒体库文件中
59            await fs.write(file.fd, media);
60            await fs.close(file.fd);
61            this.promptAction($r('app.string.Save_succeed'));
62          }
63        });
64      } catch (error) {
65        Logger.error(`callback getMediaContent failed, error code: ${error.code}, message: ${error.message}.`);
66      }
67
68    } catch (err) {
69      this.promptAction($r('app.string.Save_failed'));
70      Logger.error(`promiseerror code: ${err.code}, message: ${err.message}.`);
71    }
72  }
73  build() {
74    Column() {
75      SaveButton()
76        .onClick(async (event, result) => {
77          if (result === SaveButtonOnClickResult.SUCCESS) {
78            Logger.info("SUCCESS");
79            this.saveMedia();
80          }
81          if(result === SaveButtonOnClickResult.TEMPORARY_AUTHORIZATION_FAILED){
82            Logger.info("TEMPORARY_AUTHORIZATION_FAILED");
83          }
84        })
85    }
86    .id('saveButton')
87  }
88}