QuickBlox file attachments received without extension - android

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);

Related

how load local html on air sdk harman

how load local html on air sdk harman, i use StageWebView on AS3, if i run on its normal view, but if i make apk and instal on android phone, its error. Application cannot showing local html. I use air SDK harman 33.1.1.554.
this my code :
var file:File = File.applicationDirectory.resolvePath("html");
var destinasi:File = File.applicationStorageDirectory;
file.copyTo(destinasi, true);
var hasil:String = "file://" + destinasi.resolvePath("index.html").nativePath;
isiMenu4.loadURL(hasil);
is there issue about security on air sdk by harman? how to skip that?
This might be an issue where you don't have Android permission to write to local storage.
Try creating the directory first and see if that throws any errors.
_file = File.applicationStorageDirectory.resolvePath("html");
trace("html.url", _file.url);
trace("html.nativePath", _file.nativePath);
if (!_file.exists) {
_file.createDirectory();
//make storage directory non backupable
_file.preventBackup = true;
}

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.

PDFTron Android Sample PDF

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.

Downloading a pdf blob to device in Cordova Android App

I am working on a program where I want to save a pdf blob in a android device.
This is server side code inside a callback which receives pdf data as 'result'.
var file = new Blob([result.data], {
type: 'application/pdf;charset=utf-8;'
});
return file;
});
At client side, the following code works in chrome debugging browser and pdf is successfully downloaded. However on device I get the following error :
11-18 18:13:27.255 10504-10707/? D/FileTransfer: download blob:file:///e9088aac-8525-4071-9280-164a6e15c22e to file:///storage/emulated/0/Download/loanContract_11-10-2016_9-29-59-715.pdf
11-18 18:13:27.255 10504-10707/? E/FileTransfer: Unsupported URI: blob:file:///e9088aac-8525-4071-9280-164a6e15c22e
Here the data in window.URL.createObjectURL(data) is the file blob returned by server. Phonegap/Cordova 3.6 - Download of file through blob:file didnt help me in solving the problem.
var fileURL = window.URL.createObjectURL(data);
$scope.content = data;
a.href = fileURL;
a.download = fileName;
a.click();
console.log("Inside parent function");
$scope.fileName = fileName;
window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory, function (dirEntry) {
download($scope.fileURL, dirEntry.toURL()+'Download/'+ $scope.fileName) ;
}, function () {} );
var download = function ( uri, fileURL) {
$cordovaFileTransfer.download(
uri,
fileURL,
function (success) {
console.log(success);
},
function (err) {
console.log(err);
}, true)
};
If i give a url which sends a pdf file as HTTP response and I provide the API's URL as the parameter uri to cordovaFileTransfer.download, then the pdf gets saved.
Kindly help me saving this bob as a pdf on the device.
First, get the file by requesting the file system and then download it from your server with fileTransfer.download(). After completing the download call the FileOpener (which will open a pop up where to choose from apps like Adobe Reader). Note: Make sure to include the FileTransfer feature via CLI/Terminal:
cordova plugin add cordova-plugin-file --save
Navigate to your project folder
Open CLI/Terminal and type in:
cordova plugin add
https://github.com/don/FileOpener.git
After successful download and storage of your file on the device (see
above) you open it with:
window.plugins.fileOpener.open("file:///sdcard/Android/data/com.example.application/document.doc")
or the appropriate path on iOS.

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