gpt4 book ai didi

java - Android 存储 - 即使设置了权限,也无法读取应用程序目录之外的目录文件

转载 作者:行者123 更新时间:2023-12-01 09:47:54 25 4
gpt4 key购买 nike

尝试获取外部存储目录下的文件列表会导致没有文件,即使它们存在并且已设置读取该目录的权限。

给出以下示例:

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

File ed = Environment.getExternalStorageDirectory();

File[] fs = ed.listFiles();
Log.i(ed.getAbsolutePath(), (fs != null ? fs.length : "no") + " files found");

}
}

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test">

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

app/build.gradle

android {
compileSdkVersion 23
buildToolsVersion "23.0.3"

defaultConfig {
applicationId "com.test"
minSdkVersion 11
targetSdkVersion 23
...

在物理设备(Nexus 5,Android 6.0.1)上测试,这是 logcat:

06-15 11:42:17.309 16732-16732/com.test I//storage/emulated/0: no files found

没有其他警告或错误。

通过adb shell工具我可以看到以下子目录及其权限

$ cd /storage/emulated/
$ ls -l
drwxrwx--x root sdcard_rw 1970-03-05 14:05 Alarms
drwxrwx--x root sdcard_rw 2015-09-10 23:44 Android
drwxrwx--x root sdcard_rw 2015-08-11 18:40 BDArenaConnector
drwxrwx--x root sdcard_rw 2015-02-03 14:14 DCIM
drwxrwx--x root sdcard_rw 2016-06-14 10:59 Download
drwxrwx--x root sdcard_rw 2015-09-15 11:48 MOGO_AUDIANCE
drwxrwx--x root sdcard_rw 2015-06-19 11:49 Manga Mobile
drwxrwx--x root sdcard_rw 2015-10-19 21:02 Memes
drwxrwx--x root sdcard_rw 1970-03-05 14:05 Movies
drwxrwx--x root sdcard_rw 2016-05-24 11:01 Music
drwxrwx--x root sdcard_rw 2015-01-12 20:43 Notifications
drwxrwx--x root sdcard_rw 2015-11-24 19:19 PerfectViewer
drwxrwx--x root sdcard_rw 2016-06-01 16:58 Pictures
drwxrwx--x root sdcard_rw 1970-03-05 14:05 Podcasts
drwxrwx--x root sdcard_rw 2015-04-30 10:16 ROMs
drwxrwx--x root sdcard_rw 2015-01-12 20:36 Ringtones
drwxrwx--x root sdcard_rw 2015-08-10 18:57 SKMaps
drwxrwx--x root sdcard_rw 2015-09-15 11:48 ShareSDK
drwxrwx--x root sdcard_rw 2015-08-16 10:40 Telegram
drwxrwx--x root sdcard_rw 2015-05-29 21:36 TuneIn Radio
drwxrwx--x root sdcard_rw 2016-05-27 00:08 WhatsApp
drwxrwx--x root sdcard_rw 2015-05-20 09:21 airdroid
drwxrwx--x root sdcard_rw 2016-05-31 16:59 backups
drwxrwx--x root sdcard_rw 2015-11-10 14:42 bluetooth
drwxrwx--x root sdcard_rw 2015-11-15 19:43 com.facebook.katana
drwxrwx--x root sdcard_rw 2015-08-10 18:44 > com.facebook.orca
drwxrwx--x root sdcard_rw 2015-09-15 11:51 ktplay
drwxrwx--x root sdcard_rw 2015-04-23 20:30 media
drwxrwx--x root sdcard_rw 1970-03-05 14:04 obb
drwxrwx--x root sdcard_rw 2015-02-07 19:11 panoramas
drwxrwx--x root sdcard_rw 2015-06-19 11:53 smart-tools
drwxrwx--x root sdcard_rw 2015-09-28 12:56 wp

我知道这个目录应该可供应用程序读取,因为其他一些应用程序可以毫无问题地执行此操作(例如 Astro 文件管理器或 PerfectViewer)。

最佳答案

再次针对 6.0 进行编译时需要运行时权限

    public class MainActivity extends AppCompatActivity {

private static final int REQUEST_RUNTIME_PERMISSION = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

checkPremission();
}


void checkPremission() {
//select which permission you want
final String permission = Manifest.permission.WRITE_EXTERNAL_STORAGE;
// if in fragment use getActivity()
if (ContextCompat.checkSelfPermission(ActivityName.this, permission)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(ActivityName.this, permission)) {

} else {
ActivityCompat.requestPermissions(ActivityName.this, new String[]{permission}, REQUEST_CAMERA_PERMISSION);
}
} else {
// you have permission go ahead
File ed = Environment.getExternalStorageDirectory();
File[] fs = ed.listFiles();
Log.i(ed.getAbsolutePath(), (fs != null ? fs.length : "no") + " files found");
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_RUNTIME_PERMISSION:
final int numOfRequest = grantResults.length;
final boolean isGranted = numOfRequest == 1
&& PackageManager.PERMISSION_GRANTED == grantResults[numOfRequest - 1];
if (isGranted) {
// you have permission go ahead
File ed = Environment.getExternalStorageDirectory();

File[] fs = ed.listFiles();
Log.i(ed.getAbsolutePath(), (fs != null ? fs.length : "no") + " files found");
}else{
// you dont have permission show toast
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}

关于java - Android 存储 - 即使设置了权限,也无法读取应用程序目录之外的目录文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37832453/

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