修复file://格式图片无法显示以及媒体展示图片样例改用photoAccessHelper接口获取本地图片资源
Signed-off-by: zgf <zenggaofeng2@h-partners.com>
This commit is contained in:
parent
c51e0eca77
commit
db1ccdc604
|
@ -1,3 +1,6 @@
|
|||
## 2.3.0-rc.1
|
||||
- 修复file://格式图片无法显示
|
||||
|
||||
## 2.3.0-rc.0
|
||||
- 增加gif图duration的默认值,以及默认播放次数
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@ import { BusinessError } from '@ohos.base'
|
|||
export default class EntryAbility extends UIAbility {
|
||||
onWindowStageCreate(windowStage: window.WindowStage) {
|
||||
// Main window is created, set main page for this ability
|
||||
let list: Array<Permissions> = ['ohos.permission.MEDIA_LOCATION', 'ohos.permission.READ_MEDIA'];
|
||||
let list: Array<Permissions> = ['ohos.permission.READ_IMAGEVIDEO', 'ohos.permission.WRITE_IMAGEVIDEO'];
|
||||
let permissionRequestResult: Object;
|
||||
let atManager = abilityAccessCtrl.createAtManager();
|
||||
atManager.requestPermissionsFromUser(this.context, list, (err: BusinessError, result: Object) => {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (C) 2023 Huawei Device Co., Ltd.
|
||||
* Copyright (C) 2024 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
|
@ -12,8 +12,9 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import mediaLibrary from '@ohos.multimedia.mediaLibrary';
|
||||
import { ImageKnifeComponent, ImageKnifeOption, } from '@ohos/libraryimageknife'
|
||||
import { ImageKnifeComponent, ImageKnifeOption } from '@ohos/libraryimageknife'
|
||||
import { dataSharePredicates } from '@kit.ArkData';
|
||||
import { photoAccessHelper } from '@kit.MediaLibraryKit';
|
||||
|
||||
|
||||
@Entry
|
||||
|
@ -34,56 +35,27 @@ struct DataShareUriLoadPage {
|
|||
Text("获取媒体图库的uri用ImageKnife展示").fontSize(15)
|
||||
Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
|
||||
Button("点击加载Uri并展示")
|
||||
.onClick(() => {
|
||||
// 获取mediaLibrary实例,后续用到此实例均采用此处获取的实例
|
||||
const context = getContext(this);
|
||||
let media = mediaLibrary.getMediaLibrary(context);
|
||||
|
||||
let imageType = mediaLibrary.MediaType.IMAGE;
|
||||
// 创建文件获取选项,此处参数为获取image类型的文件资源
|
||||
let imagesFetchOp:mediaLibrary.MediaFetchOptions = {
|
||||
selections: mediaLibrary.FileKey.MEDIA_TYPE + '= ?',
|
||||
selectionArgs: [imageType.toString()],
|
||||
};
|
||||
// 获取文件资源,使用callback方式返回异步结果
|
||||
media.getFileAssets(imagesFetchOp, (error, fetchFileResult) => {
|
||||
// 判断获取的文件资源的检索结果集是否为undefined,若为undefined则接口调用失败
|
||||
if (fetchFileResult == undefined) {
|
||||
console.log('get fetchFileResult failed with error: ' + error);
|
||||
return;
|
||||
}
|
||||
// 获取文件检索结果集中的总数
|
||||
const count = fetchFileResult.getCount();
|
||||
// 判断结果集中的数量是否小于0,小于0时表示接口调用失败
|
||||
if (count < 0) {
|
||||
console.log('get count from fetchFileResult failed, count: ' + count);
|
||||
return;
|
||||
}
|
||||
// 判断结果集中的数量是否等于0,等于0时表示接口调用成功,但是检索结果集为空,请检查文件获取选项参数配置是否有误和设备中是否存在相应文件
|
||||
if (count == 0) {
|
||||
console.log('The count of fetchFileResult is zero');
|
||||
return;
|
||||
}
|
||||
console.log('Get fetchFileResult successfully, count: ' + count);
|
||||
// 获取文件检索结果集中的第一个资源,使用callback方式返回异步结果
|
||||
fetchFileResult.getFirstObject((error, fileAsset) => {
|
||||
// 检查获取的第一个资源是否为undefined,若为undefined则接口调用失败
|
||||
if (fileAsset == undefined) {
|
||||
console.log('get first object failed with error: ' + error);
|
||||
return;
|
||||
}
|
||||
console.log("fileAsset id=" + fileAsset.id + " fileAsset uri=" + fileAsset.uri + " fileAsset displayName=" + fileAsset.displayName)
|
||||
|
||||
// 加载图库第一张图片的uri
|
||||
.onClick(async () => {
|
||||
const context = getContext(this)
|
||||
let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context)
|
||||
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates
|
||||
let fetchOptions: photoAccessHelper.FetchOptions = {
|
||||
fetchColumns:[],
|
||||
predicates:predicates
|
||||
}
|
||||
let fetchResult = await phAccessHelper.getAssets(fetchOptions)
|
||||
if(fetchResult != undefined) {
|
||||
console.info("fetchResult success")
|
||||
let photoAsset: photoAccessHelper.PhotoAsset = await fetchResult.getFirstObject()
|
||||
if(photoAsset != undefined) {
|
||||
console.info("photoAsset.displayName :"+photoAsset.displayName)
|
||||
this.imageKnifeOption1 = {
|
||||
loadSrc: fileAsset.uri,
|
||||
placeholderSrc: $r('app.media.icon_loading'),
|
||||
loadSrc: photoAsset.uri,
|
||||
placeholderSrc:$r('app.media.icon_loading'),
|
||||
errorholderSrc: $r('app.media.icon_failed')
|
||||
}
|
||||
// 释放FetchFileResult实例并使其失效。无法调用其他方法
|
||||
fetchFileResult.close();
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}).margin({ top: 5, left: 3 })
|
||||
ImageKnifeComponent({ imageKnifeOption: this.imageKnifeOption1 }).width(300).height(300)
|
||||
}.width('100%').backgroundColor(Color.Pink)
|
||||
|
|
|
@ -52,8 +52,8 @@
|
|||
}
|
||||
},
|
||||
{
|
||||
"name": "ohos.permission.MEDIA_LOCATION",
|
||||
"reason": "$string:app_permission_MEDIA_LOCATION",
|
||||
"name": "ohos.permission.READ_IMAGEVIDEO",
|
||||
"reason": "$string:app_permission_READ_IMAGEVIDEO",
|
||||
"usedScene": {
|
||||
"abilities": [
|
||||
"EntryAbility"
|
||||
|
@ -62,8 +62,8 @@
|
|||
}
|
||||
},
|
||||
{
|
||||
"name": "ohos.permission.READ_MEDIA",
|
||||
"reason": "$string:app_permission_READ_MEDIA",
|
||||
"name": "ohos.permission.WRITE_IMAGEVIDEO",
|
||||
"reason": "$string:app_permission_WRITE_IMAGEVIDEO",
|
||||
"usedScene": {
|
||||
"abilities": [
|
||||
"EntryAbility"
|
||||
|
|
|
@ -114,12 +114,12 @@
|
|||
"value": "马赛克处理"
|
||||
},
|
||||
{
|
||||
"name": "app_permission_MEDIA_LOCATION",
|
||||
"value": "获取媒体图片"
|
||||
"name": "app_permission_READ_IMAGEVIDEO",
|
||||
"value": "获取读媒体资源权限"
|
||||
},
|
||||
{
|
||||
"name": "app_permission_READ_MEDIA",
|
||||
"value": "读媒体图片"
|
||||
"name": "app_permission_WRITE_IMAGEVIDEO",
|
||||
"value": "获取写入媒体资源权限"
|
||||
}
|
||||
]
|
||||
}
|
|
@ -14,7 +14,7 @@
|
|||
"main": "index.ets",
|
||||
"repository": "https://gitee.com/openharmony-tpc/ImageKnife",
|
||||
"type": "module",
|
||||
"version": "2.2.0-rc.3",
|
||||
"version": "2.3.0-rc.1",
|
||||
"dependencies": {
|
||||
"pako": "^2.1.0",
|
||||
"@ohos/gpu_transform": "^1.0.0"
|
||||
|
|
|
@ -20,24 +20,21 @@ import { DataFetchResult } from './DataFetchResult';
|
|||
|
||||
@Sendable
|
||||
export class LoadDataShareFileClient implements IDataFetch {
|
||||
loadData(request: RequestOption) {
|
||||
async loadData(request: RequestOption) {
|
||||
let result:DataFetchResult =new DataFetchResult()
|
||||
if (typeof request.loadSrc == 'string') {
|
||||
fs.open(request.loadSrc, fs.OpenMode.READ_ONLY).then((file) => {
|
||||
fs.stat(file.fd).then(stat =>{
|
||||
let buf = new ArrayBuffer(stat.size);
|
||||
fs.read(file.fd, buf).then((readLen) => {
|
||||
result.data=buf;
|
||||
fs.close(file.fd);
|
||||
}).catch((err:BusinessError) => {
|
||||
result.error = 'LoadDataShareFileClient fs.read err happened uri=' + request.loadSrc + " err.msg=" + err?.message + " err.code=" + err?.code;
|
||||
try {
|
||||
let file = await fs.open(request.loadSrc,fs.OpenMode.READ_ONLY)
|
||||
let stat = await fs.stat(file.fd)
|
||||
let buf = new ArrayBuffer(stat.size)
|
||||
await fs.read(file.fd, buf)
|
||||
.then((readLen)=>{
|
||||
result.data = buf
|
||||
fs.close(file.fd)
|
||||
})
|
||||
}).catch((err:BusinessError) => {
|
||||
result.error = 'LoadDataShareFileClient fs.stat err happened uri=' + request.loadSrc + " err.msg=" + err?.message + " err.code=" + err?.code;
|
||||
})
|
||||
}).catch((err:BusinessError) => {
|
||||
result.error ='LoadDataShareFileClient fs.open err happened uri=' + request.loadSrc + " err.msg=" + err?.message + " err.code=" + err?.code;
|
||||
})
|
||||
} catch(e) {
|
||||
result.error = 'LoadDataShareFileClient err happened uri=' + request.loadSrc+"err.msg="+e?.message+"err.code="+e?.code
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue