gpt4 book ai didi

java - Android EACCES(权限被拒绝)仅在某些设备上

转载 作者:行者123 更新时间:2023-12-01 17:17:16 26 4
gpt4 key购买 nike

我需要在外部存储中创建一个文件夹并解压缩一个文档。在某些设备上,如荣耀 10、三星 J7 和小米 A2,它可以完美运行,但在其他设备上,如华为 Mate 20 和三星 S8,既不能创建文件夹,也不能解压缩。

这些是 AndroidManifest.xml 中请求的权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

他们在<application>之外标签

在 MainActivity 中我请求权限

private boolean checkPermissions() {
if (ActivityCompat.checkSelfPermission(ActivityCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) ==
PackageManager.PERMISSION_GRANTED) {
return true;
}
requestPermissions();
return false;
}

private void requestPermissions() {
ActivityCompat.requestPermissions(
this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSION_ID
);
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == LOCATION_PERMISSION_ID) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
setFiles();
}
}
}

获得权限后,我执行 setFiles();这将创建文件夹、.nomedia 文件并将videos.zip 解压到创建的文件夹中

public void setFiles(){
createFolder();
unzip();
}

private void createFolder() {
//Create Folder
File folder = new File(Environment.getExternalStorageDirectory().toString()+"/.videoFiles");
folder.mkdirs();

//Save the path as a string value
extStorageDirectory = folder.toString();

createNomedia();
}

private void createNomedia() {
String filepath = extStorageDirectory;

File file = new File(filepath+ "/.nomedia");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}

private boolean unzip() {
String zipFile = Environment.getExternalStorageDirectory().toString() + "/videos.zip";
try{
FileUnzipper.unzip(zipFile, extStorageDirectory);
unzipComplete = true;
} catch (IOException ex){
Log.e("unzipping", "unzip Exception" + ex);
unzipComplete = false;

}
}

这是 FileUnzipper 类:

import android.util.Log;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import static android.content.ContentValues.TAG;

public class FileUnzipper{

final static int BUFFER_SIZE = 2048;

public static void unzip(String zipFile, String location) throws IOException {
int size;
byte[] buffer = new byte[BUFFER_SIZE];

try {
if ( !location.endsWith(File.separator) ) {
location += File.separator;
}
File f = new File(location);
if(!f.isDirectory()) {
f.mkdirs();
}
ZipInputStream zin = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile), BUFFER_SIZE));
try {
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
String path = location + ze.getName();
File unzipFile = new File(path);

if (ze.isDirectory()) {
if(!unzipFile.isDirectory()) {
unzipFile.mkdirs();
}
} else {
// check for and create parent directories if they don't exist
File parentDir = unzipFile.getParentFile();
if ( null != parentDir ) {
if ( !parentDir.isDirectory() ) {
parentDir.mkdirs();
}
}

// unzip the file
FileOutputStream out = new FileOutputStream(unzipFile, false);
BufferedOutputStream fout = new BufferedOutputStream(out, BUFFER_SIZE);
try {
while ( (size = zin.read(buffer, 0, BUFFER_SIZE)) != -1 ) {
fout.write(buffer, 0, size);
}

zin.closeEntry();
}
finally {
fout.flush();
fout.close();
}
}
}
}
finally {
zin.close();
}
}
catch (Exception e) {
Log.e(TAG, "Unzip exception", e);
}
}
}

现在在某些设备上这一切都有效,文件夹和 .nomedia 文件已创建,videos.zip 已解压缩,在其他设备上我得到:

E/ContentValues: Unzip exception
java.io.FileNotFoundException: /storage/emulated/0/videos.zip: open failed: EACCESS (Permission denied)
at libcore.io.IOBridge.open(IoBridge.java:496)
at java.io.FileInputStream.<init>(FileInputStream.java:159)
at java.io.FileInputStream.<init>(FileInputStream.java:115)
at com.example.test.FileUnzipper.unzip(FileUnzipper.java:32)

FileUnzipper.java:32 是 ZipInputStream zin = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipFile), BUFFER_SIZE));

我不明白为什么它在某些设备上不起作用,而且我也不知道如何修复它......

最佳答案

尝试使用 requestLegacyExternalStorage 选择退出范围存储:

<manifest ... >
<application android:requestLegacyExternalStorage="true" ... >
...
</application>
</manifest>

并参见 data and file storage overview .

关于java - Android EACCES(权限被拒绝)仅在某些设备上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61360926/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com