文件存储
各位熟悉android的都知道安卓有内部存储和外部存储,Android Q 其中有一项修改就外部存储而言即:
Environment.getExternalStorageDirectory() 你不能直接使用了必须要Context.getExternalFilesDir(xxx)去访问且共有文件不能通过file直接使用,二私有目录可以使用file相关的api
关于上面所述的内部存储和外部存储不多说丢一个链接
https://blog.csdn.net/Kelaker/article/details/80471352
各位观众老爷慢慢看,自行体会!!!
还有关于uri相关的可以看看如下链接
https://blog.csdn.net/chengkaizone/article/details/51121355
适配之路
一般文件存储都可以通过Context.getExternalFilesDir(xxx)然后变成Uri或file从而进行访问;
但本地拍照功能需要你区别对待,目前就公司里面的适配出现两种uri的问题
android Q某些特定情况下通过Uri.fromFile(xxxxx)形成的uri作为照片输出的uri,这个只是特殊情况下适配。
绝大部分适配是走ContentValue+MediaStore+ContentProvider
不多说贴出相关代码自行体会
代码片段一
public class LocalImageQHandler implements AndroidQHandler {
private String filePath = "";
protected String fileNameWithSuffix = "";
public LocalImageQHandler(String filePath, String fileNameWithSuffix) {
this.filePath = filePath;
this.fileNameWithSuffix = fileNameWithSuffix;
}
private static final String TAG = "LocalImageHandler";
/******适配核心代码*******/
//sourcePath是指文件存储的路径
//fileNameWithSuffix是指文件的名称及后缀(xxxx.apk,xxxxx.mp4这类)
@Override
public Uri handle(Context context, String sourcePath, String fileNameWithSuffix, String saveDirName) {
Uri uri = null;
ContentValues values = new ContentValues();
//为了取后缀用的,目前发现不需要后缀强制png就行
String[] split = null;
if (!TextUtils.isEmpty(fileNameWithSuffix)) {
split = fileNameWithSuffix.split("\\.");
} else {
split = this.fileNameWithSuffix.split("\\.");
fileNameWithSuffix=this.fileNameWithSuffix;
}
values.put(MediaStore.Images.Media.DISPLAY_NAME, fileNameWithSuffix);
values.put(MediaStore.Images.Media.MIME_TYPE, getMimeType() + "png");
values.put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/" + saveDirName);
Uri external = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ContentResolver resolver = context.getContentResolver();
//适合Android10
try {
uri = resolver.insert(external, values);
} catch (IllegalArgumentException e) {
values.remove(MediaStore.Images.Media.RELATIVE_PATH);
uri = resolver.insert(external, values);
} finally {
return uri;
}
}
@Override
public String getMimeType() {
return "image/";
}
@Override
public String getFilePath() {
return filePath;
}
}
各位观众老爷应该明白了吧,直接保存到公用文件中去!!!!!
好了本地拍照适配基本告一段落
要是工程中有下载功能或者保存文件到本地的案例,不要着急
我就贴一个下载apk的关键代码作为参考,如果你是下载图片,下载其他文件,只要修改MediaStore就行了
代码片段二
public Uri handle(Context context, String sourcePath, String fileName, String saveDirName) {
if (!AndroidQHandler.isExternalStorageReadable()) {
Logger.e(TAG, "External storage cannot be written!");
return null;
}
ContentValues values = new ContentValues();
//显示名称
values.put(MediaStore.Downloads.DISPLAY_NAME, fileName);
//存储文件的类型
values.put(MediaStore.Downloads.MIME_TYPE, getMimeType());
//公有文件路径
values.put(MediaStore.Downloads.RELATIVE_PATH, "Download/" + saveDirName.replaceAll("/", "") + "/");
//生成一个Uri
Uri external = MediaStore.Downloads.EXTERNAL_CONTENT_URI;
ContentResolver resolver = context.getContentResolver();
//写入
Uri insertUri = resolver.insert(external, values);
if (insertUri == null) {
return null;
}
InputStream is = null;
OutputStream os = null;
try {
//输出流
os = resolver.openOutputStream(insertUri);
if (os == null) {
return null;
}
int read;
File sourceFile = new File(sourcePath);
if (sourceFile.exists()) { // 文件存在时
is = new FileInputStream(sourceFile); // 读入原文件
byte[] buffer = new byte[1444];
while ((read = is.read(buffer)) != -1) {
//写入uri中
os.write(buffer, 0, read);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return insertUri;
}
思路:
本地存储文件到私有目录即使用Context.getExternalFilesDir(xxx),然后通过MediaStore到共有的,并删除本地的文件,然后你安装时候的uri就是return的uri了
到结尾我再说一个适配原生webview的上传文件思路,一般情况不会出现该需求,既然提了适配存储,那就得做一下,可以参考下载文件的思路。
调用原生的选择文件传递的EXTRA_OUTPUT的uri就是通过Context.getExternalFilesDir(xxx)->file->uri
回调到数据时,拿这个uri通过代码片段二进行存到共有文件中去,然后再通过通过webview的接口传给html。
结论:适配存储要么以Context.getExternalFilesDir(xxx)存到私有文件中去,要么通过私有转到共有的在进行