我正在尝试列出用户存储中的所有文件夹和文件,我在android.manifest
文件中声明了READ_EXTERNAL_STORAGE权限,但我没有什么错,我无法读取用户存储
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = "MainActivity";
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnClickListener(this);
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[] {
Manifest.permission.READ_EXTERNAL_STORAGE
}, 123);
} else {
Log.d(TAG, "onCreate: Permission Granted");
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults) {
switch (requestCode) {
case 123:
if (grantResults != null && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Permission Denied",
Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
String path = "/";
File dir = new File(path);
if (dir.isDirectory()) {
Log.d(TAG, "dir: Yes, I am!");
}
if (dir.canRead()) {
Log.d(TAG, "dir: Read me!");
} else {
Log.d(TAG, "dir: you can't read me!");
}
if (dir.canWrite()) {
Log.d(TAG, "dir: use me!");
} else {
Log.d(TAG, "dir: You Can't write me");
}
}
}
}
日志猫:D / MainActivity:onCreate:授予权限2020-05-04
07:21:09.680 27634-27634 / co.ak.externalstorage D / MainActivity:目录:
我是! 2020-05-04 07:21:09.680 27634-27634 / co.ak.externalstorage
D / MainActivity:目录:您看不懂我! 2020-05-04 07:21:09.680
27634-27634 / co.ak.externalstorage D / MainActivity:目录:您无法编写
我
最佳答案
自Android 7起,无法读取“ /”根目录。
为什么要呼叫该用户存储?
关于java - 我看不到用户存储,,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61584103/