File doesn't exist using Download Manager - android

According to this answer it's possible to copy a local file to the download older.
After trying this out:
DownloadManager downloadManager = (DownloadManager)getContext().getSystemService(getActivity().DOWNLOAD_SERVICE);
if(f.exists()){
downloadManager.addCompletedDownload("title", "descirption", true, "application/json", f.getPath(),f.length(),true);
}
I receive the following issue:
java.lang.IllegalArgumentException: File doesn't exist: /data/data/com.example.mytestapp/files/fd92a63b-aad5-4122-a23a-743d164243a1.jpg
Did this feature got cancelled in the newest android versions (30-31) and I have to copy the file in another way or is something wrong with my implementation.

Related

How to open File Manager and pick a directory before downloading file in React Native

I'm developing a React Native app for Android platform and I have to download file by using rn-fetch-blob library.
I find out some apps can open File Manager of Android and choose a directory when user download. So how can I do that?
My app download using writeFile method because it's a base64, not using fetch file from url.
Thanks for reading question and have a nice day!
You can use this package react-native-scoped-storage
import * as ScopedStorage from "react-native-scoped-storage"
let dir = await ScopedStorage.openDocumentTree(true);
Once the user selects a directory, we will recieve the information
about the directory and its uri. Now we can use this uri to
read/write.
// We can store this directory in AsyncStorage for later use.
await AsyncStorage.setItem('userMediaDirectory',JSON.stringify(dir));

Unable to create a directory in the /data/ partition of the Android

I'm trying to create a partition called "subdata" under "/data". But it's failing.
The steps I tried and the failure results are mentioned below.
File dir =new File(/data/subdata/");
boolean success = dir.mkdir();
Here, the "success" value is found "false".
File dir= context.getDir("/data/subdata",Context.MODE_WORLD_READABLE);
Here, I get "java.lang.IllegalArgumentException: File app_/data/subdata/ contains a path separator"
Please, help me in creating this subfolder under /data/ partition.
I solved my need in the below manner.
As I mentioned in the question that, if I create the sub folder manually, I'm able to read and write to that subfolder in the /data.
So, I created this subfolder through init.rc (as I mentioned, I have the build code also). As I'm more fluent in Linux than in Android, I fixed it through init.rc. Now I'm able to read/write to that folder through my android code.

Corona SDK: system.pathForFile returns nil

This code works in the simulator but not on my Android device:
local path = system.pathForFile("chinese_rules.db")
print("PATH:: " .. tostring( path ) )
When I run this code on my Galaxy S4 path returns nil.
My first thought was that it was some typo (case sensitivity) but I can't find any typo:
http://i59.tinypic.com/wlpu14.png
I can't find any reason why it should receive nil. This causes a problem as I can't load my database.
I have also tried this with the same result:
local path = system.pathForFile("chinese_rules.db", system.ResourceDirectory)
I have been able to load a path and load databases like this before.
Corona Build: 2013.2100 (2013.12.7)
Further reading the documentation I don't see that .db is a restricted file type:
Corona allows direct loading of images and audio files using the
appropriate APIs, but it has limited access to resource files on
Android using the file I/O APIs. Specifically, the following types can
not be read from the resources directory: .html, .htm., .3gp, .m4v,
.mp4,.png, .jpg, and .ttf.
http://docs.coronalabs.com/api/library/system/pathForFile.html
I found out the reason for the problem:
We are two that are working on this project and he had setup to use expansion files so two files was created (the main APK and the OBB expansion file) which I didn't notice and I only loaded the main APK file and I guess the database is in the OBB file. After setting not to use an expansion file the app works.
usesExpansionFile = false

DownloadManager not adding the file extension

I would like to download from my server *.apk file, the file type on the server is *.apk and the MIME is application/vnd.android.package-archive",
when downloading with the DownloadManager the file is downloaded but it creating it without the extension "apk", I tried this
request.setMimeType("application/vnd.android.package-archive");
and still no luck the file created without the extension,
when trying to download it with Chrome it worked perfectly...
please help.
I thought request.setMimeType(..) is used it will add the file extension automatically or Android will recognize the file type,but no... you need to use setDestinationInExternalPublicDir(...)
and set the file extension here....

downloading android .apk as .zip in android issue

I have developed an application,but when my client tries to download the application from the url provided using Samsung GT19003,it is downloading as .zip file and on click of that it says could not open the file error.Whereas it downloads as .apk and works good in our devices.Please help me as how can i resolve the issue.
Make sure that you have set the http Content-Type to application/vnd.android.package-archive
This let the chrome know that it is an application not a zip.
A simple solution is to add the below line in /etc/mime.types
application/vnd.android.package-archive apk
And restart your apache.
This will force browsers to download files as "apk" and not append "zip" to it.
if its being downloaded as .zip
You or your client can use File Expert application to unzip this .zip file it must show .apk file in the unzip folder
give a direct apk link to your user either he/she use default browser to download or use some other like opera and if it is .apk file link then it must be downloaded as is.
you may be using some content site which zip file on particular download methods please ensure it for direct .apk link.
EDIT 1:
Last but not the least .apk is just a zip file so this is not an issue just rename it(From .Zip to .apk) if you could and i hope it will solve the problem
In my case also the issue was same as #alireza-fattahi mentioned regarding the header.
Sharing this here because in our case the file was in Azure Storage and accessed by a CDN in case anyone has the same issue.
Fix for Azure Sorage and CDN
By default the APK blob that was uploaded had content type of application/zip and the same content type reflected in the reposnse header causing the downloaded file to get a .zip extension on Android OS when download was finished.
I had to set a custom rule in the storage CDN to overwrite content-type header in the response to application/vnd.android.package-archive
Remember to purge the CDN cache after you set the new rule
Read more about Azure CDN rules engine.
Here is the Example :
// set your download apk path here
$path= public_path(). "/upload/apk/Ninja11.apk";
//Set header here like this
$headers = [
'Content-Type'=>'application/vnd.android.package-archive',
'Content-Disposition'=> 'attachment; filename="ninja-release.apk"',
];
return response()->file($path , $headers);
This is Laravel framework code but in the same way, you can use in other frameworks. Simply you need to set the Content type
Note : This is not Android side issues

Categories

Resources