Skip to content

Commit ec99ab8

Browse files
committed
feat(android): add chooser
1 parent 6923bbb commit ec99ab8

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

android.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,25 @@
22
// Use of this source code is governed by a MIT-style license that can be
33
// found in the LICENSE file.
44

5-
import {
6-
NativeModules,
7-
DeviceEventEmitter,
8-
Platform,
9-
NativeAppEventEmitter,
10-
} from 'react-native'
5+
import { NativeModules, Platform } from 'react-native'
116

12-
const RNFetchBlob:RNFetchBlobNative = NativeModules.RNFetchBlob
7+
const RNFetchBlob = NativeModules.RNFetchBlob
138

149
/**
1510
* Send an intent to open the file.
1611
* @param {string} path Path of the file to be open.
1712
* @param {string} mime MIME type string
13+
* @param {string} title for chooser, if not set the chooser won't be displayed (see https://developer.android.com/reference/android/content/Intent.html#createChooser(android.content.Intent,%20java.lang.CharSequence))
1814
* @return {Promise}
1915
*/
20-
function actionViewIntent(path:string, mime:string = 'text/plain') {
16+
function actionViewIntent(path, mime, chooserTitle) {
2117
if(Platform.OS === 'android')
22-
return RNFetchBlob.actionViewIntent(path, mime)
18+
return RNFetchBlob.actionViewIntent(path, mime, chooserTitle)
2319
else
2420
return Promise.reject('RNFetchBlob.android.actionViewIntent only supports Android.')
2521
}
2622

27-
function getContentIntent(mime:string) {
23+
function getContentIntent(mime) {
2824
if(Platform.OS === 'android')
2925
return RNFetchBlob.getContentIntent(mime)
3026
else

android/src/main/java/com/RNFetchBlob/RNFetchBlob.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import okhttp3.OkHttpClient;
2828
import okhttp3.JavaNetCookieJar;
2929

30+
import javax.annotation.Nullable;
3031
import java.io.File;
3132
import java.util.Map;
3233
import java.util.concurrent.LinkedBlockingQueue;
@@ -106,7 +107,7 @@ public void run() {
106107
}
107108

108109
@ReactMethod
109-
public void actionViewIntent(String path, String mime, final Promise promise) {
110+
public void actionViewIntent(String path, String mime, @Nullable String chooserTitle, final Promise promise) {
110111
try {
111112
Uri uriForFile = FileProvider.getUriForFile(getCurrentActivity(),
112113
this.getReactApplicationContext().getPackageName() + ".provider", new File(path));
@@ -115,6 +116,9 @@ public void actionViewIntent(String path, String mime, final Promise promise) {
115116
// Create the intent with data and type
116117
Intent intent = new Intent(Intent.ACTION_VIEW)
117118
.setDataAndType(uriForFile, mime);
119+
if (chooserTitle != null) {
120+
intent = Intent.createChooser(intent, chooserTitle);
121+
}
118122

119123
// Set flag to give temporary permission to external app to use FileProvider
120124
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
@@ -130,6 +134,9 @@ public void actionViewIntent(String path, String mime, final Promise promise) {
130134
} else {
131135
Intent intent = new Intent(Intent.ACTION_VIEW)
132136
.setDataAndType(Uri.parse("file://" + path), mime).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
137+
if (chooserTitle != null) {
138+
intent = Intent.createChooser(intent, chooserTitle);
139+
}
133140

134141
this.getReactApplicationContext().startActivity(intent);
135142
}

index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,9 @@ export interface AndroidApi {
484484
* App to handle the file. For example, open Gallery app to view an image, or install APK.
485485
* @param path Path of the file to be opened.
486486
* @param mime Basically system will open an app according to this MIME type.
487+
* @param chooserTitle title for chooser, if not set the chooser won't be displayed (see [Android docs](https://developer.android.com/reference/android/content/Intent.html#createChooser(android.content.Intent,%20java.lang.CharSequence)))
487488
*/
488-
actionViewIntent(path: string, mime: string): Promise<any>;
489+
actionViewIntent(path: string, mime: string, chooserTitle?: string): Promise<any>;
489490

490491
/**
491492
*

0 commit comments

Comments
 (0)