diff --git a/README.md b/README.md index 7d198379..d9efaa3c 100644 --- a/README.md +++ b/README.md @@ -169,34 +169,14 @@ you like.. ### Handling the result -If you have a minimum requirement of Jelly Bean (API 16) and above, -you can skip the second method. +You can use the included utility method to parse the activity result: ```java protected void onActivityResult(int requestCode, int resultCode, Intent intent) { if (requestCode == FILE_CODE && resultCode == Activity.RESULT_OK) { - if (!data.getBooleanExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false)) { - // The URI will now be something like content://PACKAGE-NAME/root/path/to/file - Uri uri = intent.getData(); - // A utility method is provided to transform the URI to a File object - File file = com.nononsenseapps.filepicker.Utils.getFileForUri(uri); - // If you want a URI which matches the old return value, you can do - Uri fileUri = Uri.fromFile(file); - // Do something with the result... - } else { - // Handling multiple results is one extra step - ArrayList paths = data.getStringArrayListExtra(FilePickerActivity.EXTRA_PATHS); - if (paths != null) { - for (String path: paths) { - Uri uri = Uri.parse(path); - // Do something with the URI - File file = com.nononsenseapps.filepicker.Utils.getFileForUri(uri); - // If you want a URI which matches the old return value, you can do - Uri fileUri = Uri.fromFile(file); - // Do something with the result... - } - } - } + // Use the provided utility method to parse the result + List files = Utils.getSelectedFilesFromResult(data); + // Do something with the result... } } ``` diff --git a/library/src/main/java/com/nononsenseapps/filepicker/Utils.java b/library/src/main/java/com/nononsenseapps/filepicker/Utils.java index 897df54a..4b18d982 100644 --- a/library/src/main/java/com/nononsenseapps/filepicker/Utils.java +++ b/library/src/main/java/com/nononsenseapps/filepicker/Utils.java @@ -1,5 +1,6 @@ package com.nononsenseapps.filepicker; +import android.content.Intent; import android.net.Uri; import android.support.annotation.NonNull; import android.support.annotation.Nullable; @@ -7,6 +8,11 @@ import java.io.File; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import static com.nononsenseapps.filepicker.AbstractFilePickerActivity.EXTRA_ALLOW_MULTIPLE; +import static com.nononsenseapps.filepicker.AbstractFilePickerActivity.EXTRA_PATHS; /** * Some utility methods @@ -89,4 +95,26 @@ public static File getFileForUri(@NonNull Uri uri) { return file; } + + /** + * Parses the returned files from a filepicker activity into a nice list + * + * @param data returned by the {@link AbstractFilePickerActivity} + * @return a {@link List} of files (uris) which the user selected in the picker. + */ + @NonNull + public static List getSelectedFilesFromResult(@NonNull Intent data) { + List result = new ArrayList<>(); + if (data.getBooleanExtra(EXTRA_ALLOW_MULTIPLE, false)) { + List paths = data.getStringArrayListExtra(EXTRA_PATHS); + if (paths != null) { + for (String path : paths) { + result.add(Uri.parse(path)); + } + } + } else { + result.add(data.getData()); + } + return result; + } } diff --git a/sample/src/main/java/com/nononsenseapps/filepicker/sample/NoNonsenseFilePicker.java b/sample/src/main/java/com/nononsenseapps/filepicker/sample/NoNonsenseFilePicker.java index fb33aa36..6dff3478 100644 --- a/sample/src/main/java/com/nononsenseapps/filepicker/sample/NoNonsenseFilePicker.java +++ b/sample/src/main/java/com/nononsenseapps/filepicker/sample/NoNonsenseFilePicker.java @@ -8,7 +8,6 @@ import android.annotation.TargetApi; import android.app.Activity; -import android.content.ClipData; import android.content.Intent; import android.databinding.DataBindingUtil; import android.net.Uri; @@ -18,7 +17,6 @@ import android.view.Menu; import android.view.MenuItem; import android.view.View; - import com.dropbox.client2.DropboxAPI; import com.dropbox.client2.android.AndroidAuthSession; import com.nononsenseapps.filepicker.AbstractFilePickerFragment; @@ -37,7 +35,7 @@ import com.nononsenseapps.filepicker.sample.root.SUPickerActivity; import com.nononsenseapps.filepicker.sample.root.SUPickerActivity2; -import java.util.ArrayList; +import java.util.List; public class NoNonsenseFilePicker extends Activity { @@ -226,52 +224,20 @@ protected void onActivityResult(int requestCode, int resultCode, // Checking for the requestCodes is a bit redundant but good style if (resultCode == Activity.RESULT_OK && (CODE_SD == requestCode || CODE_DB == requestCode || CODE_FTP == requestCode)) { - // If we handled multiple files, we need to get the result differently - if (data.getBooleanExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false)) { - // This is the typical style on Android 4.2 and above - if (useClipData && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { - Log.i("SAMPLEAPP", "onActivityResult: Using ClipData"); - ClipData clip = data.getClipData(); - StringBuilder sb = new StringBuilder(); - - // clip data CAN be null in case of an empty result - if (clip != null) { - for (int i = 0; i < clip.getItemCount(); i++) { - if (i > 0) { - sb.append("\n"); - } - Uri uri = clip.getItemAt(i).getUri(); - sb.append(CODE_SD == requestCode ? - Utils.getFileForUri(uri).toString() : - uri.toString()); - } - } - - binding.text.setText(sb.toString()); - } else /* This style is available in all SDK versions */ { - Log.i("SAMPLEAPP", "onActivityResult: Using StringExtras"); - ArrayList paths = data.getStringArrayListExtra( - FilePickerActivity.EXTRA_PATHS); - StringBuilder sb = new StringBuilder(); - - if (paths != null) { - for (String path : paths) { - if (sb.length() > 0) { - sb.append("\n"); - } - sb.append(CODE_SD == requestCode ? - Utils.getFileForUri(Uri.parse(path)).toString() : - path); - } - } - binding.text.setText(sb.toString()); + // Use the provided utility method to parse the result + List files = Utils.getSelectedFilesFromResult(data); + + // Do something with your list of files here + StringBuilder sb = new StringBuilder(); + for (Uri uri : files) { + if (sb.length() > 0) { + sb.append("\n"); } - } else /* Single file mode */ { - binding.text.setText(CODE_SD == requestCode ? - Utils.getFileForUri(data.getData()).toString() : - data.getDataString()); + sb.append(CODE_SD == requestCode ? + Utils.getFileForUri(uri).toString() : + uri.toString()); } + binding.text.setText(sb.toString()); } } - }