From d5a067b7b11b4433605833573eb801cc347aa291 Mon Sep 17 00:00:00 2001 From: F43nd1r Date: Wed, 9 Nov 2016 13:48:36 +0100 Subject: [PATCH] add static helper method --- .../filepicker/FilePickerActivity.java | 30 ++++++++++++++++ .../sample/NoNonsenseFilePicker.java | 35 +++---------------- 2 files changed, 35 insertions(+), 30 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 ea60f37d..9d58dc80 100644 --- a/sample/src/main/java/com/nononsenseapps/filepicker/sample/NoNonsenseFilePicker.java +++ b/sample/src/main/java/com/nononsenseapps/filepicker/sample/NoNonsenseFilePicker.java @@ -10,6 +10,7 @@ import android.app.Activity; import android.content.ClipData; import android.content.Intent; +import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.util.Log; @@ -241,37 +242,11 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { if ((CODE_SD == requestCode || CODE_DB == requestCode || CODE_FTP == requestCode) && resultCode == Activity.RESULT_OK) { - if (data.getBooleanExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, - false)) { - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { - ClipData clip = data.getClipData(); - StringBuilder sb = new StringBuilder(); - - if (clip != null) { - for (int i = 0; i < clip.getItemCount(); i++) { - sb.append(clip.getItemAt(i).getUri().toString()); - sb.append("\n"); - } - } - - textView.setText(sb.toString()); - } else { - ArrayList paths = data.getStringArrayListExtra( - FilePickerActivity.EXTRA_PATHS); - StringBuilder sb = new StringBuilder(); - - if (paths != null) { - for (String path : paths) { - sb.append(path); - sb.append("\n"); - } - } - textView.setText(sb.toString()); - } - } else { - textView.setText(data.getData().toString()); + StringBuilder sb = new StringBuilder(); + for (Uri uri : FilePickerActivity.getActivityResult(data)){ + sb.append(uri).append('\n'); } + textView.setText(sb.toString()); } }