gpt4 book ai didi

Android:目录和文件选择器android库

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:00:34 25 4
gpt4 key购买 nike

我在我的应用程序中使用 FileChooser android 库项目从外部存储中选择文件。但它似乎并没有只选择目录让用户选择下载位置来下载文件。有没有同时支持pick file和pick directory的android库项目?

我知道这里已经回答了多个关于文件选择器或目录选择器的问题,但经过大量搜索后,我找不到目录和文件选择器的问题。任何帮助将不胜感激。

最佳答案

我没有 android 库项目,但你可以简单地使用下一个代码制作你自己的文件选择器。此代码将要求您选择一个文件浏览器,当您在文件浏览器中选择一个文件时,您将在 FilePath 字符串中的 onActivityResult 函数中获取路径。

创建此公共(public):

private static final int ACTIVITY_CHOOSE_FILE = 3;

单击按钮时,您可以调用它:

            Intent chooseFile;
Intent intent;
chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
chooseFile.setType("file/*");
intent = Intent.createChooser(chooseFile, "Choose a file");
startActivityForResult(intent, ACTIVITY_CHOOSE_FILE);

您可以使用以下代码捕获目录:

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) return;
String path = "";
if(requestCode == ACTIVITY_CHOOSE_FILE)
{
Uri uri = data.getData();
String FilePath = getRealPathFromURI(uri);

}
}

public String getRealPathFromURI(Uri contentUri) {
String [] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query( contentUri, proj, null, null,null);
if (cursor == null) return null;
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}

编辑:如果你不想使用外部文件浏览器,你可以将这个 android 库导入到你的项目中: https://code.google.com/p/afiledialog/

关于Android:目录和文件选择器android库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22619325/

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