问题
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