Skip to content

Commit

Permalink
support only android >=5 (API 21) (#386)
Browse files Browse the repository at this point in the history
* support android >=5 (API 21)

* document requirements
  • Loading branch information
vonovak authored Feb 6, 2021
1 parent 02afc3f commit a78ca40
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 28 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ A React Native wrapper for:
- Android's `Intent.ACTION_GET_CONTENT`
- Windows `Windows.Storage.Pickers`

Requires Android 5.0+ and iOS 10+

### Installation

```bash
npm i --save react-native-document-picker

OR

yarn add react-native-document-picker
```

You need to enable iCloud Documents to access iCloud
Expand Down Expand Up @@ -45,7 +51,6 @@ The type or types of documents to allow selection of. May be an array of types a
- On Android these are MIME types such as `text/plain` or partial MIME types such as `image/*`. See [common MIME types](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types).
- On iOS these must be Apple "[Uniform Type Identifiers](https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html)"
- If `type` is omitted it will be treated as `*/*` or `public.item`.
- Multiple type strings are not supported on Android before KitKat (API level 19), Jellybean will fall back to `*/*` if you provide an array with more than one value.

##### [iOS only] `mode`:`"import" | "open"`:

Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ android {
buildToolsVersion safeExtGet("buildToolsVersion", '28.0.3')

defaultConfig {
minSdkVersion safeExtGet('minSdkVersion', 16)
minSdkVersion safeExtGet('minSdkVersion', 21)
targetSdkVersion safeExtGet('targetSdkVersion', 28)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.DocumentsContract;
import android.provider.OpenableColumns;
import android.util.Log;

import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.Arguments;
Expand Down Expand Up @@ -120,27 +118,17 @@ public void pick(ReadableMap args, Promise promise) {
if (!args.isNull(OPTION_TYPE)) {
ReadableArray types = args.getArray(OPTION_TYPE);
if (types != null && types.size() > 1) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
String[] mimeTypes = readableArrayToStringArray(types);
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
} else {
Log.e(NAME, "Multiple type values not supported below API level 19");
}
String[] mimeTypes = readableArrayToStringArray(types);
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
} else if (types.size() == 1) {
intent.setType(types.getString(0));
}
}

boolean multiple = !args.isNull(OPTION_MULIPLE) && args.getBoolean(OPTION_MULIPLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, multiple);
}

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
intent = Intent.createChooser(intent, null);
}
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, multiple);

currentActivity.startActivityForResult(intent, READ_REQUEST_CODE, Bundle.EMPTY);
currentActivity.startActivityForResult(Intent.createChooser(intent, null), READ_REQUEST_CODE, Bundle.EMPTY);
} catch (ActivityNotFoundException e) {
sendError(E_UNABLE_TO_OPEN_FILE_TYPE, e.getLocalizedMessage());
} catch (Exception e) {
Expand All @@ -163,14 +151,15 @@ public void onShowActivityResult(int resultCode, Intent data, Promise promise) {

try {
List<Uri> uris = new ArrayList<>();
if (uri != null) {
uris.add(uri);
} else if (clipData != null && clipData.getItemCount() > 0) {
// condition order seems to matter: https://github.com/rnmods/react-native-document-picker/issues/317#issuecomment-645222635
if (clipData != null && clipData.getItemCount() > 0) {
final int length = clipData.getItemCount();
for (int i = 0; i < length; ++i) {
ClipData.Item item = clipData.getItemAt(i);
uris.add(item.getUri());
}
} else if (uri != null) {
uris.add(uri);
} else {
sendError(E_INVALID_DATA_RETURNED, "Invalid data returned by intent");
return;
Expand Down Expand Up @@ -229,11 +218,9 @@ private WritableMap getMetadata(Uri uri) {
String fileName = cursor.getString(displayNameIndex);
map.putString(FIELD_NAME, fileName);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
int mimeIndex = cursor.getColumnIndex(DocumentsContract.Document.COLUMN_MIME_TYPE);
if (!cursor.isNull(mimeIndex)) {
map.putString(FIELD_TYPE, cursor.getString(mimeIndex));
}
int mimeIndex = cursor.getColumnIndex(DocumentsContract.Document.COLUMN_MIME_TYPE);
if (!cursor.isNull(mimeIndex)) {
map.putString(FIELD_TYPE, cursor.getString(mimeIndex));
}
int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
if (!cursor.isNull(sizeIndex)) {
Expand All @@ -254,7 +241,7 @@ private void prepareFileUri(Context context, WritableMap map, Uri uri) {
}
String fileName = map.getString(FIELD_NAME);
if (fileName == null) {
fileName = System.currentTimeMillis() + "";
fileName = String.valueOf(System.currentTimeMillis());
}
try {
File destFile = new File(dir, fileName);
Expand Down
2 changes: 1 addition & 1 deletion react-native-document-picker.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ Pod::Spec.new do |s|
s.authors = package['author']
s.source = { :git => "https://github.com/Elyx0/react-native-document-picker", :tag => "v#{s.version}" }
s.source_files = "ios/RNDocumentPicker/*.{h,m}"
s.platform = :ios, "9.0"
s.platform = :ios, "10.0"
s.dependency 'React-Core'
end

0 comments on commit a78ca40

Please sign in to comment.