Android - Accessing app private folder getFilesDir() path from file manager app - android

I need to access the private folder of my app for debugging purposes.
I use myActivity.getFilesDir()
to handle the private folder but I cannot see it in the filesystem when using the file manager.
Before, I used Environment.getExternalStorageDirectory() and I could see the com.myDomain.app folder in the Android/data directory and I was able to open it and check the private files for debug.
Now, how can I access the getFilesDir()
folder from the file manager?

The directory I used to see being created in /Android/data path is found by means of the getExternalFilesDir(null) method call.
I saw an example with the null parameter, and it works for me.
I replaced all "getFilesDir()" instances in my code files with "getExternalFilesDir(null)", for debugging purposes.
That folder is correctly created and accessible: /Android/data/com.mydomain.app
Every other app is using that folder, even if it is accessible to the user.
I do not know whether I will end up using that folder or the hidden (as far it seems) one.

You are running into the Android security system. The private files of an application can only be accessed by the application itself. You only have a few options:
Add some code to your application that lets you view, dump or copy the private files. I typically make such code dependant on DEBUG builds so that I don't accidentally release it into production.
Move the storage to a public location. I don't typically like this because you have to change code between testing and production, which risks mistakes.
Save files on to an SD card and then access it outside of the device so you are not subject to Android restrictions.
Root the phone so you can bypass the normal restrictions.
If you make some simple code to copy all private files to a public location, then you can write your production code, but with debug builds, you can get it to snapshot the files for debugging.
Hope that helps.

Related

Cleaning up a folder on a phone

first question on this site, if improper just tell me.
I am creating an app on an adroid platform. With this app I create some files and folders in the shared document folder.
What happens is this: with every build, at least with the -cleaninstall parameter set, it is impossible to overwrite existing files and/or folders. Even after deleting them on the phone.
Probably this is due to the fact that the filesystem thinks that the new build is not the original owner of the file/folder and is therefore not authorized to delete or overwrite.
As a bypass solution I am using an "appname" variable to create a folder to store data in, if necessary I update the "appname" variable so a fresh set of folders is created, based on the "appname" but this a pretty crooked way to work.
DocumentFolder := System.IOUtils.TPath.GetSharedDocumentsPath;
AppName := 'ExpensesV2';
AppFolder := DocumentFolder+PathDelim+AppName;
if NOT DirectoryExists(AppFolder) then ForceDirectories(AppFolder);
Is there a proper way to really remove/clean up that specific folder OR get the proper autorisation.
Thank you for your valued responses!!
You don't specify how you're going about creating the folders and writing the files. There are different mechanisms for doing so depending upon the type of file you want to write (media files, documents, generic files) and who you want to have access to it (public vs app private storage). There are also additional complications with permissions and things depending upon what OS version you're targetting.
Given the information available, the best I can suggest is having a look at these links:
https://developer.android.com/training/data-storage
https://developer.android.com/training/data-storage/use-cases
The latter gives some sample cases of types of file and purpose and suggests the mechanism to use to write/read it.
In the most general of senses, if you want to write to public external storage then use getExternalStoragePublicDirectory(); but the warning note here is that the user can do as they wish with the files in the storage area; putting their own files there, renaming files, deleting files etc etc It is beyond the control of your app to manage.
If you want your app to manage the storage space properly then you need to use something like Context#getExternalFilesDir(), but then if you want those files to be externally visible you will have to share them with the system and look into things like file sharing or content providers.

How to create file on android using xamarin?

I've tried several folders but looks like no file is created.
External read & write permission have been enabled.
The code run without any sign of error. (as given below)
Here is the screenshot of the code inside mainactivity.cs (this is for testing purposes).
I also assume that since this is platform specific, the code must be in android project.
string filename = Path.Combine(Xamarin.Essentials.FileSystem.AppDataDirectory, "count1.txt") ;
StreamWriter sw = File.CreateText(filename);
sw.WriteLine("asad ini test");
sw.Close();enter code here
1b. I 'm using Android 6.0 phone, but I can't find such folder /data/user...
(I'm not sure if such folder is targeted special version of Android)
I search both internal /Android/myproject/files/ and found nothing
I search external SDcard /Android/myproject/files/ and still found nothing
(please look at the my android phone folders, i can't even find /data folder
Once, this is accomplished the next step would be how to call this function from the generalproject (non android, non IOS, non UWP project).
Example to write file given in xamarin document.
Where should this code reside? in general project folder ? or, android project folder?
The term "external" used in android is very misleading. It doesn't mean the external removable SD card rather some folders that don't require "root" permission. Therefore, initially I can't find the folder which I save a file into.
Another reason is each phone manufacture uses different name, hence it is very difficult to get an absolute address of an external removable SD Card.
More details is explained in blog_external_removable_SD_Card
You can't write to the removable SD Card
an old post here

Android - How to create folders/files in internal storage

I want to be able to create a folder for my app within "Internal Storage/" (on startup, if required) and later, within this folder, create any files (.txt) the user creates in the app.
These files must remain if the app is uninstalled, so use of getExternalFilesDir() - while it works - is not possible. I should note I don't want the user to have to use the file picker to create the folder or any files - ideally the app will take care of saving it to the app's public folder for them. So far I've tried using MediaStore and Storage Access Framework but have had little success. Just need a push in the right direction.
Note: I'm aware of getExternalStoragePublicDirectory(). It worked for what I need quite well, but I'd prefer not to use deprecated functions if possible. I also have the required permission in my manifest file, just having a hard time creating these files outside of my app's own folder within /data/
Thanks in advance anyone who can help :)
Try this,
private fun getInternalLogFile(filename: String): File? =
File(
context.getDir(INTERNAL_DIRECTORY_NAME, Context.MODE_PRIVATE),
filename
)
INTERNAL_DIRECTORY_NAME is your folder name

KIvy Android how to create and read a file

After looking all over google I haven't found a way of accessing Android internal storage from python.
I need to store an image generated by kivy app. I would like my python code to be able to navigate to a root user dir, create an app specific dir (if not found) and save the image there.
Simply creating a file with my device's path doesn't work. Should I be setting permissions for internal storage access? I'm lost with it.
You have something wrong in your code, you didn't paste the code nor logs, so... let's get it another way.
You can read/write with python just fine in the app folder (/data/data/org.something) with using:
app_folder = os.path.dirname(os.path.abspath(__file__))
To write to SD card you'll need to use App.user_data_dir, which as you can see in the docs on android gets you /sdcard/<app_name>. Not sure if it automatically creates it if it's missing, so it needs checking out probably. However, if it doesn't exist, just create it with python:
os.mkdir(App.user_data_dir)

Save files locally, but prevent them from showing up in the users file manager

Using PhoneGap 3.2 and the File API, I'm downloading a set of images to display in the app. I create a folder named "Appname" and put all the files there. On Android this folder is accessible through the file manager, and on some models the images show up in the users image gallery.
Is it possible to save files locally, but prevent them from showing up to the user outside of the app?
Technically, no. Especially if the client has root access.
You may try the followings to mitigate the problem:
a) Name your files to start with a DOT (.) so that it is recognized as hidden file. (Still, a file manager configured to show hidden files can show it).
b) Store the file instead on some databases in the /data/data/your.app.packages path, which is by default only accessible to your app. (Still a root user can see it).
c) A linux trick. Create a file, open it, hold the file descriptor but remove the file. In this way the file is removed from the directory structure so that it doesn't show up in the FS layer (and thus inaccessible). To make it permanent, use the file descriptor you hold to create a link (or dig into the /proc directory tree to make links with files under fd.
Since this trick works on linux, I guess it should work on Android. But it's probably overkill.
d) Other stopgaps include encryption, obfuscation, etc. But they don't exactly fall into the kind you are looking for.

Categories

Resources