PDFTron Android Sample PDF - android

Using the Android sample in the WebViewer folder I have an application in Android Studio which when run on my device works and displays the xod file given as expected. However I've tried to change the lines:
String documentPath = getFilesDir().getPath() + "/GettingStarted.xod";
InputStream in = getAssets().open("xod/GettingStarted.xod");
to
String documentPath = getFilesDir().getPath() + "/sample.pdf";
InputStream in = getAssets().open("xod/sample.pdf");
And have also changed the endsWith(".xod") to endsWith(".pdf") but I only get a grey screen. For this to work does the file have to be a .xod? As it works for xod files but not pdf files.
Thanks for your time.

Yes, for mobile viewing you need to convert your files to the web-optimized XOD format.
The PDF backend for WebViewer is not available for mobile browsers. This is due to limitations of the hardware and the mobile browsers.
For desktop browsers, to switch from XOD backend to PDF backend, you need to follow the steps. See here for more details, especially if you run into any errors.
var myWebViewer = new PDFTron.WebViewer({
path: "lib",
type: "html5",
documentType: "pdf",
initialDoc: "GettingStarted.pdf"
}, viewerElement);
Notice the documentType parameter is set to pdf.

Related

QuickBlox file attachments received without extension

I am developing a NativeScript Android app and using QuickBlox.
I can open images and audio files inside the app, but there are some issues with .pdf, Word and Excel documents.
Code written by a co-worker:
var attID = message.attachments[0].id
var fileSrc = ChatManager.getQB().content.publicUrl(attID) + "/" + "/download.xml?token=" + ChatManager.getSessionToken()
I get the URL with blob + session token, then:
var intent = new android.content.Intent(android.content.Intent.ACTION_VIEW, android.net.Uri.parse(args.object.src));
intent.addCategory(android.content.Intent.CATEGORY_BROWSABLE);
application.android.startActivity.startActivity(intent);
In this way I successfully download .pdf files, but .doc, .docx, .xls and .xlsx return without extension.
I also tried getting the URL through privateUrl() (without interpolating the token), with the same result.
Another not working method:
httpModule.request({
url: uid,
method: 'GET',
headers: {
'QuickBlox-REST-API-Version': '0.1.0',
'QB-Token': ChatManager.getSessionToken()
}
}).then(res => {
var file = res.content.toFile();
var intent = new android.content.Intent(android.content.Intent.ACTION_VIEW);
var uri = android.net.Uri.fromFile(new java.io.File(file.path));
intent.setDataAndType(uri, 'application/pdf');
application.android.startActivity.startActivity(android.content.Intent.createChooser(intent, 'Apri file...'));
});
Any way I try, it feels like QuickBlox returns the extension on .pdf files only. Any suggestions?
UPDATE:
I tried opening the URL on different devices.
https://api.quickblox.com/blobs/[blobId]?token=[token]
On Chrome for Windows and iOS Safari, the file is downloaded or opened in browser correctly. On Android, it returns without extension on these browsers: Chrome e LineageOS stock browser. On Internet Samsung 7.4.00.70, it's opened in the browser correctly.
With httpModule.request(), I get this warning in the debug console on result:
Resource interpreted as Document but transferred with MIME type [my file mime type]
At the moment I am using this workaround in native code, but later I'll need to find something that works on iOS too.
I'm not marking this as an answer since it doesn't solve the problem
var r = new android.app.DownloadManager.Request(android.net.Uri.parse(args.object.src));
r.setDestinationInExternalPublicDir(android.os.Environment.DIRECTORY_DOWNLOADS, fileName.text);
r.allowScanningByMediaScanner();
r.setNotificationVisibility(android.app.DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
var dm = application.android.startActivity.getSystemService(android.content.Context.DOWNLOAD_SERVICE);
dm.enqueue(r);

Trouble loading local html file into default device browser in Android app

I am an iOS developer assigned a task in Android, so bear with me, I'm a bit green in Android.
I am attempting to load a local html file that is stored in the device download directory in a folder called user_guide. I want the html file to load in the device's browser (not in a webview for reasons outside the scope of this post). I am using the following code to launch the browser:
String downloadPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
String path = "file://" + downloadPath + "/user_guide/index.html"; // + R.raw.test;
Uri pathUrl = Uri.parse(path);
Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
browserIntent.setData(pathUrl);
context.startActivity(browserIntent);
I obtained the value of path by setting a breakpoint and manually set it in Chrome on my device to verify that is does work and loads the proper file. However, when I try to run this code in the app, I get the following toast message:
Cannot display PDF (index.html is of invalid format)
I'm confused about this message since I am trying to load an html file, not a PDF. Can anyone help me out? THanks!
Try changing "browserIntent.setData(pathUrl)" to
browserIntent.setDataAndType(pathUrl, "text/html")
to explicitly specify that it's HTML.
I found this suggestion at https://stackoverflow.com/a/7009685/10300291.

How can I use an Html file in a raw Android folder to populate a Xamarin Forms webview?

I have an html file saved on an Android device here:
/data/data/MyApp.Mobile.Forms/files/4650-0/11456.pptx.html
I'm able to use FileReaders to look at the Html. However when I set it as my webViews HTML source, like so:
var path = Directory.GetFiles(directoryPath, "*.pptx.html")[0];
string htmlString = File.ReadAllText(path);
var htmlWebViewSource = new HtmlWebViewSource();
htmlWebViewSource.Html = #htmlString;
htmlWebViewSource.BaseUrl = DependencyService.Get<IBaseUrl>().Get();
webView.Source = htmlWebViewSource;
I get an activity indicator but no webview loads. I've tested other sites, e.g. Google, and raw html, and these work. But for some reason, my html does not.
I've also seen other approaches using stream readers, but none of these have worked for me.
I see a lot of talk of my html file needs to be in the assets folder. But the file is saved at runtime and files can't be saved to assets at runtime.
Please help me work out what I'm missing here. Can I have a webview using a file from that data/data path?

Codename one can't get correct storage path on Android

We are trying to included some bundled images with our app which integrates with URLImages but unzipping them into the storage folder (with url paths that match those expected).
It works fine on ios and in the simulator, but is failing to find or have access to the correct path on Android.
String storagePath = getStoragePath(url + suffix);
OutputStream os = Storage.getInstance().createOutputStream(storagePath);
...
getStoragePath(String string){
for (String s : Arrays.asList("/", "\\", "%", "?", "*", ":", "=")) {
string = StringUtil.replaceAll(string, s, "_");
}
return string;
}
On android the path seems to be wrong from this.
Is there any way to manually get the correct path and then use FileSystemStorage to open the output stream?
I'd rather not hard code any paths since they do occasionally change, and can vary from system to system.
Any other options I'm not seeing here?
You are coding to an implementation detail which is specifically hidden and might change in the future. FileSystemStorage & Storage are logically separate and shouldn't be mixed.
If you want access from FileSystemStorage you should always only use that API and avoid Storage.
This is by design, in some OS's storage is implemented in a very unique way that doesn't map to a filesystem file at all...

How to create a blob in Android HTML5 app

My server spits out the source of a PDF. With XHR2 I request that code using xhr.responseType = 'blob'; For iOS I then just use
blob = new Blob([this.response], {type: "application/pdf"});
and then save it using Phonegap's Filewriter. When I then open the saved file, I have a perfect PDF. I simply can't get anything similar to work on Android. My last attempt was:
if (!window.BlobBuilder && window.WebKitBlobBuilder){
window.BlobBuilder = window.WebKitBlobBuilder;
var bb = new window.BlobBuilder();
bb.append(this.response);
blob = bb.getBlob("application/pdf");}
since Android coughed over just plain 'BlobBuilder'. That duly creates a PDF which FileWriter then saves. The files structure is presumably OK as a PDF since PDF readers open it without complaint. Unfortunately, the PDF is completely blank.
I just don't seem to be able to get my elderly head around whatever is required by Android.

Categories

Resources