How to resolve crashes when reading the width of video files in the gallery
Source: Dev.to
Problem Description
When publishing a graphic note, after selecting an image or video from the system album, the file path of the system album resource is obtained. During publishing, the interface needs to retrieve the width and height of the image/video resource.
Selecting a video file from the gallery and then calling
photoAccessHelper.PhotoAsset.get(photoAccessHelper.PhotoKeys.WIDTH)
causes a crash.
Problem code
async uriGetAssets() {
try {
let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(this.context);
let predicates = 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 = {
fetchColumns: [],
predicates: predicates
};
let fetchResult = await phAccessHelper.getAssets(fetchOption);
// Obtain the PhotoAsset object corresponding to the URI and read partial information of the file.
const asset = 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));
}
}
Background Knowledge
- Interface documentation for
photoAccessHelper.PhotoAsset.get. - Interface documentation for
photoAccessHelper.FetchOptions.
Troubleshooting Process
The error code 14000014 corresponds to the message “Member is not a valid PhotoKey”, indicating that WIDTH and HEIGHT are not valid keys for the get method.
Possible reasons:
- The video file could not be read, so width and height do not exist.
- The method used to obtain width and height is incorrect.
Since the video is successfully read, the first possibility is ruled out. The issue lies in the method of obtaining the information.
The photoAccessHelper.PhotoAsset.get() interface only supports querying four attributes: uri, media_type, subtype, and display_name. For other attributes, the required PhotoKeys must be specified in fetchColumns. For example, to retrieve the title attribute, you would set fetchColumns: ['title'].
Analysis Conclusion
photoAccessHelper.PhotoAsset.get() can query only the four properties mentioned above. To obtain other properties such as width and height, they must be included in the fetchColumns of the FetchOptions.
Solution
Add the required properties (width, height, and optionally title) to the fetchColumns when configuring FetchOptions.
// Add the properties for width, height and title that need to be obtained in the fetchColumns of the configuration item.
let fetchOption = {
fetchColumns: ['width', 'height'],
predicates: predicates
};
After this change, the asset object will contain the width and height values, and they can be accessed without causing a crash.
Verification Result
The issue is resolved and verified: width and height of video files are now correctly retrieved without crashes.