英文:
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
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
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(),
"No permission yet",
Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(),
"Permission already granted",
Toast.LENGTH_SHORT).show();
File dcimPath = new File("/storage/emulated/0/bluetooth"); //This path is correct
File[] files = dcimPath.listFiles(); // gives null pointer exception
Log.i("files length ",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(),
"Permission granted",
Toast.LENGTH_SHORT).show();
File dcimPath = new File("/storage/emulated/0/bluetooth/");
File[] files = dcimPath.listFiles(); //gives error
Log.i("files length ",files.length+"");
}
else{
Toast.makeText(getApplicationContext(),
"Permission denied",
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 <application>
tag on the Android Manifest
File
android:requestLegacyExternalStorage="true"
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...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论