InterApp Open Local File

As the title suggests I am trying to open a locally stored file – in this case a pdf. For my use case, it is required that I download the file first since it is a secured file from a web server that requires passing an authentication token in the header.

I am testing with a publicly available pdf.

var InterApp = require('FuseJS/InterApp');
var FileSystem = require("FuseJS/FileSystem");
function openPdfFromUrl(){
	var path = FileSystem.cacheDirectory + "/" + "2016-NYC-Triathlon-Course-Map.pdf";

	var oReq = new XMLHttpRequest();
	oReq.open("GET", "http://www.nyctri.com/wp-content/uploads/2016/06/2016-NYC-Triathlon-Course-Map.pdf", true);
	oReq.responseType = "arraybuffer";
	oReq.onload = function (oEvent){
		var arrayBuffer = oReq.response;
		FileSystem.writeBufferToFile(path, arrayBuffer)
	    .then(function() {
	        console.log("Successful write");
	        openPdf(path);
	    }, function(error) {
	        console.log("Error while writing: " + error);
	    });
	}
	oReq.send(null);
	// InterApp.launchUri("http://www.nyctri.com/wp-content/uploads/2016/06/2016-NYC-Triathlon-Course-Map.pdf");
}

function openPdf(path){
	console.log("file:/" + path);
	InterApp.launchUri("file:/"+path);
}

It successfully writes the file but has trouble trying to open it on Android. I have not tested on iOS. The file is written to the file system correctly and can be opened outside of the app. I have tried with “file://”+path and have gotten a different error. The path as it is now resolves to: “file://storage/emulated/0/Android/data/com.apps.openingfiles/cache/2016-NYC-Triathlon-Course-Map.pdf”. Is this a limitation of InterApp and will I need to figure something out in Uno? Thanks!

Hi there :slight_smile:

While testing your example I found a couple of issues:

The first problem is that a launched app does not have access to the path returned by FileSystem.cacheDirectory. Instead you need to use FileSystem.androidPaths.externalCache, which can be read by the launced app.

Also, instead of "file:/" + path you need to do "file://" + path, to create a valid (absolute) file uri.

Unfortunately it appears that file:/// URIs currently do not work, due to a bug in our URI parser. I’ll create a ticket for this bug, there’s a fix in the works that’ll make it into a future release.

Thank you for the reply and I will be awaiting the fix to the URI parser in a future release.