Skip to content
This repository has been archived by the owner on Feb 11, 2022. It is now read-only.

added a static helper method for parsing activity results #138

Merged
merged 3 commits into from
Apr 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 4 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> 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<Uri> files = Utils.getSelectedFilesFromResult(data);
// Do something with the result...
}
}
```
Expand Down
28 changes: 28 additions & 0 deletions library/src/main/java/com/nononsenseapps/filepicker/Utils.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package com.nononsenseapps.filepicker;

import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;

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
Expand Down Expand Up @@ -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<Uri>} of files (uris) which the user selected in the picker.
*/
@NonNull
public static List<Uri> getSelectedFilesFromResult(@NonNull Intent data) {
List<Uri> result = new ArrayList<>();
if (data.getBooleanExtra(EXTRA_ALLOW_MULTIPLE, false)) {
List<String> paths = data.getStringArrayListExtra(EXTRA_PATHS);
if (paths != null) {
for (String path : paths) {
result.add(Uri.parse(path));
}
}
} else {
result.add(data.getData());
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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<String> 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<Uri> 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());
}
}

}