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文件。

相关内容

热门资讯

透视透视挂!德普之星app安卓... 透视透视挂!德普之星app安卓版破解版(透视)确实有挂(详细辅助系统教程)1、德普之星app安卓版破...
透视好友!wepoker透视脚... 透视好友!wepoker透视脚本免费下载(透视)好像有挂(详细辅助德州论坛);运wepoker透视脚...
透视教程!wejoker辅助器... 透视教程!wejoker辅助器怎么卖(透视)原来有挂(详细辅助科技教程);软件透明挂微扑克wpk插件...
透视能赢!pokemmo辅助器... 透视能赢!pokemmo辅助器脚本下载(透视)其实真的有挂(详细辅助AI教程)1、系统规律教程、辅助...
透视总结!pokemmo辅助器... 透视总结!pokemmo辅助器脚本下载(透视)一贯是有挂(详细辅助必胜教程)1、透视辅助简单,软件透...
透视app!wejoker手机... 透视app!wejoker手机版透视脚本(透视)都是是有挂(详细辅助曝光教程)1、透视辅助简单,软件...
透视线上!we-poker辅助... 透视线上!we-poker辅助(透视)真是真的是有挂(详细辅助详细教程)1、实时开挂更新:用户可以随...
透视攻略!aapoker怎么开... 透视攻略!aapoker怎么开辅助器(透视)竟然是真的有挂(详细辅助可靠教程)1. ai辅助创建新账...
透视免费!hhpoker万能辅... 透视免费!hhpoker万能辅助器(透视)好像有挂(详细辅助必备教程)1、玩家可以在hhpoker万...
透视代打!德普之星有透视辅助吗... 透视代打!德普之星有透视辅助吗(透视)真是有挂(详细辅助科技教程);1、透视辅助简单,软件透明挂操作...