gpt4 book ai didi

java - 我无法读取用户存储,

转载 作者:行者123 更新时间:2023-12-01 16:56:19 24 4
gpt4 key购买 nike

我正在尝试列出用户存储中的所有文件夹和文件,我已在 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");
}
}
}
}

log cat: D/MainActivity: onCreate: Permission Granted 2020-05-04 07:21:09.680 27634-27634/co.ak.externalstorage D/MainActivity: dir: Yes, I am! 2020-05-04 07:21:09.680 27634-27634/co.ak.externalstorage D/MainActivity: dir: you can't read me! 2020-05-04 07:21:09.680 27634-27634/co.ak.externalstorage D/MainActivity: dir: You Can't write me

最佳答案

自 Android 7 以来,“/”根目录不可读。

为什么要调用该用户存储?

关于java - 我无法读取用户存储,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61584103/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com