Detect Folders Created by Apps - android

Is there any way to detect whether a folder was created by an Android app, and not by the user?

No. There's no such information stored in file system (nor in android nor in Linux in general). And even if it would, from filesystem perspective user never creates anything. It is always the app (and it's not important if app does it by itself or directed by user). App just tells the OS "create that folder for me", w/o giving any more detailed information why it wants that to be done and who is the real culprit.
EDIT: I just realised there's a case which maybe was on OP's mind - SD card. Content there can be created "by user" (which means when it got card mounted and accessed i.e. from desktop) and not by android application. But finding that out could be tricky. Some below suggest FileObserver. I am not sure. I'd maybe try to monitor mount/unmount broadcasts, then perhaps scan the sd card to see if there's anything added after unmount. But it is rather ineffective too, as if it is real sd card and user put it in machine with wrong clock, then my scan won't find much there. Other attempt is to know what is there (so build a "map" of sdcard once you see it for the first time and then compare with subsequent mounts). Still, the question remains - why you need that for? Maybe there's simplier approach to your problem

Related

How do I write to the SD card on Nougat/Android 7.0 (VR friendly)?

I'm looking for a way to make my VR Android app (for Samsung Galaxy S7 and S9) able to write files to the SD card (e.g. by downloading a .zip file and unzipping it there).
The app is mostly going to be used by people, who don't know a lot about Android/smartphones and don't want to have to deal with anything complicated (not necessarily seniors but close enough), that's why I want to make it as easy as possible for them, which also includes making choices myself (and setting it up for them) instead of showing complicated dialogs.
Special requirements:
The files must not be deleted when the app is uninstalled - that's why I can's use getExternalFilesDirs() (Storage Volume).
The folder everything happens in has to be easily accessable, so the zip files can be transfered to the SD card on your PC too (instead of downloading them through the app in case they are too big) without having to go down a huge amount of levels and remembering a long folder path.
Using Storage Access Framework isn't a good alternative either because not only is picking folders nothing that's especially VR friendly but it also requires knowledge about folders most of the users simply won't have and/or won't want to deal with every time they open my app. But: If there was a way to only show this once (on the very first start after installing the app) and maybe even set the root folder to the folder I chose, so the users only have to hit "accept", that would be worth a try (unless there's an easier way).
Yes, I did set the android.permission.WRITE_EXTERNAL_STORAGE permission and also enabled the "force allow apps on external storage" developer setting but trying to write to the SD card still throws an "Access Denied" exception.
Are there any others ways to write to the SD card that are VR friendly?

How to get OTG files via Storage Access Framework

So, I want to read a very specific file from any connected USB OTG drive. It's a text file that should be stored on a USB stick and I need to read that file, which has a specific name, say myFile.txt. While I understand this kind of getting user input is dumb and not user-friendly, it's what I'm told to implement.
But my problem is, that there are many mount points for USB Drives. on my phone it's /storage/USBStorage1, while on my friend's it's /storage/[HEX_ID]. I tried to get all of them, but it's not possible. in Nexus phones it mounts on /mnt/media_rw/[HEX_ID] which requires root access.
I searched around a little and found SAF, but I didn't find anything on how to use it to open any file without user's direct selection. I don't want the picker to show up. I just want to check if [USB_OTG_PATH]/myFile.txt exists or not, and if it exists I want to read it.
And on a sidenote, do I need to have root acces to read /mnt folders (except media_rw)?
I didn't find anything on how to use it to open any file without user's direct selection
That is because there is no option for this.
I just want to check if [USB_OTG_PATH]/myFile.txt exists or not, and if it exists I want to read it.
That is not supported.
If your file is located in one of the directories returned by getExternalFilesDirs(), getExternalCacheDirs(), and getExternalMediaDirs(), then you can access it directly using normal Java file I/O. However, AFAIK, that directory needs to be created as part of running your app — another developer ran into problems trying to create the directories ahead of time. So, for your use case, this approach is unlikely to be practical, though with luck I am wrong and it proves useful to you.
do I need to have root acces to read /mnt folders (except media_rw)?
In general, yes, though in practice the answer varies by device, Android OS version, etc.

Can an app detect that a file (image) has been copied off the device?

Is there any way for my Android app to know which image files (from mediastore) that have been copied from the phone and onto a computer (normally, via USB)? I'm working on a gallery app, which shows all photos on the device. It would be neat if it somehow could mark the pictures that have been copied off the device -- that these are backed up.
So, is there such a "broadcast event/intent" that can be listened for? Is there any alternative way to detect that a file has been copied through USB, say via some altered timestamp or something?
There is no event that triggers this. Also you can't see that a file has been copied as it won't change any timestamps etc. as far as I know.
But if you manage the backup mechanism you have access to this information yourself...

Is it possible to track how many users have moved my app to SD card?

I'm interested in the analytics of how many users have moved my application to the SD card. Is there a way to determine this data? Ideally, I'd want to log an event that its been moved to the SD card (and vice-versa), but at this process is out with the application, I'm unsure how to go about getting what I want. For what its worth, I'm currently using Flurry for analytics. Thanks!
Check the path of your application files when the application is started. Edit: I was thinking of checking the running executable path, however that doesn't seem to work.
Have you tried the answer to How to detect when an App is force moved to SD-Card on rooted Android ?

Where can i store data(text and image) in android

i'm writing an application that needs to store some data,and picture. For example place's information. this information don't need to change very often. and
I have seen that databases are
stored under /data/data/package_name/databases
I decided to store my data under /data/data/package_name/files.
With the emulator i can see all these files (databases)
under the proposed directories but moving the application on a real
device and installing a file system browser i cannot see any file
under /data. i know that there are some security constrain in (not-rooted) device. However, are there any suggestion about the solution.. where can i store these data and how? because i'm quite new to android. Thanks so much for your help.
The reason you can't see it on the device is basically just as you said; the device isn't rooted, so other apps don't have access to the /data folder.
This is okay though, because you can still store your files there. Your app has access to anything under /data/data/package_name/, you just won't be able to see it in a file browser unless you root. This is normally a good thing, to keep average users from mucking around with your databases/files.
Read up more on storage methods here.

Categories

Resources