From 345ac2f477e5b1ab9bcbf0923f68a98790e7c7ad Mon Sep 17 00:00:00 2001 From: F43nd1r Date: Wed, 9 Nov 2016 13:48:36 +0100 Subject: [PATCH 1/3] add static helper method --- .../filepicker/FilePickerActivity.java | 30 +++++++++++ .../sample/NoNonsenseFilePicker.java | 54 +++---------------- 2 files changed, 36 insertions(+), 48 deletions(-) diff --git a/library/src/main/java/com/nononsenseapps/filepicker/FilePickerActivity.java b/library/src/main/java/com/nononsenseapps/filepicker/FilePickerActivity.java index 00293d5f..5f0a8549 100644 --- a/library/src/main/java/com/nononsenseapps/filepicker/FilePickerActivity.java +++ b/library/src/main/java/com/nononsenseapps/filepicker/FilePickerActivity.java @@ -7,10 +7,16 @@ package com.nononsenseapps.filepicker; import android.annotation.SuppressLint; +import android.content.ClipData; +import android.content.Intent; +import android.net.Uri; +import android.os.Build; import android.os.Environment; import android.support.annotation.Nullable; import java.io.File; +import java.util.ArrayList; +import java.util.List; @SuppressLint("Registered") public class FilePickerActivity extends AbstractFilePickerActivity { @@ -30,4 +36,28 @@ protected AbstractFilePickerFragment getFragment( mode, allowMultiple, allowCreateDir, allowExistingFile, singleClick); return fragment; } + + + @SuppressWarnings("unused") + public static List getActivityResult(Intent data) { + List result = new ArrayList<>(); + if (data.getBooleanExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false)) { + ClipData clip; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && (clip = data.getClipData()) != null) { + for (int i = 0; i < clip.getItemCount(); i++) { + result.add(clip.getItemAt(i).getUri()); + } + } else { + List paths = data.getStringArrayListExtra(FilePickerActivity.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..2b946b6b 100644 --- a/sample/src/main/java/com/nononsenseapps/filepicker/sample/NoNonsenseFilePicker.java +++ b/sample/src/main/java/com/nononsenseapps/filepicker/sample/NoNonsenseFilePicker.java @@ -222,55 +222,13 @@ public boolean onOptionsItemSelected(MenuItem item) { @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { - // Always check the 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()); - } - } else /* Single file mode */ { - binding.text.setText(CODE_SD == requestCode ? - Utils.getFileForUri(data.getData()).toString() : - data.getDataString()); + if ((CODE_SD == requestCode || CODE_DB == requestCode || CODE_FTP == requestCode) && + resultCode == Activity.RESULT_OK) { + StringBuilder sb = new StringBuilder(); + for (Uri uri : FilePickerActivity.getActivityResult(data)){ + sb.append(uri).append('\n'); } + binding.text.setText(sb.toString()); } } From a8ab9db6e7ab38d87b0fea3a09aaa64d3fd14b59 Mon Sep 17 00:00:00 2001 From: Jonas Kalderstam Date: Fri, 7 Apr 2017 23:10:00 +0200 Subject: [PATCH 2/3] Move helper method to Utils class --- .../filepicker/FilePickerActivity.java | 30 ------------------- .../com/nononsenseapps/filepicker/Utils.java | 28 +++++++++++++++++ .../sample/NoNonsenseFilePicker.java | 24 ++++++++++----- 3 files changed, 44 insertions(+), 38 deletions(-) diff --git a/library/src/main/java/com/nononsenseapps/filepicker/FilePickerActivity.java b/library/src/main/java/com/nononsenseapps/filepicker/FilePickerActivity.java index 5f0a8549..00293d5f 100644 --- a/library/src/main/java/com/nononsenseapps/filepicker/FilePickerActivity.java +++ b/library/src/main/java/com/nononsenseapps/filepicker/FilePickerActivity.java @@ -7,16 +7,10 @@ package com.nononsenseapps.filepicker; import android.annotation.SuppressLint; -import android.content.ClipData; -import android.content.Intent; -import android.net.Uri; -import android.os.Build; import android.os.Environment; import android.support.annotation.Nullable; import java.io.File; -import java.util.ArrayList; -import java.util.List; @SuppressLint("Registered") public class FilePickerActivity extends AbstractFilePickerActivity { @@ -36,28 +30,4 @@ protected AbstractFilePickerFragment getFragment( mode, allowMultiple, allowCreateDir, allowExistingFile, singleClick); return fragment; } - - - @SuppressWarnings("unused") - public static List getActivityResult(Intent data) { - List result = new ArrayList<>(); - if (data.getBooleanExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false)) { - ClipData clip; - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && (clip = data.getClipData()) != null) { - for (int i = 0; i < clip.getItemCount(); i++) { - result.add(clip.getItemAt(i).getUri()); - } - } else { - List paths = data.getStringArrayListExtra(FilePickerActivity.EXTRA_PATHS); - if (paths != null) { - for (String path : paths) { - result.add(Uri.parse(path)); - } - } - } - } else { - result.add(data.getData()); - } - return 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 2b946b6b..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 { @@ -222,14 +220,24 @@ public boolean onOptionsItemSelected(MenuItem item) { @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { - if ((CODE_SD == requestCode || CODE_DB == requestCode || CODE_FTP == requestCode) && - resultCode == Activity.RESULT_OK) { + // Always check the 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)) { + // 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 : FilePickerActivity.getActivityResult(data)){ - sb.append(uri).append('\n'); + for (Uri uri : files) { + if (sb.length() > 0) { + sb.append("\n"); + } + sb.append(CODE_SD == requestCode ? + Utils.getFileForUri(uri).toString() : + uri.toString()); } binding.text.setText(sb.toString()); } } - } From 90f5e7c0a0f10619360a4517ca180cc402e365be Mon Sep 17 00:00:00 2001 From: Jonas Kalderstam Date: Fri, 7 Apr 2017 23:15:33 +0200 Subject: [PATCH 3/3] Mention utility method in README instead of convoluted code --- README.md | 28 ++++------------------------ 1 file changed, 4 insertions(+), 24 deletions(-) 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... } } ```