Save pictures for quick app

Keywords: Javascript Mobile network iOS Android

There is a hole in the saved image in the quick application. If you step on it, you will be hit. Here, you should also distinguish Huawei from other manufacturers. Although there is no problem with the debugger provided by Huawei and the official suppliers, Huawei mobile phones cannot be saved after the application goes online.

1. different points

  • Definition filename field to be displayed when Huawei mobile downloads pictures
  • Download the picture as a temporary file first, and then save it locally after the picture download is completed
  • Use request.download to download. Add the filename field to the api. Here, I take the time stamp of the current time as the download file name, and then divide the network path of the picture to get the suffix of the picture.
let photoPath = 'http://xxxxxxx.png';
let list = photoPath.split('.');
/* Use time as the name of the picture */
let timeValue = new Date().valueOf();
/* Get picture suffix */
let photoExt = list[list.length - 1];
/* Set the name of the picture to save */
let filename = `${timeValue}.${photoExt}`;

/* Then add the filename field to monitor the download task after the download operation. */
  • Then use request.onDownloadComplete to monitor the download task. If 1000 is returned, the download task fails.
  • Use media.saveToPhotosAlbum to save the picture after the download is successful

Complete code file

let photoPath = 'http://xxxxxxx.png';
let list = photoPath.split('.');
/* Use time as the name of the picture */
let timeValue = new Date().valueOf();
/* Get picture suffix */
let photoExt = list[list.length - 1];
/* Set the name of the picture to save */
let filename = `${timeValue}.${photoExt}`;
request.download({
  url: photoPath,
  filename: filename,
  success: data => {
    /* Listening to download task: 1000 download failed, 1001 download task does not exist */
    request.onDownloadComplete({
      token: data.token,
      success: data => {
        /* Save pictures */
        media.saveToPhotosAlbum({
          uri: data.uri,
          success: () => {
            prompt.showToast({
              message: 'Picture saved successfully!'
            })
          },
          fail: (data, code) => {
            if(code == 201) {
              prompt.showToast({
                message: 'The picture can only be saved after the authorization is successful'
              })
            } else if(code == 202) {
              prompt.showToast({
                message: 'Please refresh and try again'
              })
            } else if(code == 300) {
              prompt.showToast({
                message: 'I/O Error, picture save failed'
              })
            }
          }
        })
      },
      fail: (data, code) => {
        if(code == 1000) {
          prompt.showToast({
            message: 'Picture download failed'
          })
        } else if(code == 1001) {
          prompt.showToast({
            message: 'Download task does not exist'
          })
        }
      }
    })
  },
  fail: () => {

  }
})

Studying hard. If it helps your study, leave your mark.

Posted by ignorant on Wed, 23 Oct 2019 11:10:52 -0700