更多资讯请访问:????????????????????????????????????????????????????????????????????????????????????????????????????再见,你好三月,阳春三月万物复苏,愿一切如约而至。共创鸿蒙社区。前几天有同事问我怎么把系统相册里的图片保存下来。那时,我很困惑。看来我是真的不懂鸿蒙的?而这个操作在我们平时的开发中也经常用到,所以就入手了。效果显示踩坑之路应该是在官网介绍的。去官网发现有一点介绍。附上链接:https://developer.harmonyos.com/cn/docs/documentation/doc-guides/media-data-mgmt-storage-0000001050994909说安卓和鸿蒙差不多,思路应该也差不多,所以我找到一篇文章(https://harmonyos.51cto.com/posts/10568)里面有MediaStore类,用于操作系统的媒体数据库鸿蒙确实有类似的类AVStorage,但是开放的功能不如像MediaStore一样强大。后来发现鸿蒙的设计思路有点像ios的。每个应用都有自己的沙河目录,每个应用的数据都保存在当前应用中,极大的保证了数据的隐蔽性和安全性,这一点优于安卓。安全性要好得多。保存图片到系统相册demolayout://showpicture//选择照片//保存图像效果如图:config.json中涉及的权限配置如下:"reqPermissions":[{"name":"ohos.permission.READ_USER_STORAGE"},{"name":"ohos.permission.WRITE_USER_STORAGE"}]动态ic应用权限需要动态申请这两个权限,申请时会有权限弹窗。不写的话不会有权限弹窗,不过也可以用。String[]permissions={"ohos.permission.READ_USER_STORAGE","ohos.permission.WRITE_USER_STORAGE"};requestPermissionsFromUser(权限,0);获取图片保存权限后,即可将图片保存到系统相册中。我们需要DataAbilityHelper和AVStorage来添加、删除、修改和检查我们的媒体。//保存图片到相册fileName文件名PixelMap图片数据privatevoidsaveImageToLibrary(StringfileName,PixelMappixelMap){try{ValuesBucketvaluesBucket=newValuesBucket();//文件名valuesBucket.putString(AVStorage.Images.Media.DISPLAY_NAME,fileName);//相对路径valuesBucket.putString("relative_path","DCIM/");//文件格式和类型必须注意JPEG和PNG类型不支持valuesBucket.putString(AVStorage.Images.Media.MIME_TYPE,"image/JPEG");//Applicationexclusive:当is_pending设置为1时,表示只有本应用可以访问这张图片,其他应用找不到这张图片。图片处理操作完成后,将is_pending设置为0,释放独占,让其他应用可以访问该图片。应用可以看到valuesBucket.putInteger("is_pending",1);//鸿蒙的helper.insert方法和Android的contentResolver.insert方法不同。Android方法直接返回一个uri,我们可以直接操作,而鸿蒙方法Returning官方的描述是Returnstheindexoftheinserteddatarecord(返回插入数据记录的索引)。这个索引就是我理解中的id。因此,我们需要拼出文件的uri,然后操作DataAbilityHelperhelper=DataAbilityHelper。创造者(这个);intindex=helper.insert(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI,valuesBucket);Uriuri=Uri.appendEncodedPathToUri(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI,String.valueOf(index));//获取到uri后,Android可以通过contentResolver.openOutputStream(uri)获取输出流写入文件,而鸿蒙没有提供这样的方法,所以我们只能通过uri获取FileDescriptor,然后通过生成输出流FileDescriptor打包并编码成一个新的图像文件。这里helper.openFile方法必须有w写模式,否则会报FileNotFound错误FileDescriptorfd=helper.openFile(uri,"w");ImagePackerimagePacker=ImagePacker.create();ImagePacker.PackingOptionspackingOptions=newImagePacker.PackingOptions();OutputStreamoutputStream=newFileOutputStream(fd);packingOptions.format="图片/jpeg";packingOptions.quality=90;布尔结果=imagePacker.initializePacking(outputStream,packingOptions);如果(结果){结果=imagePacker.addImage(pixelMap);如果(结果){longdataSize=imagePacker.finalizePacking();}}outputStream.flush();outputStream.close();valuesBucket.clear();//释放排他值Bucket.putInteger("is_pending",0);helper.update(uri,valuesBucket,null);}catch(Exceptione){e.printStackTrace();}}效果如下读取本地相册图片,在config.json中配置读取文件权限(ohos.permission.READ_USER_STORAGE)"reqPermissions":[{"name":"ohos.permission.READ_USER_STORAGE"}]在能力中手动申请权限String[]permissions={"ohos.permission.READ_USER_STORAGE"};requestPermissionsFromUser(permissions,0);弹出数据源选择框获取数据源方法//选择一张图片privatevoidselectPhoto(){//调用系统的选择源数据视图Intentintent=newIntent();操作opt=newIntent.OperationBuilder().withAction("android.intent.action.GET_CONTENT").build();意图.setOperation(选择);intent.addFlags(Intent.FLAG_NOT_OHOS_COMPONENT);intent.setType("图片/*");startAbilityForResult(意图,imgRequestCode);,imgRequestCode字段是自定义的,必须是int类型。该字段与上述selectPhoto()方法中的imgRequestCode一致。根据这个imgRequestCode判断是否是选中图片的回调返回,/*选择图片回调*/@OverrideprotectedvoidonAbilityResult(intrequestCode,intresultCode,IntentresultData){if(requestCode==imgRequestCode&&resultData!=null){//选中的Img对应的UriStringchooseImgUrl=resultData.getUriString();//定义数据能力助手对象DataAbilityHelperhelper=DataAbilityHelper.creator(getContext());//定义图片源对象ImageSourceimageSource=null;//获取选中的Img对应的IdStringchooseImgId=空;//如果文件被选中,getUriString的结果为dataability:///com.android.providers.media.documents/document/image%3A437,其中%3A437为“:”的URL编码结果,后面的数字就是图片对应的Id//如果选择gallery,getUriString的结果是dataability:///media/external/images/media/262,最后是图片对应的Id//这里需要判断是否选择了文件或图库if(chooseImgUri.lastIndexOf("%3A")!=-1){chooseImgId=chooseImgUri.substring(chooseImgUri.lastIndexOf("%3A")+3);}else{chooseImgId=chooseImgUri.substring(chooseImgUri.lastIndexOf('/')+1);}//获取图片对应的uri。由于获取到的前缀是content,我们将其替换为相应的dataability前缀Uriuri=Uri.appendEncodedPathToUri(AVStorage.Images.Media.EXTERNAL_DATA_ABILITY_URI,cho??oseImgId);try{//读取图片FileDescriptorfd=helper.openFile(uri,"r");imageSource=ImageSource.create(fd,null);//创建位图PixelMappixelMap=imageSource.createPixelmap(null);//设置图片控件对应的Bitmapphoto.setPixelMap(像素地图);}catch(Exceptione){e.printStackTrace();}finally{if(imageSource!=null){imageSource.release();}}}}总结官网现有文档不多,鸿蒙的开发遇到很多问题,有Android基础的朋友可以参考Android的思路解决。应该可以事半功倍。希望这次分享对大家有所帮助。更多信息请访问:官方鸿蒙技术社区共建https://ost.51cto.com