Skip to content

Commit 04c1e73

Browse files
authored
Merge pull request #11 from doomsower/android_chooser
Android add chooser
2 parents e275a84 + ec99ab8 commit 04c1e73

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
@@ -28,6 +28,7 @@
2828
import okhttp3.JavaNetCookieJar;
2929
import okhttp3.OkHttpClient;
3030

31+
import javax.annotation.Nullable;
3132
import java.io.File;
3233
import java.util.Map;
3334
import java.util.concurrent.LinkedBlockingQueue;
@@ -107,7 +108,7 @@ public void run() {
107108
}
108109

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

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

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

index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,9 @@ export interface AndroidApi {
507507
* App to handle the file. For example, open Gallery app to view an image, or install APK.
508508
* @param path Path of the file to be opened.
509509
* @param mime Basically system will open an app according to this MIME type.
510+
* @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)))
510511
*/
511-
actionViewIntent(path: string, mime: string): Promise<any>;
512+
actionViewIntent(path: string, mime: string, chooserTitle?: string): Promise<any>;
512513

513514
/**
514515
*

0 commit comments

Comments
 (0)