How to get path of folder inside Emulator - android

I added some folder inside Emulator's SD Card from File Explorer. However, I have been having a hard time locating this folder programmatically to access contents inside the directory. From the image below, say I want to get path to "BiometricImages" folder. How can I do so.
This approach I use does not work for me.
val directory = appContext.getExternalFilesDir("BiometricImages")
val images = Files.walk(Paths.get(Environment.getExternalStorageDirectory().path + "/BiometricImages"), 5).filter { it.absolutePathString().endsWith(".jpg") }.toList()
I am using this code to access some contents inside the directory. The results recognizes that the directory is present but for a strange reason, it doesnt recognize the contents inside the directory. Always returns empty. However, it works on real device

Related

Folders not created using mkdirs() in android

I am creating folders using mkdirs() method. Before that, i am also checking if folder is already present or not. if not then only create. It works fine almost all devices but somehow its not creating in some devices.
I also checked runtime permission and storage access framework before creating folders. its all good still it does not create folders. Below is the sample path of creating folders:
Path: /storage/emulated/0/MyAppFolder/TestFolder
Here, /storage/emulated/0/ is internal storage path. After that i am creating two folders using below code:
val folder = File(Path)
if (!folder.exists()) {
if(!folder.mkdirs()){
Log.e("MyActivity","Folder not created")
}
}
I also tried using below code:
val folder = File(Path)
if (!folder.parentFile.exists()) {
if(!folder.parentFile.mkdirs()){
Log.e("MyActivity","Folder not created")
}
}
But still not working.
As per the getFilesDir() documentation, you should never assume a hardcoded installation or directory path - you should only be using relative paths compared to one of the storage directories

How to get path to file in android apk

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.

Can't find file downloaded by download manager in android

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

Slashes removed from calls to File.mkdirs()

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() );

android assetManager

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.

Categories

Resources