更新时间:2022-01-05 08:58:47浏览次数:429+次
在鸿蒙开发中,调用相册选择图片,获取到这个地址:dataability:///media/external/images/media/25,然后要怎么的把地址转为绝对路径?
解答
以下代码可解决问题:
/**
* 获取图片绝对路径
* @return
*/
public String getImagePath() {
String url = "";
try {
helper = DataAbilityHelper.creator(getContext());
ResultSet resultSet = helper.query(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI,
new String[]{
AVStorage.Images.Media.ID,
AVStorage.Images.Media.DISPLAY_NAME, AVStorage.Images.Media.DATA
}, null);
while (resultSet.goToNextRow()){
int columnIndexForName = resultSet.getColumnIndexForName(AVStorage.Images.Media.DATA);
url = resultSet.getString(columnIndexForName);
}
} catch (DataAbilityRemoteException e) {
e.printStackTrace();
}
return url;
}
说明
其实拿绝对路径没有什么用,因为又不让你往那写,要读直接用这个路径读就好了。另外,还可以参考以下文章:鸿蒙跳转到图库或者文件选择图片并显示到Image控件中。
首先当然是建个布局页面,页面上放个按钮和图片控件,如下代码:
<?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">
<Button
ohos:id="$+id:btnChooseImg"
ohos:height="match_content"
ohos:width="match_content"
ohos:text="选择图片"
ohos:text_size="40vp"
ohos:background_element="gray"></Button>
<Image
ohos:id="$+id:showChooseImg"
ohos:height="match_content"
ohos:width="match_content"
></Image>
</DirectionalLayout>
然后看一下后台代码,后代主要用到DataAbility去访问图片,访问图片我们需要申请ohos.permission.READ_USER_STORAGE权限,代码如下:
package com.example.phonetest.slice;
import com.example.phonetest.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Image;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.photokit.metadata.AVStorage;
import ohos.utils.net.Uri;
import java.io.File;
import java.io.FileDescriptor;
public class ChooseImageTestSlice extends AbilitySlice {
static final HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x0001, "选择图片测试");
private final int imgRequestCode=1123;
Image showChooseImg=null;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_choose_image_test);
//获取存储权限
requestPermissionsFromUser(new String[]{"ohos.permission.READ_USER_STORAGE"},imgRequestCode);
Button btnChooseImg=(Button)findComponentById(ResourceTable.Id_btnChooseImg);
btnChooseImg.setClickedListener(c->{
//选择图片
selectPic();
});
showChooseImg=(Image)findComponentById(ResourceTable.Id_showChooseImg);
}
private void selectPic() {
Intent intent = new Intent();
Operation opt=new Intent.OperationBuilder().withAction("android.intent.action.GET_CONTENT").build();
intent.setOperation(opt);
intent.addFlags(Intent.FLAG_NOT_OHOS_COMPONENT);
intent.setType("image/*");
startAbilityForResult(intent, imgRequestCode);
}
@Override
protected void onAbilityResult(int requestCode, int resultCode, Intent resultData) {
if(requestCode==imgRequestCode)
{
HiLog.info(label,"选择图片getUriString:"+resultData.getUriString());
//选择的Img对应的Uri
String chooseImgUri=resultData.getUriString();
HiLog.info(label,"选择图片getScheme:"+chooseImgUri.substring(chooseImgUri.lastIndexOf('/')));
//定义数据能力帮助对象
DataAbilityHelper helper=DataAbilityHelper.creator(getContext());
//定义图片来源对象
ImageSource imageSource = null;
//获取选择的Img对应的Id
String chooseImgId=null;
//如果是选择文件则getUriString结果为content://com.android.providers.media.documents/document/image%3A30,其中%3A是":"的URL编码结果,后面的数字就是image对应的Id
//如果选择的是图库则getUriString结果为content://media/external/images/media/30,最后就是image对应的Id
//这里需要判断是选择了文件还是图库
if(chooseImgUri.lastIndexOf("%3A")!=-1){
chooseImgId = chooseImgUri.substring(chooseImgUri.lastIndexOf("%3A")+3);
}
else {
chooseImgId = chooseImgUri.substring(chooseImgUri.lastIndexOf('/')+1);
}
//获取图片对应的uri,由于获取到的前缀是content,我们替换成对应的dataability前缀
Uri uri=Uri.appendEncodedPathToUri(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI,chooseImgId);
HiLog.info(label,"选择图片dataability路径:"+uri.toString());
try {
//读取图片
FileDescriptor fd = helper.openFile(uri, "r");
imageSource = ImageSource.create(fd, null);
//创建位图
PixelMap pixelMap = imageSource.createPixelmap(null);
//设置图片控件对应的位图
showChooseImg.setPixelMap(pixelMap);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (imageSource != null) {
imageSource.release();
}
}
}
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
这里要注意下在弹出的选择有让我们选择图库还是文件,这两种方式处理方式不一样,判断一下就可以了,代码中已经注释。
然后看一下运行结果:
注:记得提前拍两张照片,因为模拟器里面一开始是没有照片的。
相关资讯