I successfully download a pdf file from the internet and then I open the downloads folder using the following Intent:
Intent pdfFileIntent = new Intent();
pdfFileIntent.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
ctx.startActivity(pdfFileIntent);
This code is located within the onReceive() method of a BroadcastReceiver I created to deal with download complete events.
The download is complete and successful (I can see the download status in the downloads folder, which is opened right after the download is complete, as expected). However, when I click the newly downloaded file, another dialog pops up which requests me to select the application with which the file will be opened (I can select either Adobe Reader or Quickoffice). When I select Adobe Reader, I get the error : "The document path is not valid" and when I select Quickoffice, I get the error: "File cannot be opened".
This is terribly annoying especially because the downloads folder and the download process and management of downloaded files is completely out of my hands - Android is aware of the files in the downloads directory when it presents them to me, so how can it be that it doesn't pass a valid path? What is going on?
To make things clear, I didn't specify any path in my code - Android chose the path in which to save the file. And I can even see that the file has a reasonable size in KBs, which means it's not empty and it's definitely a pdf file (.pdf extension).
Please help. Thanks in advance.
And I can even see that the file has a reasonable size in KBs, which means it's not empty and it's definitely a pdf file (.pdf extension).
The fact that a file has the PDF extension doesn't necessarily make it a valid PDF file! Move the file to your PC and see whether you can open it there. You may have a bug in the code performing the download, causing some invalid content to be written to the file.
Related
For example I'm trying to download xyz.pptx from a Facebook group.
Everything works when I do it on my PC,
but on my Xiaomi Redmi Note 8T, in the Downloads it's shown as xyz.pptx, but its file type isn't recognised (Question mark icon), and can't be opened with any app.
Files downloaded via Chrome or even from Facebook Messenger are saved correctly.
I copied the download link on my phone, and opened it on my PC and it correctly downloads it as xyz.pptx.
It seems like it doesn't finish (though I receive that notification the it has finished)
because it's in storage/emulated/0/Download folder, and when I share it via Gmail, the file is called downloadfile-21.bin.
After I rename it to xyz.pptx, it works.
Why does this happen?
I have the same issue. First I thought that it is related to security of facebook groups but it is a different issue. The filename contains an invalid character like ő, ű.
Facebook launches the downloader app which seem to use the MI Browser or something similar to download the file and it fails to catch the filename properly so the download app replaces it with download-xxx.bin. I suspect that the download app has a character encoding issue. It works well with standard ASCII file names.
I couldn't even download it, so I had to do a trick first.
If I copy the link to the MI Browser it will remove the illegal characters from the filename before downloading. This mechanism seems to be broken on the phone so for my case download fails with invalid URL error.
Go to settings, applications, manage applications, MI browser, clear defaults
Now go back to facebook and download the file. Open downloads, select the file, press open, select download here app.
Now you have the downloadfile-28.bin in your directory.
You can open it then with the matching app.
I can only propose a workaround:
Tap on the downloaded file in the notification area which opens the downloaded list (press the check for finished downloads). Tap on the downloaded file. Select copy in the source row. Now open chrome and paste the url. Press enter and save the file. You can find it in the download folder.
In my case, i was getting issues with pdf files. So i used an application named Drive PDF Viewer to open the bin file. It works perfectly. It doesn't open with neither official acrobat pdf nor any Mi viewer.
I want to host an HTML file on server and once user is registered on the app, i want to get this file downloaded and saved into assets folder of the app on the local device and once i have this file into assets folder, i will display it into a WebView and user should be able to see the contents of that file every time user opens it.
And whenever i update the file on server, i want to display a popup for user to accept it, and once user have accepted it, i want to replace the old HTML file from assets folder with the new one.
And from now onwards, the new file will be downloaded on the local system and every time user wants to access the file, he should be able to access the new one.
I'm not sure if that's possible at all, but would certainly be interested in hearing from anyone that knows anything about such things.
Since I cannot write any files to assets or any raw dir, where it will
be written?
openFileOutput() writes to internal storage.
What will the location of that file be? Where will it be located in
the Android file system ?
The apk is read only and the assets/ folder is not modifiable at
runtime, which sounds like something you may be seeking.
Link Source
When the user downloads an attachment/file from Chrome from Gmail, whatever
I need the file to be downloaded to a specific defined directory.
I try to be more clear: I need to create an app that contains the files that are downloaded.
Therefore the app will define a directory.
When the user downloads something from internet, Gmail.. the download should go in there.
I do not know how to implement this kind of behavior.
If you don't want the DownloadManager to put your file outside of your app or on external storage. You may have to handle the download requests yourself and just write the file out into a download directory within your apps data directory. This question has an example of how to get the OutpustStream of the connection and write out the file. Hope this helps.
Android:How to download the File from the server and save it in specific folder in sdcard.
Why does android allow delete a file, although it is being used by another application?
For example: I have a mp3 file on sdcard, I use mp3 player to play it, and then I open a File Manager app and browse to that file; delete it successfully.
If on windows, a message will be displayed and we can't do that.
Why does the android operating system not?
I don't know the principle of file management on android os.
Does anyone have document about it? Help me please!
Going with the information from here (similar query to yours), linux file deletion only removes the pointers to the file until the last process using that file is complete. At this point the contents of the file are freed.
What this means is that the following should not work:
Play an MP3 file from the SDcard.
Open up file manager app and delete said MP3 file.
After MP3 file finishes, attempt to play it again.
let's say I've got a file called foo.html sitting (quite comfortable) in my assets/www directory (next to my index.html).
I'd like to copy that file to another location on the device. My first approach window.resolveLocalFileSystemURI("foo.html", cool(), notCool());is not working. Also with a prefix like www/ it won't.
It would be interesting to know if it is actually possible at all to access files via Phonegap. I'm not convinced and therefore would like to see a code snippet how to obtain a FileEntry for files in the assets directory - if possible.
edit:
Ok now we've got a call like this
window.resolveLocalFileSystemURI("file:///android_asset",
function(entry){
console.log(entry.fullPath);},
function(evt){
console.log(evt.code);}
);
but we've get an error with code: undefined (Phonegap v1.2) and code: 1 with v1.0 (code 1 = file not found?!)
You can't do what you want to do. The files in the assets directory are not technically on the file system so they are not accessible via the File API. This means calling window.resolveLocalFileSystemURI() will not return you a FileEntry.
The best you can hope for is to access those files via XHR. If they are text based you can always take the result of the XHR and write it to the file system using the FileWriter. I wrote a blog post that shows how to get a file from the assets directory using XHR.
http://simonmacdonald.blogspot.com/2011/12/on-fourth-day-of-phonegapping-creating.html
You should be able to access the file this way:
window.resolveLocalFileSystemURI("file:///android_asset/www/foo.html", onResolveSuccess, onFail);
You can then use the File API - FileEntry.copyTo or FileEntry.moveTo to actually do the action. Note - you cannot actually write into the assset/www folder, only to an SD card.
Hope this helps
Leon - "you cannot actually write into the assset/www folder, only to an SD card" The first part is true, you can not write to the asset/www path. But, if the app is installed on the device, you can create and write to a file and it gets created at android's root. If the app is installed on an SD card, that file gets created at the SD card's root. Files thusly created are NOT deleted or altered when clearing user data for the app and are NOT deleted when the app is deleted.