java - File.listFiles() 在 android 11 中返回 null

标签 java android java-io android-11

我正在创建一个应用程序来测试 File.listFiles()方法是否有效。为了检查这一点,我创建了一个应用程序并在那里使用了它,但是这个返回 null 代替了一个数组。
这是我的完整代码,请帮忙,我已授予 android 11 的所有权限
MainActivity.java

package com.rajkumarcreations.file;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.widget.TextView;
import android.widget.Toast;

import org.w3c.dom.Text;

import java.io.File;

import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
import static android.os.Build.VERSION.SDK_INT;

public class MainActivity extends AppCompatActivity {
TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.btn);
        if(!checkPermission()){
            requestPermission();
        }else{
            File file = new File(Environment.getExternalStorageDirectory()+"/Download/");
            File[] allfiles = null;
            allfiles = file.listFiles();
            if(file.exists()){
                tv.setText("Exist");
            }
            if(allfiles!=null){
                Toast.makeText(this, "length is "+allfiles.length, Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(this, "Array is null", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private boolean checkPermission() {
        if (SDK_INT >= Build.VERSION_CODES.R) {
            return Environment.isExternalStorageManager();
        } else {
            int write = ContextCompat.checkSelfPermission(MainActivity.this, WRITE_EXTERNAL_STORAGE);
            int read = ContextCompat.checkSelfPermission(MainActivity.this, READ_EXTERNAL_STORAGE);
            return write == PackageManager.PERMISSION_GRANTED && read == PackageManager.PERMISSION_GRANTED;
        }
    }

    private void requestPermission() {
        if (SDK_INT >= Build.VERSION_CODES.R) {
            try {
                Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
                intent.addCategory("android.intent.category.DEFAULT");
                intent.setData(Uri.parse(String.format("package:%s",new Object[]{getApplicationContext().getPackageName()})));
                startActivityForResult(intent, 2000);
            } catch (Exception e) {
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
                startActivityForResult(intent, 2000);
            }
        } else {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{WRITE_EXTERNAL_STORAGE,READ_EXTERNAL_STORAGE}, 333);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2000) {
            if (SDK_INT >= Build.VERSION_CODES.R) {
                if (Environment.isExternalStorageManager()) {

                    Toast.makeText(this, "Allow permissions granted", Toast.LENGTH_SHORT).show();

                } else {
                    Toast.makeText(this, "Allow permission for storage access!", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode,permissions,grantResults);
        if (requestCode==333){
            if (grantResults.length > 0) {
                boolean WRITE_EXTERNAL_STORAGE = grantResults[0] == PackageManager.PERMISSION_GRANTED;
                boolean READ_EXTERNAL_STORAGE = grantResults[1] == PackageManager.PERMISSION_GRANTED;
                if (READ_EXTERNAL_STORAGE && WRITE_EXTERNAL_STORAGE) {

                    Toast.makeText(this, "All permissions granted", Toast.LENGTH_SHORT).show();

                } else {
                    Toast.makeText(this, "Allow permission for storage access!", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}
list 文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rajkumarcreations.file">
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:requestLegacyExternalStorage="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>
XML 文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/btn"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

最佳答案

According to your requirement you were trying to access list of files under a directory by using listfiles() method. And want check it is working or not. But this returning null.


首先,在 list 中声明 MANAGE_EXTERNAL_STORAGE 权限。
// For 30 and after
<uses-permission
    android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
    tools:ignore="ScopedStorage" />

// For before 30
<uses-permission
    android:name="android.permission.READ_EXTERNAL_STORAGE"
    tools:node="merge" />
<uses-permission
    android:name="android.permission.STORAGE"
    tools:node="merge" />
//30 岁及以后

Second, Use the ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION intent action to direct users to a system settings page where they can enable the following option for your app: Allow access to manage all files.


在您的 onCreate 方法上编写代码:
if (Build.VERSION.SDK_INT < 30) {
    if (!checkBefore30()) {
        requestBefore30();
    } else { 
      // User granted file permission, Access your file
      readFiles();
    }
  } else if (Build.VERSION.SDK_INT >= 30) {
    check30AndAfter();
  } else {
    // User already has file access permission
    readFiles();
}
在你的 Activity 上写下这些方法:
private boolean checkBefore30() {
     return ContextCompat.checkSelfPermission(YourActivity.this,
         Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
}


private void requestBefore30() {
   if(ActivityCompat.shouldShowRequestPermissionRationale(YourActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
                    Toast.makeText(LoginActivity.this, "Storage permission required. Please allow this permission", Toast.LENGTH_LONG).show();
                    ActivityCompat.requestPermissions(YourActivity.this, 
                            new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 100);
                } else {
                    ActivityCompat.requestPermissions(YourActivity.this, 
                            new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 100);
   }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case 100:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // Permission for storage access successful!
                // Read your files now
            } else {
                // Allow permission for storage access!
            }
            break;
    }
}


@RequiresApi(api = Build.VERSION_CODES.R)
private void check30AndAfter() {
    if (!Environment.isExternalStorageManager()) {
        try {
            Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
            intent.addCategory("android.intent.category.DEFAULT");
            intent.setData(Uri.parse(String.format("package:%s", getApplicationContext().getPackageName())));
            startActivityForResult(intent, 200);
        } catch (Exception e) {
            Intent intent = new Intent();
            intent.setAction(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
            startActivityForResult(intent, 200);
        }
    }
}

@RequiresApi(api = Build.VERSION_CODES.R)
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 200) {
        if (30 >= Build.VERSION_CODES.R) {
            if (Environment.isExternalStorageManager()) {
                // Permission for storage access successful!
                // Read your files now
            } else {
                // Allow permission for storage access!
            }
        }
    }
}


private void readFiles() {
        File file;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString());
        } else {
            file = new File(Environment.getExternalStorageDirectory().toString() + "/Download/");
        }
        File[] files= null;
        files= file.listFiles();
        if (file.exists()) {
            //  files exist
        }
        if (allfiles != null) {
            Toast.makeText(this, " file length is" + files.length, Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "No files", Toast.LENGTH_SHORT).show();
        }
    }
希望您的问题将得到解决。

关于java - File.listFiles() 在 android 11 中返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68032841/

相关文章:

java - 如何在多用户环境中处理表单编辑?

java - Tomcat:@Overriden 日志过滤器混合输出及其非覆盖变体

android - 数据库中的表数(android)

java - 是否可以通过android studio中的字符串变量处理R.array.string-array-name?

java - 无效类异常

java - Java 编译器如何解析类型转换?

java - 如何将 LocalDate 转换为 ChronoZonedDateTime?

android - 计算android屏幕尺寸?

java - 减少 java 代码中打开文件的数量

java - 是否可以获取 IOException 后面的 errno?