Skip to content

promise resolve 7 Reject for opening the actionview indent #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -586,14 +586,22 @@ RNFetchBlob.config({
})
.fetch('GET', `http://www.example.com/awesome.apk`)
.then((res) => {
android.actionViewIntent(res.path(), 'application/vnd.android.package-archive')
android.actionViewIntent(res.path(), 'application/vnd.android.package-archive').then(() => {
console.log('File opened')
}).catch(e => {
console.warn('Unable to open file', e);
})
})
```

Or show an image in image viewer

```js
android.actionViewIntent(PATH_OF_IMG, 'image/png')
android.actionViewIntent(PATH_OF_IMG, 'image/png').then(() => {
console.log('File opened')
}).catch(e => {
console.warn('Unable to open file', e);
})
```

## File System
Expand Down
17 changes: 9 additions & 8 deletions android/src/main/java/com/RNFetchBlob/RNFetchBlob.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ public void actionViewIntent(String path, String mime, final Promise promise) {
Uri uriForFile = FileProvider.getUriForFile(getCurrentActivity(),
this.getReactApplicationContext().getPackageName() + ".provider", new File(path));

Intent intent = new Intent(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= 24) {
// Create the intent with data and type
Intent intent = new Intent(Intent.ACTION_VIEW)
.setDataAndType(uriForFile, mime);
intent.setDataAndType(uriForFile, mime);

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

// Validate that the device can open the file
PackageManager pm = getCurrentActivity().getPackageManager();
if (intent.resolveActivity(pm) != null) {
this.getReactApplicationContext().startActivity(intent);
}

} else {
Intent intent = new Intent(Intent.ACTION_VIEW)
.setDataAndType(Uri.parse("file://" + path), mime).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.parse("file://" + path), mime).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}

PackageManager pm = getCurrentActivity().getPackageManager();
if (intent.resolveActivity(pm) != null) {
this.getReactApplicationContext().startActivity(intent);
promise.resolve(true);
} else {
promise.reject("ENOAPP", "No app installed for " + mime);
}
ActionViewVisible = true;

Expand Down
6 changes: 5 additions & 1 deletion android/src/main/java/com/RNFetchBlob/RNFetchBlobReq.java
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,11 @@ private void done(Response resp) {
} catch (ClassCastException ex) {
// unexpected response type
if (responseBody != null) {
callback.invoke("Unexpected FileStorage response file: " + responseBody.string(), null);
try {
callback.invoke("Unexpected FileStorage response file: " + responseBody.string(), null);
} catch (IOException e) {
e.printStackTrace();
}
} else {
callback.invoke("Unexpected FileStorage response with no file.", null);
}
Expand Down