Android11及更高版本:通过作用域存储创建zip文件
创始人
2024-08-20 10:30:21
0

在Android 11及更高版本中,由于安全和隐私原因,应用程序不再具有对外部存储的常规访问权限。因此,如果要创建zip文件,则需要使用Scoped Storage。以下是实现这一目标的步骤:

  1. 在清单文件中启用“MANAGE_EXTERNAL_STORAGE”权限。

  1. 获取文件访问权限。
if (Environment.isExternalStorageManager()) {
    // Create zip file via Scoped Storage
} else {
    Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
    Uri uri = Uri.fromParts("package", getPackageName(), null);
    intent.setData(uri);
    startActivity(intent);
}
  1. 创建zip文件。
OutputStream os = getContentResolver().openOutputStream(zipUri);
ZipOutputStream zipOutputStream = new ZipOutputStream(os);
ZipEntry zipEntry = new ZipEntry(fileName);
zipOutputStream.putNextEntry(zipEntry);
FileInputStream inputStream = new FileInputStream(file);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
    zipOutputStream.write(buffer, 0, bytesRead);
}
zipOutputStream.closeEntry();
zipOutputStream.finish();
zipOutputStream.close();

在上述代码中,zipUri是zip文件的Uri,fileName是zip文件的名称,file是要被添加到zip文件中的文件。

通过以上步骤,我们可以在Android 11及更高版本中使用Scoped Storage创建zip文件。

相关内容

热门资讯

重要通知!xpoker怎么作弊... 重要通知!xpoker怎么作弊,德州局怎么透视,细节方法(有挂技巧);德州局怎么透视软件透明挂更新新...
教学盘点!we poker辅助... 教学盘点!we poker辅助器v3.3,aapoker透视方法,详细教程(有挂教程)科技教程也叫必...
热点推荐!hhpkoer辅助器... 热点推荐!hhpkoer辅助器视频,wepoker破解版内购,必胜教程(有挂神器);1.hhpkoe...
三分钟了解!wejoker辅助... 1、三分钟了解!wejoker辅助脚本,wpk作弊最怕三个东西,新2025教程(有挂攻略)(UU p...
盘点十款!wpk俱乐部辅助器,... 盘点十款!wpk俱乐部辅助器,wepoker免费透视,攻略教程(有挂透视);1分钟了解详细教程(微信...
免费测试版!约局吧德州有挂吗,... 免费测试版!约局吧德州有挂吗,拱趴大菠萝辅助神器,详细教程(有挂神器);小薇(透视辅助)致您一封信;...
玩家攻略推荐!poker wo... 玩家攻略推荐!poker world辅助,hhpkoer辅助挂是真的吗,我来教教你(有挂技巧);1分...
最新研发!哈糖大菠萝怎么开挂,... 最新研发!哈糖大菠萝怎么开挂,哈糖大菠萝挂,教你攻略(有挂辅助);致您一封信;亲爱哈糖大菠萝怎么开挂...
查到实测辅助!wepoker辅... 查到实测辅助!wepoker辅助器官方,wepoker线上大神,系统教程(有挂神器)这是由厦门游乐互...
总算清楚!pokermaste... 1、总算清楚!pokermaster修改器,hhpoker德州透视,力荐教程(有挂方法)。2、pok...