Skip to content

Commit 09ae8f5

Browse files
Promise resolve and reject for actionViewIntent
1 parent 6923bbb commit 09ae8f5

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,14 +586,22 @@ RNFetchBlob.config({
586586
})
587587
.fetch('GET', `http://www.example.com/awesome.apk`)
588588
.then((res) => {
589-
android.actionViewIntent(res.path(), 'application/vnd.android.package-archive')
589+
android.actionViewIntent(res.path(), 'application/vnd.android.package-archive').then(() => {
590+
console.log('File opened')
591+
}).catch(e => {
592+
console.warn('Unable to open file', e);
593+
})
590594
})
591595
```
592596

593597
Or show an image in image viewer
594598

595599
```js
596-
android.actionViewIntent(PATH_OF_IMG, 'image/png')
600+
android.actionViewIntent(PATH_OF_IMG, 'image/png').then(() => {
601+
console.log('File opened')
602+
}).catch(e => {
603+
console.warn('Unable to open file', e);
604+
})
597605
```
598606

599607
## File System

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,10 @@ public void actionViewIntent(String path, String mime, final Promise promise) {
111111
Uri uriForFile = FileProvider.getUriForFile(getCurrentActivity(),
112112
this.getReactApplicationContext().getPackageName() + ".provider", new File(path));
113113

114+
Intent intent = new Intent(Intent.ACTION_VIEW);
114115
if (Build.VERSION.SDK_INT >= 24) {
115116
// Create the intent with data and type
116-
Intent intent = new Intent(Intent.ACTION_VIEW)
117-
.setDataAndType(uriForFile, mime);
117+
intent.setDataAndType(uriForFile, mime);
118118

119119
// Set flag to give temporary permission to external app to use FileProvider
120120
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
@@ -123,15 +123,16 @@ public void actionViewIntent(String path, String mime, final Promise promise) {
123123

124124
// Validate that the device can open the file
125125
PackageManager pm = getCurrentActivity().getPackageManager();
126-
if (intent.resolveActivity(pm) != null) {
127-
this.getReactApplicationContext().startActivity(intent);
128-
}
129-
130126
} else {
131-
Intent intent = new Intent(Intent.ACTION_VIEW)
132-
.setDataAndType(Uri.parse("file://" + path), mime).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
127+
intent.setDataAndType(Uri.parse("file://" + path), mime).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
128+
}
133129

130+
PackageManager pm = getCurrentActivity().getPackageManager();
131+
if (intent.resolveActivity(pm) != null) {
134132
this.getReactApplicationContext().startActivity(intent);
133+
promise.resolve(true);
134+
} else {
135+
promise.reject("ENOAPP", "No app installed for " + mime);
135136
}
136137
ActionViewVisible = true;
137138

0 commit comments

Comments
 (0)