如何使用Android Studio创建文件选择器对话框 [英] How to create a File Chooser Dialog with Android Studio

查看:1163
本文介绍了如何使用Android Studio创建文件选择器对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我想创建一个文件选择器,在选择文件后,它会打开一个普通对话框,其中包含所选文件的路径。我尝试执行此项目,但我不知道该怎么做。

Today I want create a File Chosser that, after slected the file, It open a Normal Dialog with the path of the file selected. I tried to do this project, but I didn't understood how I can do.

推荐答案

您可以查看以下内容:< a href = https://github.com/iPaulPro/aFileChooser rel = nofollow> aFileChooser

You can check this out : aFileChooser

代码:

清单活动

<activity
android:name="com.ipaulpro.afilechooser.FileChooserActivity"
android:icon="@drawable/ic_chooser"
android:enabled="@bool/use_activity"
android:exported="true"
android:label="@string/choose_file" >
<intent-filter>
    <action android:name="android.intent.action.GET_CONTENT" />

    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.OPENABLE" />

    <data android:mimeType="*/*" />
</intent-filter>

Java代码

private static final int REQUEST_CHOOSER = 1234;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Create the ACTION_GET_CONTENT Intent
Intent getContentIntent = FileUtils.createGetContentIntent();

Intent intent = Intent.createChooser(getContentIntent, "Select a file");
startActivityForResult(intent, REQUEST_CHOOSER);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent     data) {
switch (requestCode) {
    case REQUEST_CHOOSER:   
        if (resultCode == RESULT_OK) {

            final Uri uri = data.getData();

            // Get the File path from the Uri
            String path = FileUtils.getPath(this, uri);

            // Alternatively, use FileUtils.getFile(Context, Uri)
            if (path != null && FileUtils.isLocal(path)) {
                File file = new File(path);
            }
        }
        break;
 }
}

如果要使用存储访问框架(API 19岁以上),将Ian Lake的LocalStorageProvider(包含在此库中)包含在您的< application>中:

If you want to use the Storage Access Framework (API 19+), include Ian Lake's LocalStorageProvider (included in this library) in your <application>:

<provider
    android:name="com.ianhanniballake.localstorage.LocalStorageProvider"
    android:authorities="com.ianhanniballake.localstorage.documents"
    android:enabled="@bool/use_provider"
    android:exported="true"
    android:grantUriPermissions="true"
    android:permission="android.permission.MANAGE_DOCUMENTS" >
        <intent-filter>
            <action android:name="android.content.action.DOCUMENTS_PROVIDER" />
        </intent-filter>
</provider>

这篇关于如何使用Android Studio创建文件选择器对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆