0

I am trying to open a remote file using following code:

if (isAndroid) {
    const context = utils.ad.getApplicationContext();
    try {
        if (this.isExternalStorageAvailable() && !this.isExternalStorageReadOnly()) {
            const fsa = new FileSystemAccess();
            const mimeTypeMap = android.webkit.MimeTypeMap.getSingleton();
            const mimeType = mimeTypeMap.getMimeTypeFromExtension(fsa.getFileExtension(filePath).replace(".", "").toLowerCase());
            const intent = new android.content.Intent(android.content.Intent.ACTION_VIEW);
            intent.setFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setDataAndType(android.net.Uri.fromFile(new java.io.File(filePath)), mimeType);

            context.startActivity(android.content.Intent.createChooser(intent, "Open File..."));

            return true;
        } else {
            // TODO: no external storage access logic
        }
    } catch (e) {
        console.log(e);
        //traceWrite("Error in openFile", traceCategories.Error, traceMessageType.error);
    }
    return false;
}

However when I run this code on Android Version 28 and above, I get following exception.

Error: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

When I further checked the exception, here I found that resolution of issue provided at given link. However by performing setFlag/addFlag does not resolve my issue. Another solution provided is to get Activity Context instead of Application context. Can someone guide me on how to resolve this issue?

Pochmurnik
  • 780
  • 6
  • 18
  • 35
Rajan Phatak
  • 524
  • 8
  • 24
  • You would need to call `addFlags()` on the `Intent` returned from `createChooser()`. That is, `Intent chooser = Intent.createChooser(intent, ...);`, `chooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);`, `context.startActivity(chooser);`. Though it would be preferable to just use the `Activity`, if that's where this code is called from. – Mike M. Jan 21 '19 at 07:05
  • Actually, the post you've linked has that solution in [this answer there](https://stackoverflow.com/a/39760006). – Mike M. Jan 21 '19 at 07:14
  • @MikeM. This did not resolve my issue....I am getting exception now on openFile Error: android.os.FileUriExposedException: file:///storage/emulated/0/Download/test.pdf exposed beyond app through ClipData.Item.getUri()... I found some links and also created Provider to provide required access to file in downloads directory... – Rajan Phatak Jan 21 '19 at 10:10
  • "This did not resolve my issue" – Actually, it did. Your question was specifically about setting `FLAG_ACTIVITY_NEW_TASK` on a chooser `Intent`. The `FileUriExposedException` is a completely different issue. I'm not sure what you're saying in that last sentence, but if you still need assistance for that new issue, you should have a look at [this post](https://stackoverflow.com/questions/38200282). – Mike M. Jan 21 '19 at 10:22
  • @MikeM. My question was opening a File on Android 28 and above. Anyways i have found the answer. I am not sure if i am not able to submit it because its marked with Duplicate. I think my answer may help NativeScript community – Rajan Phatak Jan 22 '19 at 04:32
  • No, your question body is specifically about the `AndroidRuntimeException` you got because you were starting an `Activity` from a non-`Activity` `Context`, and how your application of `setFlags()`/`addFlags()` did not resolve that Exception. The fact that you got another Exception after fixing this one does not mean that this issue was not resolved. This site is for specific questions, not overall, interactive app debugging. – Mike M. Jan 22 '19 at 04:40
  • If you have a solution for a general issue that you'd like to share, then you're more than welcome to post a new question and answer for it. I would suggest, though, that your question body describe the overall problem, rather than focusing on a single hurdle encountered in solving it. – Mike M. Jan 22 '19 at 04:44
  • @MikeM. I would do as you suggested. And regarding your comment, while creating question i always thought that it was only Access related issue since it was working on other version of Android. When that problem was solved, i encountered other issues. – Rajan Phatak Jan 22 '19 at 13:44
  • Understood. Yeah, that particular Exception really doesn't have anything to do with the file access. It's strictly a `Context`/`Activity` issue, and it would happen no matter what kind of `Intent` was used, or what information was on that `Intent`. The rules did change between some Android versions, so it's another annoying glitch that can happen when your end goal is something not even really related. Anyway, I do hope that you will post your question and answer, as this does sound like it would be very useful for NativeScript users. I don't see a lot of posts for that here often. Cheers! – Mike M. Jan 22 '19 at 13:52

0 Answers0