赞
踩
在之前的分享中,曾经提到一个文件上传的问题
/data/user/0/cn.jltf.neighbor/files/534.jpg: open failed: ENOENT (No such file)
该文件不存在。但实际上该文件存在并能显示出来。后来发现是鸿蒙系统的权限问题,只能访问私有目录下的文件。
解决方法:将文件复制到私有目录下,再进行访问。
代码如下:
- HiLog.info(label,"私有目录:"+this.getFilesDir());
- HiLog.info(label,"缓存目录:"+this.getCacheDir());
- HiLog.info(label,"数据目录:"+this.getDataDir());
- HiLog.info(label,"外部目录:"+this.getExternalCacheDir());
- HiLog.info(label,"引用目录:"+this.getPreferencesDir());
- HiLog.info(label,"同步目录:"+this.getDistributedDir());
- HiLog.info(label,"应用目录:"+this.getDir("", Context.MODE_PRIVATE));
- File[] dataChildFiles = this.getDataDir().listFiles();
- for (File dataChildFile : dataChildFiles) {
- HiLog.info(label,"数据子目录:"+dataChildFile);
- }
- File[] externalFiles=this.getExternalMediaDirs();
- for (File externalFile : externalFiles) {
- HiLog.info(label,"外部Media目录:"+externalFile);
- }
-
- try {
- //该目录和JSUI中的internal://cache/目录是一个目录
- File file=new File(this.getCacheDir()+"/111.jpg");
- HiLog.info(label,"文件路径:"+file);
- if(file.exists())
- {
- HiLog.info(label,"文件已存在");
- return;
- }
- HiLog.info(label,"文件不存在");
- InputStream inputStream = getContext().getResourceManager().getResource(ResourceTable.Media_icon);
- ImageSource.SourceOptions srcOpts = new ImageSource.SourceOptions();
- srcOpts.formatHint = "image/jpeg";
- ImageSource imageSource = ImageSource.create(inputStream, srcOpts);
-
-
- // 设置图片参数
- ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
- decodingOptions.desiredSize=new Size(200,200);
- imageSource.createPixelmap(decodingOptions);
- //该种方式直接访问internal://app目录// FileOutputStream fos=new FileOutputStream("/data/user/0/com.example.abilitytransfertest/files//111.jpg");
- FileOutputStream fos=new FileOutputStream(this.getCacheDir()+"/111.jpg");
-
-
- ImagePacker imagePacker = ImagePacker.create();
- ImagePacker.PackingOptions packingOptions = new ImagePacker.PackingOptions();
- packingOptions.format = "image/jpeg";
- packingOptions.quality = 90;
- boolean result = imagePacker.initializePacking(fos, packingOptions);
- if(result)
- {
- result = imagePacker.addImage(imageSource.createPixelmap(decodingOptions));
- if (result) {
- long dataSize = imagePacker.finalizePacking();
- HiLog.info(label,"文件大小:"+dataSize);
- }
- }
-
- fos.flush();
- fos.close();
- if(file.exists())
- {
- HiLog.info(label,"文件已存在");
- return;
- }
- } catch (IOException | NotExistException e) {
- HiLog.info(label,"文件保存出错:"+e.getMessage());
- e.printStackTrace();
- }
-
- File file=this.getCacheDir();
- File[] files = file.listFiles();
- for (File file1 : files) {
- HiLog.info(label,"File目录:"+file1);
- }
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。