How To Create A File Chooser Dialog With 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
Solution 1:
You can check this out : aFileChooser
Code:
Activity in Manifest
<activityandroid: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><actionandroid:name="android.intent.action.GET_CONTENT" /><categoryandroid:name="android.intent.category.DEFAULT" /><categoryandroid:name="android.intent.category.OPENABLE" /><dataandroid:mimeType="*/*" /></intent-filter>
Java Code
privatestaticfinalintREQUEST_CHOOSER=1234;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create the ACTION_GET_CONTENT IntentIntentgetContentIntent= FileUtils.createGetContentIntent();
Intentintent= Intent.createChooser(getContentIntent, "Select a file");
startActivityForResult(intent, REQUEST_CHOOSER);
}
@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CHOOSER:
if (resultCode == RESULT_OK) {
finalUriuri= data.getData();
// Get the File path from the UriStringpath= FileUtils.getPath(this, uri);
// Alternatively, use FileUtils.getFile(Context, Uri)if (path != null && FileUtils.isLocal(path)) {
Filefile=newFile(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>:
<providerandroid: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><actionandroid:name="android.content.action.DOCUMENTS_PROVIDER" /></intent-filter></provider>
Solution 2:
dependencies {
compile'org.apache.commons:commons-io:1.3.2'
}
Post a Comment for "How To Create A File Chooser Dialog With Android Studio"