I'm trying to download a file to directory that can vary based on whichever directory the user chooses. I store the current directory in a file object and now attempting to download the file. The file downloads, but it doesn't download the specified directory. So, what can I do to get the file in the directory selected.
// Getting path to store the file
String path = root.getAbsolutePath();
path += curr.getName();
request.setDestinationInExternalPublicDir(path, new File(url).getName());
// get download service and enqueue file
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);
The String named url, that is being passed to the File constructor, contains a URL. Just using that to retrieve that name of the file.
UPDATE:
I just found the file. Its in the phone not the sd card. It was in this folder, storage\emulated\00. Not sure why? Also, the absolute path that I'm getting is storage\emulated\0.
So, I figured out that the path returned is always storage\emulated\0 for the call getExternalStorageDirectory().getPath()) to the Environment. Which is what I using to the get the initial directory. So for instance if I navigated through the folders to the Downloads folder the path would be storage\emulated\0\Download. When I was downloading the file though instead of going to the actual downloads folder it would go to, or create, the downloads folder in the emulated folder in the storage folder. To resolve this I found the index of the zero in the path, added 1, and got the substring using that. After that it worked. Also I had to do this in another method where I navigate through the directory. As a parameter I pass in the file and then within the method I get the substring. Within the method that I use to download the file I get storage\emulated\0 no matter what. Not sure why it does this. If anyone could explain this it would be greatly appreciated.
Does root.getAbsolutePath() return a string with a path separator character at the end?
If not, then you might be doing something like bin/usr/var/appfilename.ext instead of bin/usr/var/app/filename.ext, because you're concatenating it straight into name of the file.
I still had this issue on Android 7. In my case, the file would not show up until I restart the device. This a known issue: https://issuetracker.google.com/issues/36956498
Source: https://stackoverflow.com/a/20413888/2552811
Related
I use some class. That's constructor needs some file path that contains some files.
ex)
Komoran komoran = new Komoran("D:\program_project\lib");
Now I'm making android app. so I can't use absolute path (Because other people who download my app don't have that folder and files)
so I decide to use 'assets' folder that is maybe in APK file. So, final code is like below.
Komoran komoran = new Komoran("file:///android_asset");
but It seems like folder path is wrong(this class doesn't work). What I did wrong ?
This "file:///android_asset" path is used for applications which uses webview ex. cordova/phonegap. As it is part of resources because when Apk is created you can not use this path to access your assets folder. You have to work with context.getResources().getAssets().open("fileName")this code only.
Maybe u can add this code:
context.getResources().getAssets().open("fileName").
u will get a inputstream, and u can do something u want.
No need to use getResources.
You can use directly
context.getAssets().open("fileName").
My app depends on 3 different files. I already put the two larger ones as expansion files which seems to work fine. Now I don't know where to put the third one (small file). I tried the raw folder but actually I don't know what the path of the file is once it is in those folders.
I tried this here for the raw folder
myClassifier.loadModel( pathToExpansionFiles + "/File1",
pathToExpansionFiles + "/File2",
R.raw.File3);
However the return value of R.raw.File3 is an integer, but the function myClassifier expects a string that is the path to File3. Has anyone an idea how to do that?
I tried the raw folder but actually I don't know what the path of the file is once it is in those folders.
There is no path to it. That is a file on your development machine. It is not a file on the device. It is merely an entry inside your APK file.
but the function myClassifier expects a string that is the path to File3
Either switch to some library that works with an InputStream (from getResources().openRawResource()) or make a copy of the data to a local file using that InputStream yourself.
As the title suggests, I am trying to create a folder on Android, but all of the slashes have been removed from it.
For some more background information:
Specifically, I am trying to create a directory to store my application's users' files. These files must be accessible to the user from a file manager (such as File Manager HD) because the application does not support full file management. Using the standard from API level 8+, I reference the root of the publicly accessible folder with Environment.getExternalStoragePublicDirectory(). I then try to create a folder located at DCIM > Sketchbook > [the name of the sketch] using File.mkdirs(). For more information, see the code below.
I have already:
checked to make sure that the SD card is mounted, readable, and writable
enabled the permission WRITE_EXTERNAL_STORAGE
tried using File.mkdir() for every file in the hierarchy up to the folder location
tried using /, \\, File.separatorChar, and File.separator as folder separators
Code:
boolean success = true;
//The public directory
File publicDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
//The location of the sketchbook
File sketchbookLoc = new File(publicDir + "Sketchbook" + File.separator);
//The location of the sketch
//getGlobalState().getSketchName() returns the name of the sketch: "sketch"
File sketchLoc = new File(sketchbookLoc + getGlobalState().getSketchName() + File.separator);
if(!sketchLoc.mkdirs()) success = false;
//Notify the user of whether or not the sketch has been saved properly
if(success)
((TextView) findViewById(R.id.message)).setText(getResources().getText(R.string.sketch_saved));
else
((TextView) findViewById(R.id.message)).setText(getResources().getText(R.string.sketch_save_failure));
With various incarnations of the aforementioned tests (the ones that actually worked), I have received a consistent result: I get a new folder in DCIM whose name corresponds to the combination of all of the folders that should have been hierarchical parents of it. In other words, I have created a new directory, but all of the folder separators have been removed from it.
Now, I ask you:
Am I attempting to save the user data in the correct location? Is there another way that I should be doing this?
Is it even possible to create new folders in the DCIM folder? Does Android prevent it?
Is this problem specific to me? Is anyone else able to create a folder in the DCIM folder?
Am I using the right folder separators?
Is there something else that I am absolutely, completely, and utterly missing?
Now that I am done typing, and you are done reading my (excessively long) question, I hope that I can find some sort of answer. If you need clarification or more information, please say so.
EDIT: An example of the created folder is "DCIMSketchbooksketch", where it should be "DCIM/Sketchbook/sketch".
don't use
File sketchbookLoc = new File(publicDir + "Sketchbook" + File.separator);
but
File sketchbookLoc = new File(publicDir , "Sketchbook");
because publicDir.toString() will not end with a file separator (even if you declared it that way). toString() gives the canonical name of the file.
So your source becomes :
//The location of the sketchbook
File sketchbookLoc = new File(publicDir , "Sketchbook" );
//The location of the sketch
File sketchLoc = new File(sketchbookLoc , getGlobalState().getSketchName() );
I need to get Absolute path to Folder in Assets.
Some like this for sd-card:
final String sdDir = Environment.getExternalStorageDirectory() + "Files";
What i do incorrect?
First I try to get path (in green ractangle) this way but I alwase get "False".
Then I comment this block and try to get path from getAssets().list();
But I get 3 folders witch I see first time.
I want to make massive like this "green" but I need to use files from assets:
Look the image
Help me to get absolute path to my Files folder.
I'm not exactly sure what you're trying to do, so I'll try to cover the bases, and maybe this will help.
If you are just trying to get a list of what's in your assets, then
use getAssets().list("Files"). (You have to use the subdirectory,
because of this).
If you are trying to get an absolute path to your assets directory
(or any subdirectory), you can't. Whatever is in the assets directory
is in the APK. It's not in external storage like on an SD card.
If you are trying to open up files in the assets directory, use
AssetManager.open(filename) to get the InputStream. Here,
filename should be the relative path from the assets directory.
EDIT
I'm not sure what you mean by "massive", but if you want to load the file black.png from assets instead of the SD card, then write this:
// must be called from Activity method, such as onCreate()
AssetManager assetMgr = this.getAssets();
mColors = new Bitmap[] {
BitmapFactory.decodeStream(assetMgr.open("black.png"));
// and the rest
};
Assets are stored in the APK file so there are no absolute path than your application can use. But, I would suggest to take a look at file:///android_asset. It might fits your needs. Here is a good example on how to display an Asset in a WebView.
I have few html files in assets folder of my application. My application loads these files depending on the device language. When I check for the existance of the file it say does not exist, but when I load that file using browser.loadUrl(filename), it loads it fine.
Following code will help you to understand my problem:
String filename="file:///android_asset/actualfilemname.html";
File f = new File(filename);
if(!f.exist){
filename = "file:///android_asset/newfile.html";[Everytime it loads this file even though I have actualfilename.html in the folder]
}
browser.loadUrl(filename);
[it loads the newfile.html but not actualfilename.html]
You can't use File for resources. You'll need to use the AssetManager for that.
(In the off-chance that File does handle resources, which I don't think it does, you'll have to convert the path to a URI first, for example using URI.create(). File(String) expects a path, not a URI.)
Is this the exact code you are using? you probably want to be calling f.exists() not filename.exist().
Edit: try working with the AssetManager instead of hard coding your file path. My best guess is that the file path you are using is not exactly how it supposed to be.