【Harmony OS】【JAVA UI】AVMetadataHelper 基本使用
准备阶段
参考资料媒体元数据获取开发指导
准备一个视频文件,放在resources/base/rawfile/文件目录下(如下图所示)
权限
申请读写权限(ohos.permission.WRITE_USER_STORAGE,ohos.permission.READ_USER_STORAGE)在config.json(代码和图片如下所示)
"reqPermissions": [
{"name": "ohos.permission.READ_USER_STORAGE"},
{"name": "ohos.permission.WRITE_USER_STORAGE"},
],
在mainAbility的onstart申请动态权限,代码如下所示
package com.harmony.alliance.mydemo;
import com.harmony.alliance.mydemo.slice.*;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
public class MainAbility extends Ability {
@Override
public void onStart(Intent intent) {
String[] permissions = {
"ohos.permission.WRITE_USER_STORAGE",
"ohos.permission.READ_USER_STORAGE"
};
requestPermissionsFromUser(permissions, 0);
super.onStart(intent);
super.setMainRoute(AVMetadataHelperAbilitySlice.class.getName());
}
}
代码实现
在resources/base/layout写av_metadata_helper.xml文件,代码如下
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:orientation="vertical">
<Text
ohos:height="200fp"
ohos:width="match_parent"
ohos:text="写入"
ohos:text_color="#ed6262"
ohos:text_size="20fp"
ohos:id="$+id:wite"
ohos:text_alignment="center"
ohos:background_element="#80ffffff"/>
<Text
ohos:height="200fp"
ohos:width="match_parent"
ohos:text="读取"
ohos:text_color="#ffffff"
ohos:text_size="20fp"
ohos:id="$+id:read"
ohos:text_alignment="center"
ohos:background_element="#ed6262"/>
<Image
ohos:scale_mode="clip_center"
ohos:image_src="#ed6200"
ohos:height="400fp"
ohos:width="400fp"
ohos:id="$+id:mIvLogo"/>
</DirectionalLayout>
效果图如下所示
java代码
package com.harmony.alliance.mydemo.slice;
import com.harmony.alliance.mydemo.ResourceTable;
import com.harmony.alliance.mydemo.utils.HiLogUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.global.resource.RawFileEntry;
import ohos.global.resource.Resource;
import ohos.media.image.PixelMap;
import ohos.media.photokit.metadata.AVMetadataHelper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class AVMetadataHelperAbilitySlice extends AbilitySlice {
@Override
protected void onStart(Intent intent) {
super.onStart(intent);
setUIContent(ResourceTable.Layout_av_metadata_helper);
findComponentById(ResourceTable.Id_wite).setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
writeToDisk();
}
});
findComponentById(ResourceTable.Id_read).setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
AVMetadataHelper avMetadataHelper = new AVMetadataHelper ();
String externalFilePath = getFilesDir() + "/video_resuorce.mp4";
avMetadataHelper.setSource(externalFilePath);
PixelMap pixelMap = avMetadataHelper.fetchVideoPixelMapByTime(0);
Image miv= (Image) findComponentById(ResourceTable.Id_mIvLogo);
miv.setPixelMap(pixelMap);
avMetadataHelper.release();
}
});
}
private void writeToDisk() {
String rawFilePath = "entry/resources/rawfile/video_resuorce.mp4";
String externalFilePath = getFilesDir() + "/video_resuorce.mp4";
File file = new File(externalFilePath);
if (file.exists()) {
return;
}
RawFileEntry rawFileEntry = getResourceManager().getRawFileEntry(rawFilePath);
try (FileOutputStream outputStream = new FileOutputStream(new File(externalFilePath))) {
Resource resource = rawFileEntry.openRawFile();
// cache length
byte[] cache = new byte[1024];
int len = resource.read(cache);
while (len != -1) {
outputStream.write(cache, 0, len);
len = resource.read(cache);
}
HiLogUtils.PrintLog("写入成功"+externalFilePath);
} catch (IOException exception) {
HiLogUtils.PrintLog(exception.getMessage());
}
}
}
运行效果
欲了解更多更全技术文章,欢迎访问https://developer.huawei.com/consumer/cn/forum/?ha_source=zzh