DownloadManager directly into external storage directory - android

I'm trying to download a big zip file directly inside the external storage directory, for example
"/mnt/sdcard/Android/data/com.vexdev.audioguida.app/files/data"
i guess i should be using
DownloadManager.Request.setDestinationInExternalPublicDir(path, fileName)
but i don't know how to provide it with a path that is consistent across different android devices.
AND i'm also trying to get a path to access this file later, like this:
File file = new File(path + fileName);
i'm asking here because i know that those two methods are expecting different rooted paths, because i tried to provide a path like this:
Application.getAppContext().getExternalFilesDir(Environment.getDataDirectory().getAbsolutePath()).getAbsolutePath();
but the DownloadManager was not putting the files where the File constructor was searching it later. (It was downloading in the wrong directory actually!)
I'm looking for a way to download those files directly in the right directory, without having to move them.

i guess i should be using DownloadManager.Request.setDestinationInExternalPublicDir(path, fileName)
Not for the location you suggested. The closer match would be setDestinationInExternalFilesDir().
but i don't know how to provide it with a path that is consistent across different android devices.
There isn't even a path that will be consistent within one device, as different accounts will use different paths. For example, the path you typed into your question might be used on some devices for some accounts, but the details will vary.
i'm also trying to get a path to access this file later
That is covered in the DownloadManager.Request documentation:
setDestinationInExternalFilesDir() maps to getExternalFilesDir() on Context
setDestinationInExternalPublicDir() maps to getExternalStoragePublicDirectory() on Environment
i tried to provide a path like this
That is not how you use getExternalFilesDir(). Please read the JavaDocs to see what valid values are for the parameter to that method. getDataDirectory() is not a valid value.

Related

Android 11: ACTION_OPEN_DOCUMENT_TREE get absolute Folder Path

Since Android 11 restricts the freedom accesing the external storage, I've looked many solutions to convert following path which I receive from ACTION_OPEN_DOCUMENT_TREE intent: content://com.android.providers.downloads.documents/tree/msd%3A163975/document/msd%3A163975 to a path looking like this: storage/emulated/0/Downloads/TestFolder
Why do I need the absolute folder path?:
The user can choose a folder, where the app will create a backup ZIP file each day.
Since the user might forget which folder he selected, he should be read the absolute path of the folder he choose. content://com.android.providers.... is not really descriptive for the enduser.
According to some valid answers, android 10-11 prevent developers receiving the absolute path of a folder. Is that true?
Other solutions I tried executing without any success:
Get Real Folder Path from Document Tree Uri
Get Full Path From Uri

Path of a file in an internal directory

I have a file gpac.xlsx in the internal storage of my phone. I want to get its path. In android, it's confusing. Which one of these is the correct path?
/data/sdcard0/gpac.xlsx
/data/emulated/0/gpac.xlsx
Or some other besides these?
I have a file gpac.xlsx in the internal storage of my phone.
Based on your sample paths, your file is in what the Android SDK refers to as external storage.
I want to get its path.
new File(Environment.getExternalStorageDirectory(), "gpac.xlsx");
Which one of these is the correct path?
It could be either of those or something else. Use the Java snippet shown above to derive the proper path at runtime for the particular device that your code happens to be running on.

android - how to get path of mp3 stored in raw folder

After searching for solution, I have found from this link that it is possible to retrieve path like:
Using Resource Id
Syntax : android.resource://[package]/[resource_id]
Example : Uri.parse("android.resource://com.my.package/" + R.raw.mp3filename);
Which is exactly what I want. Problem is that, I'm using lolipop, and it does not work. When the package/resource is parsed, path from Uri.parse will be null. Is there other way around to write this, since in my Song class, I have only access to resourceId (R.raw.mp3filename).
Is it possible to get file path by using R.raw.mp3file
No, because it is not a file on the device. It is a file on the hard drive of your development machine. On the device, it is merely an entry in an APK file.
Either:
Do whatever you are trying to do some other way that does not involve a file path, or
Use openInputStream() on a Resources object (you can get one from any Context via getResources()), and use Java I/O to copy the contents of that resource to a local file, such as on internal storage

Is there a recommended path to save files on SD Card?

I see files from apps all over in the sd card so there doesn't seem to be an android standard. But when saving app related files externally, is there a recommended directory path to use or a common practice?
ie. (sdcard)/myapp/ or (sdcard)/apps/myapp/
Simply use Environment#getExternalStoragePublicDirectory(String type) with the various String types for a specific folder.
This creates folders in .../sdcard/Android/data/my.package.name/<type>.
From the Developer's Guides:
In API Level 8 or greater, use getExternalStoragePublicDirectory(), passing it the type of public directory you want, such as DIRECTORY_MUSIC, DIRECTORY_PICTURES, DIRECTORY_RINGTONES, or others. This method will create the appropriate directory if necessary.
If you're using API Level 7 or lower, use getExternalStorageDirectory() ...
You should first off use:
Environment.getExternalStorageDirectory();
To figure out the sdcard location. From there typically it is just your appname as the folder to store any files in. Or so that appears to be the standard looking at my phone.
I would do like this:
Environment.getExternalStorageDirectory() + "Android/data/" + yourpackagename;
if you check this file, you can see lots of apps here.

Using a file located in either the raw or assets folder

Currently I am able to download a file off the internet and store on the SD card, then use the file from there. However that makes the file (with proprietary data) available to be seen. I would prefer to use the file from somewhere like raw or assets folder.
I will skip the downloading code, but my code to use the file is this
File myFile = new File (android.os.Environment.getExternalStorageDirectory() + "/folder/filename.xml");
Intent myIntent = new Intent(Intent.ACTION_VIEW);
myIntent.setData(Uri.fromFile(myFile));
Android opens the file with the default application and all is good.
I have found similar Q/A's that revolve around using code like
Uri.parse("android.resource://com.projectname.testing/raw/filename");
and
InputStream ins = getResources().openRawResource(R.raw.filename);
but I can't work out how to get either of those two back into a 'file' format to be used with my .setData code
I would like to solve my problem by simply accessing the file as a file. However since it is being used by an external application I have read I might need to make a temporary copy of the file with mode_world_readable then delete it after the application closes. This sounds like a lot of extra work, especially since my code above does work for a file stored on the SD card.
Does anyone have any ideas?
Thanks
I would prefer to use the file from somewhere like raw or assets folder.
Note that these too can be "seen".
but I can't work out how to get either of those two back into a 'file' format to be used with my .setData code
setData() does not take a File. It takes a Uri. Use Uri.parse() to parse other types of Uri values -- you already have this code shown above.
However since it is being used by an external application I have read I might need to make a temporary copy of the file with mode_world_readable then delete it after the application closes.
It definitely will need to be world-readable. Also, not all apps support all schemes, so apps that support file:// or http:// might not support android.resource://.

Categories

Resources