中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
如何获取图片的缩略图?
微信扫码分享
import { common, Want, wantConstant } from '@kit.AbilityKit'; import { photoAccessHelper } from '@kit.MediaLibraryKit'; import { dataSharePredicates } from '@kit.ArkData'; // 定义一个uri数组,用于接收PhotoViewPicker选择图片返回的uri let uris: Array<string> = []; const context = getContext(this); async function uriGetAssets() { try { let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context); let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates(); // 配置查询条件,使用PhotoViewPicker选择图片返回的uri进行查询 predicates.equalTo('uri', uris[0]); let fetchOption: photoAccessHelper.FetchOptions = { fetchColumns: [], predicates: predicates }; let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = await phAccessHelper.getAssets(fetchOption); // 得到uri对应的PhotoAsset对象,读取文件的部分信息 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); console.info('asset width: ', asset.get(photoAccessHelper.PhotoKeys.WIDTH)); console.info('asset height: ', asset.get(photoAccessHelper.PhotoKeys.HEIGHT)); console.info('asset title: ' + asset.get(photoAccessHelper.PhotoKeys.TITLE)); // 获取缩略图 asset.getThumbnail((err, pixelMap) => { if (err == undefined) { console.info('getThumbnail successful ' + JSON.stringify(pixelMap)); } else { console.error('getThumbnail fail', err); } }); } catch (error) { console.error('uriGetAssets failed with err: ' + JSON.stringify(error)); } }