I must get a File object or a Uri object from android assets directory, but I only know how to get inputStream object.
Is there any other way to do it?
You cannot get a File object to an asset, as it is not a file on the device. It is a file in your development machine, but it is merely an entry in an APK file on the device.
Some things, like WebView, can work with a file:///android_asset/... Uri, where ... is replaced by the relative path within assets/ of your project to the file of interest. For example, if your project has assets/index.html, file:///android_asset/index.html would point to that file. AFAIK, not everything can use this sort of Uri, and in particular things that need a URI (e.g., java.net) probably cannot use it.
Related
I am trying to make an app app that will be used to upload a file to the Firebase Storage. I don't want the user to have to choose a file instead i want to package the file with the app. (e.g in the raw/asssets folder).
I have tried several things and I can't make sense of anything. I have no direction. I watched a few tutorials on youtube but all of them use 'choose a file' method.
Any help is greatly appreciated.
You cannot get the absolute the path of the file located in assets. Because those files which are located inside the assets folder does not have the absolute path because they are packaged with the application. But you can use an AssetManager object to get an InputStream on an asset.
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
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.
i'm still don't understand what's wrong with my app. I'm usind DownloadManager for get from a server some mp3 files and i want to put it inside /Podcasts default android folder, but i'm not able to do it.
I've try different way but without success. if i use
Uri destination = Uri.fromFile(new File(Environment.DIRECTORY_PODCASTS));
an exception it's thrown:
java.lang.securityexception destination must be on external storage: file///Podcasts
why?
you may find this thread useful. Basically, it seems that only system apps are able to reach Media Directory. Whhy don't you just use exgternal directory?
String pathLocalFile = getExternalFilesDir( null ).getPath()
would return your app's directory on external memory and you can create relevant directories there.
I need to get the absolute path of the assets folder in my app as I want to serve the files within using a webserver and it needs the absolute path. Is this possible?
I would have thought there might be something like this:
String rootDir = getAssets().getRootDirectory();
but there's not.
Any help appreciated, cheers.
Is this possible?
No. There is no "absolute path of the assets folder in [your] app". Assets are stored in the APK file.
In select cases, such as URLs supplied to a WebView, you can use the special file:///android_asset base URL to reference files in your assets.
You can always copy files from the assets directory in the APK to a folder on the device, then serve that folder.
As mentioned, Android assets cannot be accessed with absolute paths in the device file system. So whenever you have to provide a filesystem path to a method, you're out of luck.
However, in your case there are additional options:
I want to serve the files within using a webserver and it needs the absolute path.
Needing the absolute path is only true if you want to serve the file as a static file with the default mechanism a webserver provides for that. But webservers are much more flexible: you an map any path in an URL to any data source you can access: files, databases, web resources, Android resources, Android assets. How to do that depends on the web server you use.
For example, you can define for youself that any URL starting with https://example.com/assets/ should be mapped to the assets folder of your Android APK. You can then open the asset as an InputStream and serve the content to the webserver's client.
If you have any asset like PDF file stored inside assets folder then get its path using below line:
/assets/file_name.pdf