首页 技术 正文
技术 2022年11月15日
0 收藏 326 点赞 2,670 浏览 1058 个字

使用cordova-plugin-file可以帮助我们方便的操作app中的图片等文件,分享一下我在用该插件从图库读取图片时遇到的一个平台相关的小问题。

使用场景,我会在APP中新增一张图片,会有一个可点击的【保存】按钮,点击时将图片copy到自己新建的目录下。

在Android平台下(小米5),选中图片之后,系统的路径形式是:“file://路径+图片名.后缀名+?+时间戳”的方式。而iOS平台是一个“file://路径+图片名.后缀名”的方式。

在Android平台下需要对图片路径字符串进行截取,把?后面的字符串截取调,只保留前面部分。

 // fileItem是一个图片对象,filePath是真正的路径
if($ionicPlatform.isIOS){
fileItem.filePath = fileItem.filePath.substring(0, fileItem.filePath.length);//iOS 平台选的图片生成路径中没有'?',返回-1, substring 不接受负值 会返回空 }else {
fileItem.filePath = fileItem.filePath.substring(0, fileItem.filePath.lastIndexOf("?"));
}
//或者
fileItem.filePath = fileItem.filePath.substring(0, fileItem.filePath.lastIndexOf("?")=== -1 ? fileItem.filePath.length : fileItem.filePath.lastIndexOf("?"));
//这样不同平台下,文件路径就可以个格式化为"file://.../image.jpeg"

不可统一写成

fileItem.filePath = fileItem.filePath.substring(0, fileItem.filePath.lastIndexOf(“?”));
这里在iOS平台下回返回-1,而substring不接受负数,否则会返回空数组[]。导致格式化的路径失败。

具体的cordova-plugin-file的更多操作文件的函数请参考

http://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/index.html#file-system-layouts

https://github.com/apache/cordova-plugin-file

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,941
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,465
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,280
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,094
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,728
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,765