无法从任何Android文件夹获取文件。

huangapple go评论127阅读模式
英文:

Not able to get files from any Android folder

问题

以下是翻译好的部分:

我是 Android 方面的新手,正在尝试测试一些东西。我有一个简单的应用程序,我只想从任何 Android 文件夹中读取文件。到目前为止,我只是检查了路径,我从以下位置得知:

context.getExternalFilesDir(null);

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)

获取到的路径类似于:

/storage/emulated/0/然后是其余的文件夹

我已经做了一切,已在清单文件中请求了权限,也在应用程序中请求了权限。我检查了即使权限已经被授予,文件夹路径也是正确的,但是 File.listFiles() 正在给我一个空指针异常。

请查看下面的代码:

在清单文件中的权限:

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

获取权限并尝试检查文件的代码:

if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 101);
    Toast.makeText(getApplicationContext(),
            "尚未获得权限",
            Toast.LENGTH_SHORT).show();
} else {
    Toast.makeText(getApplicationContext(),
            "权限已经被授予",
            Toast.LENGTH_SHORT).show();

    File dcimPath = new File("/storage/emulated/0/bluetooth"); //此路径是正确的

    File[] files = dcimPath.listFiles(); // 报空指针异常
    Log.i("文件数量:", files.length + "");
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == 101) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(getApplicationContext(),
                    "已授予权限",
                    Toast.LENGTH_SHORT).show();
            File dcimPath = new File("/storage/emulated/0/bluetooth/");

            File[] files = dcimPath.listFiles(); // 报错
            Log.i("文件数量:", files.length + "");
        } else {
            Toast.makeText(getApplicationContext(),
                    "权限被拒绝",
                    Toast.LENGTH_SHORT).show();
        }
    }
}

错误信息:

Caused by: java.lang.NullPointerException: Attempt to get length of null array
英文:

I am new in android and was trying to test something. I have this simple app, where I just want to read files from any android folder. Till now, I just checked how the paths are there which I came to know from the following

context.getExternalFilesDir(null);

&

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)

The paths obtained are like

/storage/emulated/0/ then rest of the folders

I have done everything, asked for permission in the manifest file, and in the app too. I check that even though the permission is granted and folder path is also correct but the File.listFiles() is givingme null pointer exception.

Please take a look at the code below:

Permission in manfest file

 &lt;uses-permission android:name=&quot;android.permission.READ_EXTERNAL_STORAGE&quot;/&gt;
&lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot;/&gt;

Code to get permission and try checking the files

 if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},101);
Toast.makeText(getApplicationContext(),
&quot;No permission yet&quot;,
Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(),
&quot;Permission already granted&quot;,
Toast.LENGTH_SHORT).show();
File dcimPath = new File(&quot;/storage/emulated/0/bluetooth&quot;); //This path is correct
File[] files = dcimPath.listFiles(); // gives null pointer exception
Log.i(&quot;files length &quot;,files.length+&quot;&quot;);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == 101){
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
Toast.makeText(getApplicationContext(),
&quot;Permission granted&quot;,
Toast.LENGTH_SHORT).show();
File dcimPath = new File(&quot;/storage/emulated/0/bluetooth/&quot;); 
File[] files = dcimPath.listFiles(); //gives error
Log.i(&quot;files length &quot;,files.length+&quot;&quot;);
}
else{
Toast.makeText(getApplicationContext(),
&quot;Permission denied&quot;,
Toast.LENGTH_SHORT).show();
}
}
}

ERROR

 Caused by: java.lang.NullPointerException: Attempt to get length of null array

答案1

得分: 1

请尝试将以下行添加到Android Manifest文件中的<application>标签中:

        android:requestLegacyExternalStorage="true"

这用于读取除应用程序私有存储之外的共享存储。如果您想要了解更多信息,请阅读这篇文章...

希望这对您有所帮助。如有疑问,请随意提问...

英文:

Try adding this line to &lt;application&gt; tag on the Android Manifest File

        android:requestLegacyExternalStorage=&quot;true&quot;

This is used to read Shared Storage apart from the app's private storage. Read this if you are curious to know more...

Hope this helps. Feel free to ask for clarifications...

huangapple
  • 本文由 发表于 2020年7月26日 18:33:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63098972.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定