Skip to content
This repository was archived by the owner on Mar 16, 2019. It is now read-only.

Commit bb76484

Browse files
committed
Add android action view intent API for #75
1 parent fe8badd commit bb76484

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

Diff for: src/android.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2016 wkh237@github. All rights reserved.
2+
// Use of this source code is governed by a MIT-style license that can be
3+
// found in the LICENSE file.
4+
// @flow
5+
6+
import {
7+
NativeModules,
8+
DeviceEventEmitter,
9+
Platform,
10+
NativeAppEventEmitter,
11+
} from 'react-native'
12+
13+
const RNFetchBlob:RNFetchBlobNative = NativeModules.RNFetchBlob
14+
15+
/**
16+
* Send an intent to open the file.
17+
* @param {string]} path Path of the file to be open.
18+
* @param {string} mime MIME type string
19+
* @return {Promise}
20+
*/
21+
function actionViewIntent(path:string, mime = 'text/plain':string) {
22+
if(Platform.OS === 'android')
23+
return RNFetchBlob.actionViewIntent(path, mime)
24+
else
25+
return Promise.reject('RNFetchBlob.actionViewIntent only supports Android.')
26+
}
27+
28+
29+
export default {
30+
actionViewIntent
31+
}

Diff for: src/android/src/main/java/com/RNFetchBlob/RNFetchBlob.java

+17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.RNFetchBlob;
22

3+
import android.content.Intent;
4+
import android.net.Uri;
5+
36
import com.facebook.react.bridge.Callback;
47
import com.facebook.react.bridge.Promise;
58
import com.facebook.react.bridge.ReactApplicationContext;
@@ -46,6 +49,20 @@ public void run() {
4649

4750
}
4851

52+
@ReactMethod
53+
public void actionViewIntent(String path, String mime, Promise promise) {
54+
try {
55+
Intent intent= new Intent(Intent.ACTION_VIEW)
56+
.setDataAndType(Uri.parse("file://" + path), mime);
57+
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
58+
59+
this.getReactApplicationContext().startActivity(intent);
60+
promise.resolve(null);
61+
} catch(Exception ex) {
62+
promise.reject(ex.getLocalizedMessage());
63+
}
64+
}
65+
4966
@ReactMethod
5067
public void createFileASCII(final String path, final ReadableArray dataArray, final Callback callback) {
5168
threadPool.execute(new Runnable() {

Diff for: src/android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ public void onReceive(Context context, Intent intent) {
562562
cursor.moveToFirst();
563563
String filePath = cursor.getString(0);
564564
cursor.close();
565-
this.callback.invoke(null, null, filePath);
565+
this.callback.invoke(null, RNFetchBlobConst.RNFB_RESPONSE_PATH, filePath);
566566
}
567567
else
568568
this.callback.invoke(null, null, null);

Diff for: src/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import fs from './fs'
2020
import getUUID from './utils/uuid'
2121
import base64 from 'base-64'
2222
import polyfill from './polyfill'
23+
import android from './android'
2324
const {
2425
RNFetchBlobSession,
2526
readStream,
@@ -36,6 +37,7 @@ const {
3637
cp
3738
} = fs
3839

40+
3941
const Blob = polyfill.Blob
4042
const emitter = DeviceEventEmitter
4143
const RNFetchBlob:RNFetchBlobNative = NativeModules.RNFetchBlob
@@ -383,6 +385,7 @@ class FetchBlobResponse {
383385
export default {
384386
fetch,
385387
base64,
388+
android,
386389
config,
387390
session,
388391
fs,

0 commit comments

Comments
 (0)