Skip to content

Commit a78ca40

Browse files
authored
support only android >=5 (API 21) (#386)
* support android >=5 (API 21) * document requirements
1 parent 02afc3f commit a78ca40

File tree

4 files changed

+20
-28
lines changed

4 files changed

+20
-28
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,16 @@ A React Native wrapper for:
1010
- Android's `Intent.ACTION_GET_CONTENT`
1111
- Windows `Windows.Storage.Pickers`
1212

13+
Requires Android 5.0+ and iOS 10+
14+
1315
### Installation
1416

1517
```bash
1618
npm i --save react-native-document-picker
19+
20+
OR
21+
22+
yarn add react-native-document-picker
1723
```
1824

1925
You need to enable iCloud Documents to access iCloud
@@ -45,7 +51,6 @@ The type or types of documents to allow selection of. May be an array of types a
4551
- 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).
4652
- On iOS these must be Apple "[Uniform Type Identifiers](https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html)"
4753
- If `type` is omitted it will be treated as `*/*` or `public.item`.
48-
- 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.
4954

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

android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ android {
1919
buildToolsVersion safeExtGet("buildToolsVersion", '28.0.3')
2020

2121
defaultConfig {
22-
minSdkVersion safeExtGet('minSdkVersion', 16)
22+
minSdkVersion safeExtGet('minSdkVersion', 21)
2323
targetSdkVersion safeExtGet('targetSdkVersion', 28)
2424
}
2525

android/src/main/java/io/github/elyx0/reactnativedocumentpicker/DocumentPickerModule.java

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
import android.content.Intent;
99
import android.database.Cursor;
1010
import android.net.Uri;
11-
import android.os.Build;
1211
import android.os.Bundle;
1312
import android.provider.DocumentsContract;
1413
import android.provider.OpenableColumns;
15-
import android.util.Log;
1614

1715
import com.facebook.react.bridge.ActivityEventListener;
1816
import com.facebook.react.bridge.Arguments;
@@ -120,27 +118,17 @@ public void pick(ReadableMap args, Promise promise) {
120118
if (!args.isNull(OPTION_TYPE)) {
121119
ReadableArray types = args.getArray(OPTION_TYPE);
122120
if (types != null && types.size() > 1) {
123-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
124-
String[] mimeTypes = readableArrayToStringArray(types);
125-
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
126-
} else {
127-
Log.e(NAME, "Multiple type values not supported below API level 19");
128-
}
121+
String[] mimeTypes = readableArrayToStringArray(types);
122+
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
129123
} else if (types.size() == 1) {
130124
intent.setType(types.getString(0));
131125
}
132126
}
133127

134128
boolean multiple = !args.isNull(OPTION_MULIPLE) && args.getBoolean(OPTION_MULIPLE);
135-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
136-
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, multiple);
137-
}
138-
139-
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
140-
intent = Intent.createChooser(intent, null);
141-
}
129+
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, multiple);
142130

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

164152
try {
165153
List<Uri> uris = new ArrayList<>();
166-
if (uri != null) {
167-
uris.add(uri);
168-
} else if (clipData != null && clipData.getItemCount() > 0) {
154+
// condition order seems to matter: https://github.com/rnmods/react-native-document-picker/issues/317#issuecomment-645222635
155+
if (clipData != null && clipData.getItemCount() > 0) {
169156
final int length = clipData.getItemCount();
170157
for (int i = 0; i < length; ++i) {
171158
ClipData.Item item = clipData.getItemAt(i);
172159
uris.add(item.getUri());
173160
}
161+
} else if (uri != null) {
162+
uris.add(uri);
174163
} else {
175164
sendError(E_INVALID_DATA_RETURNED, "Invalid data returned by intent");
176165
return;
@@ -229,11 +218,9 @@ private WritableMap getMetadata(Uri uri) {
229218
String fileName = cursor.getString(displayNameIndex);
230219
map.putString(FIELD_NAME, fileName);
231220
}
232-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
233-
int mimeIndex = cursor.getColumnIndex(DocumentsContract.Document.COLUMN_MIME_TYPE);
234-
if (!cursor.isNull(mimeIndex)) {
235-
map.putString(FIELD_TYPE, cursor.getString(mimeIndex));
236-
}
221+
int mimeIndex = cursor.getColumnIndex(DocumentsContract.Document.COLUMN_MIME_TYPE);
222+
if (!cursor.isNull(mimeIndex)) {
223+
map.putString(FIELD_TYPE, cursor.getString(mimeIndex));
237224
}
238225
int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
239226
if (!cursor.isNull(sizeIndex)) {
@@ -254,7 +241,7 @@ private void prepareFileUri(Context context, WritableMap map, Uri uri) {
254241
}
255242
String fileName = map.getString(FIELD_NAME);
256243
if (fileName == null) {
257-
fileName = System.currentTimeMillis() + "";
244+
fileName = String.valueOf(System.currentTimeMillis());
258245
}
259246
try {
260247
File destFile = new File(dir, fileName);

react-native-document-picker.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ Pod::Spec.new do |s|
1111
s.authors = package['author']
1212
s.source = { :git => "https://github.com/Elyx0/react-native-document-picker", :tag => "v#{s.version}" }
1313
s.source_files = "ios/RNDocumentPicker/*.{h,m}"
14-
s.platform = :ios, "9.0"
14+
s.platform = :ios, "10.0"
1515
s.dependency 'React-Core'
1616
end

0 commit comments

Comments
 (0)