在安卓11中,File.listFiles()方法返回null。

7

我正在创建一个应用程序来测试File.listFiles()方法是否正常工作。为了检查这一点,我制作了一个应用程序并在其中使用了它,但是它返回null而不是数组。

这是我的完整代码,请帮忙看看。我已经授予安卓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();
                }
            }
        }
    }
}

清单文件

<?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>

folder.listfiles() 返回了 null。是哪个目录? - blackapps
尝试使用 File file = Environment.getExternalStorageDirectory(); 在 Android 11 设备上列出外部存储的根目录,您将获得所有公共目录(包括下载目录)。而且,您无需任何权限即可执行此操作。 - blackapps
你确定返回的是 null 而不是空数组吗?因为我更倾向于期望一个空数组。 - blackapps
folder.listfiles()对于每个目录都返回了。 - Rajkumar
因此,请了解allfiles==null和allfiles.length==0之间的区别。后者将是一个空数组。 - blackapps
显示剩余12条评论
2个回答

5
根据您的要求,您试图使用listfiles()方法访问目录下的文件列表,并想要检查它是否正常工作。但是它返回了null首先,在清单中声明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及以上版本

第二,使用 ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION 意图操作,将用户引导到系统设置页面,在该页面上他们可以为您的应用启用以下选项:允许访问管理所有文件。

在您的 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();
        }
    }

希望您的问题得到解决。


谢谢,你救了我的一天。你的源代码可以工作,但仍需要通过Google Play控制台验证使用MANAGE_EXTERNAL_STORAGE :) - Djen Nik

1
尝试使用以下代码,它可以正常工作。

 //Add below permission in your manifest file
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />


//Add below attribute in manifest application tag
 android:requestLegacyExternalStorage="true"

//Activity code, ask file read/write runtime permission
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    requestForPermission();
    getFile();
}

private void getFile() {
    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[] 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 void requestForPermission() {
    ActivityCompat.requestPermissions(
            this,
            new String[]{
                    android.Manifest.permission.READ_EXTERNAL_STORAGE,
                    android.Manifest.permission.WRITE_EXTERNAL_STORAGE
            },
            101
    );
}


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode) {
        case 101:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                getFile();
            } else {
                //not granted
            }
            break;
        default:
            break;
    }
}
}

1
好的,但如果我提供路径/storage/emulated/0/whatsapp/Media/.statuses呢? 实际上,我正在创建一个可以保存WhatsApp状态的应用程序,我成功地创建了它,但在Android 9之前它仍然有效,但在10和11上却不行,所以我遇到了这个错误,这就是为什么我一直在寻找解决方法的原因。 - Rajkumar
你好,你解决了吗? - Vivek
我到现在还没有解决它。 - Rajkumar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接