How to resolve crashes when reading the width of video files in the gallery


Read the original article:How to resolve crashes when reading the width of video files in the gallery



Problem Description

When publishing a graphic note, after selecting an image or video from the system album, the filepath of the system album resource is obtained. Subsequently, when publishing the content, the publishing interface needs to retrieve the width and height information of the image/video resource. However, after selecting a video file from the gallery, a crash occurs when attempting to get the width using photoAccessHelper.PhotoAsset.get(photoAccessHelper.PhotoKeys.WIDTH).

cke_415.png

The problem code is as follows:

  async uriGetAssets() {
     try {
       let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(this.context);
       let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
       // Configure query conditions, use PhotoViewPicker to select the URI of the image to be queried. 
       predicates.equalTo('uri', this.uri);
       let fetchOption: photoAccessHelper.FetchOptions = {
         fetchColumns: [],
         predicates: predicates
       };
       let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption);
       // Obtain the PhotoAsset object corresponding to the URI and read partial information of the file. 
       const asset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject();
       console.info('asset displayName: ', asset.displayName);
       console.info('asset uri: ', asset.uri);
       console.info('asset photoType: ', asset.photoType);
       // The following code will throw an error when trying to get the width and height.       
       console.info('asset width: ', asset.get(photoAccessHelper.PhotoKeys.WIDTH));
       console.info('asset height: ', asset.get(photoAccessHelper.PhotoKeys.HEIGHT));
     } catch (error) {
       console.error('uriGetAssets failed with err: ' + JSON.stringify(error));
     }
  }
Enter fullscreen mode

Exit fullscreen mode



Background Knowledge

The interface documentation for photoAccessHelper.PhotoAsset.get.

The interface documentation for photoAccessHelper.FetchOptions.



Troubleshooting Process

According to the guide, error code 14000014 corresponds to the error message “Member is not a valid PhotoKey,” which means that width and height are invalid keys. There are two possible reasons for this error:

  1. The video file failed to be read, and width and height do not exist.
  2. he method used to obtain the width and height is incorrect.

However, since the video was successfully read, the first possibility is ruled out, so the issue lies in the method of obtaining the information.

Check whether the photoAccessHelper.PhotoAsset.get() interface supports retrieving the width and height of a video file. According to the interface documentation, the get method only supports querying four attributes: ‘uri’, ‘edia_type’, ‘ubtype’, and ‘display_name’. For other queries, you need to specify the required PhotoKeys in the fetchColumns. For example, to get the ‘title’ attribute, you would use fetchColumns: ['title'].



Analysis Conclusion

The photoAccessHelper.PhotoAsset.get() interface only supports queries for the four properties: ‘uri’, ‘edia_type’, ‘ubtype’, and ‘display_name’. For other queries, the required PhotoKeys must be specified in the fetchColumns.



Solution

// Add the properties for width, height, and title that need to be obtained in the fetchColumns of the configuration item.
 let fetchOption: photoAccessHelper.FetchOptions = {
   fetchColumns: ['width','height'],
   predicates: predicates
 };
Enter fullscreen mode

Exit fullscreen mode



Verification Result

Issue resolved and verified



Written by Dogan Evci



Source link