How to create a File Chooser Dialog with Android Studio

混江龙づ霸主 提交于 2020-08-11 22:29:32

问题


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.


回答1:


You can check this out : aFileChooser

Code:

Activity in Manifest

<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 Code

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;
 }
}

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>



回答2:


dependencies {
    compile 'org.apache.commons:commons-io:1.3.2'
}


来源:https://stackoverflow.com/questions/38170660/how-to-create-a-file-chooser-dialog-with-android-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!