易游网-易游模拟器

 找回密码
 立即注册
查看: 2076|回复: 0

[andriod] android访问外部存储新老版本区别

[复制链接]

3382

主题

3401

帖子

38

积分

超级版主

Rank: 8Rank: 8

积分
38

技术达人

发表于 2021-1-19 16:12:26 | 显示全部楼层 |阅读模式
apiversion < 29
Android Environment.getExternalStorageDirectory()
1、当有sd卡是时,获取的sd目录
2、当没有sd卡时,获取的是手机内部存储目录


api级别(使用SDK_VERSION区分)>=29,卸载引用的时移除文件。
android10推荐是用Context.getExternalFilesDir()访问特定于应用的目录中的文件

一、获取(创建)自身目录下的文件夹
获取及创建,如果手机中没有对应的文件夹,则系统会自动生成
//在自身目录下创建apk文件夹
File apkFile = context.getExternalFilesDir("apk");

二、创建自身目录下的文件
生成需要下载的路径,通过输入输出流读取写入
String apkFilePath = context.getExternalFilesDir("apk").getAbsolutePath();
File newFile = new File(apkFilePath + File.separator + "temp.apk");
OutputStream os = null;
try {
os = new FileOutputStream(newFile);
if (os != null) {
os.write("file is created".getBytes(StandardCharsets.UTF_8));
os.flush();
}
} catch (IOException e) {
} finally {
try {
if (os != null) {
os.close();
}
} catch (IOException e1) {
}
}

三、创建公共目录下的文件夹
通过MediaStore.insert写入
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
return null;
}
ContentResolver resolver = context.getContentResolver();
ContentValues values = new ContentValues();
values.put(MediaStore.Downloads.DISPLAY_NAME, fileName);
values.put(MediaStore.Downloads.DESCRIPTION, fileName);
//设置文件类型
values.put(MediaStore.Downloads.MIME_TYPE, "application/vnd.android.package-archive");
//注意MediaStore.Downloads.RELATIVE_PATH需要targetVersion=29,
//故该方法只可在Android10的手机上执行
values.put(MediaStore.Downloads.RELATIVE_PATH, "Download" + File.separator + "apk");
Uri external = MediaStore.Downloads.EXTERNAL_CONTENT_URI;
Uri insertUri = resolver.insert(external, values);
return insertUri;

四、公共目录下的指定文件夹下创建文件
结合上面代码,我们主要是在公共目录下创建文件或文件夹拿到本地路径uri,不同的Uri,可以保存到不同的公共目录中。接下来使用输入输出流就可以写入文件

重点:AndroidQ中不支持file://类型访问文件,只能通过uri方式访问

ContentResolver resolver = context.getContentResolver();
Uri insertUri = resolver.insert(external, values);
if(insertUri == null) {
return;
}
String mFilePath = insertUri.toString();
InputStream is = null;
OutputStream os = null;
try {
os = resolver.openOutputStream(insertUri);
if(os == null){
return;
}
int read;
File sourceFile = new File(sourcePath);
if (sourceFile.exists()) { // 文件存在时
is = new FileInputStream(sourceFile); // 读入原文件
byte[] buffer = new byte[1024];
while ((read = is.read(buffer)) != -1) {
os.write(buffer, 0, read);
}
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

五、通过MediaStore读取公共目录下的文件
ParcelFileDescriptor parcelFileDescriptor = null;
FileDescriptor fileDescriptor = null;
Bitmap tagBitmap = null;
try {
parcelFileDescriptor = context.getContentResolver().openFileDescriptor(uri, "r");
if (parcelFileDescriptor != null && parcelFileDescriptor.getFileDescriptor() != null) {
fileDescriptor = parcelFileDescriptor.getFileDescriptor();
//转换uri为bitmap类型
tagBitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (parcelFileDescriptor != null) {
parcelFileDescriptor.close();
}
} catch (IOException e) {
}
}

六、使用MediaStore删除文件
context.getContentResolver().delete(fileUri, null, null);

七、APP通过MediaStore访问文件所需要的权限
header 1        无权限        READ_EXTERNAL
Audio        可读写APP自己创建的文件,但不可直接使用路径访问        可以读其他APP创建的媒体类文件,删改操作需要用户授权
Image        可读写APP自己创建的文件,但不可直接使用路径访问        可以读其他APP创建的媒体类文件,删改操作需要用户授权
File        可读写APP自己创建的文件,但不可直接使用路径访问        不可读写其他APP创建的非媒体类文件
Downloads        可读写APP自己创建的文件,但不可直接使用路径访问        不可读写其他APP创建的非媒体类文件
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|易游网-易游模拟器 Copyright @2015-2021 ( 浙ICP备15028007号-1 )

GMT+8, 2024-5-8 03:23 , Processed in 0.036130 second(s), 8 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表