简单的Android目录选择器-如何? [英] Simple Android Directory picker - How?
问题描述
我刚开始在Android Studio中进行编码,感觉棒极了.. !!
如何为目录选择器"编写代码.即,单击按钮后,将显示一个简单的对话/活动"屏幕,其中可以显示目录列表.
此外,要将该目录中的所有文件存储到Array变量中. (一旦单击确定"按钮).
PS:我在这里搜索过,发现一些很酷的文件选择",但是我正在寻找目录选择器.
谢谢.
尝试使用Intent.ACTION_OPEN_DOCUMENT_TREE
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
Intent i = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
i.addCategory(Intent.CATEGORY_DEFAULT);
startActivityForResult(Intent.createChooser(i, "Choose directory"), 9999);
}
然后从onActivityResult data.getData()获取结果Uri
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case 9999:
Log.i("Test", "Result URI " + data.getData());
break;
}
}
I have just started coding in Android Studio and feeling Awesome..!!
How can I write a code for a 'Directory Picker'. i.e., When a button is clicked, a simple Dialog/Activity screen which can show list of directories.
Also, want to store all the files in that directory in to an Array variable. (Once OK button is clicked).
PS: I have searched here and found some cool 'File choose' but m looking for Directory Chooser..!
Thanks in advance.
Try to use Intent.ACTION_OPEN_DOCUMENT_TREE
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
Intent i = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
i.addCategory(Intent.CATEGORY_DEFAULT);
startActivityForResult(Intent.createChooser(i, "Choose directory"), 9999);
}
And get result Uri from onActivityResult data.getData()
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case 9999:
Log.i("Test", "Result URI " + data.getData());
break;
}
}
这篇关于简单的Android目录选择器-如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!