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?