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