diff --git a/README.md b/README.md index 863bcc32..183a0929 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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"`: diff --git a/android/build.gradle b/android/build.gradle index 2933694a..89a4e28a 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -19,7 +19,7 @@ android { buildToolsVersion safeExtGet("buildToolsVersion", '28.0.3') defaultConfig { - minSdkVersion safeExtGet('minSdkVersion', 16) + minSdkVersion safeExtGet('minSdkVersion', 21) targetSdkVersion safeExtGet('targetSdkVersion', 28) } diff --git a/android/src/main/java/io/github/elyx0/reactnativedocumentpicker/DocumentPickerModule.java b/android/src/main/java/io/github/elyx0/reactnativedocumentpicker/DocumentPickerModule.java index 69719712..7a797b0c 100644 --- a/android/src/main/java/io/github/elyx0/reactnativedocumentpicker/DocumentPickerModule.java +++ b/android/src/main/java/io/github/elyx0/reactnativedocumentpicker/DocumentPickerModule.java @@ -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; @@ -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) { @@ -163,14 +151,15 @@ public void onShowActivityResult(int resultCode, Intent data, Promise promise) { try { List 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; @@ -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)) { @@ -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); diff --git a/react-native-document-picker.podspec b/react-native-document-picker.podspec index 25226e24..a6442f89 100644 --- a/react-native-document-picker.podspec +++ b/react-native-document-picker.podspec @@ -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