Associate a directory with my app in internal storage - android

I do not actually know what this is called. But I have seen app icons beside their directories in internal storage. I assume that it's some kind of association. I would like to know how to achieve this for a directory made by my app.
Here is a screenshot showing Telegram and WhatsApp doing what I want:
I am using the following to create a directory:
File(Environment.getExternalStorageDirectory(), "MyApp").mkdirs()
What else should I add to the above code?

What else should I add to the above code?
Nothing. And note that this code does not work on Android 10 (by default) or Android R+ (for all apps), as external storage has been locked down.
But I have seen app icons beside their directories in internal storage.
Note that from an Android SDK standpoint, you are looking at external storage. More importantly, you are looking at external storage through some app.
I would like to know how to achieve this for a directory made by my app.
There is nothing in Android directly for this. You will need to talk to the developers of the app that you used (where you see this icon) and ask what they are doing. It could be as simple as "see if there is an installed app with the same name as the directory" or "see if there is an installed launcher icon with the same name as the directory".
Because I have seen it all file managers I have used
That means that you have more people that you can contact to see what they are doing. Again, there is nothing in the OS for this. After all, if there were, I could write an app to associate my app icon with every directory, and others could do the same. Whatever these file managers are doing, hopefully it is based on information that apps cannot manipulate to somehow spam the association information.

Related

If I know the exact location of a file or folder on an Android device, can I put a file there even if it doesn't belong to my app?

I've been asked to make my android app, at the press of a button, download a file and put it into /enterprise/device/settings/datawedge/autoimport/. This location isn't something that my app creates or belongs to my app -- another app on the system reads files from there to import settings.
Is it possible in general to do this kind of thing? If so, how?
Is it possible in general to do this kind of thing?
No. In general, files exist outside of areas where you could possibly get permission to write. That would include /enterprise/, /TKoL/, or other arbitrarily-named directories off of the system root directory.
On a rooted device, you probably can arrange to write there. And pre-installed system apps might be able to write there.

Android Documents Provider how to use and do I need it?

I'm going to create an app for accessing the files from the Internet. I do not want to implement UI, but instead to make them visible from other file managers. So, I chose to implement a Document Provider. And I did it. On the next pictures you can see that I open "Files" app, find "My document provider" and can access its files.
However, it's an emulator. When I tried to use the app on my physical device I found that I have no ways to see document providers. I use Xiaomi Redmi Note 5 Pro (android 9.0) there is no "Files" app. I tested top ~20 File Managers from Google Play and even MIUI build in file-explorer, but none of them can show me my "My document provider".
I managed to see my documents provider only when tried to attach a document from Gmail app. But it's not what I need.
Questions:
am I right that Documents Provider is not what I need?
Am I right it's rather an ADDITIONAL way to share the files with other client apps which use Storage Access Framework (like Gmail, photo editor, etc), but was not designed to be used as the main method for accessing the files?
Am I right that I have to implement my own Activities with UI and full functionality of file explorer as a main approach for user to access his files? (like Google Drive app - it has both: own UI for managing the files and documents provider accessible from system picker)
UPDATED
Finally I've found a solution. As I understand standard application "Files" is build in to ANY device. However, probably, manufacturer "hides" this app and provides some analog. On MIUI it's some "File Manager". On many Samsung devices, as well as on Pixel 2 XL, it's "Google Files" app. None of them see document providers. Only "Files" app can.
The "Files" app is still accessible as an option when you use SAF (for example start an Intent with ACTION_OPEN_DOCUMENT). But still you are not able to launch that app standalone. However, you can download "Files shortcut" from Google Play. When open it, you can choose an app for which "Files shortcut" should work as a shortcut. I chose "Files" app and everything works (didn't work until I checked "remember my choice" option). On both, Pixel 2 and Redmi Note 5 I can see my document providers. It's a great news, since I do not have to waste time for creating UI!
P.S. Please note, what I described above is not taken from a reliable source. I used the phrase "hidden app" and the fact about pre installed "Files" on ANY device just based on my observation and understanding.
P.P.S Also, I found that on Pixel 2 in "Google files" app there is also a way to open "Files": Browse -> Other storage -> System traces. For some reason there is no "Other storage" button on Redmi Note 5.
I do not want to implement UI, but instead to make them visible from other file managers
You have no means of forcing other apps to do much of anything. In particular, you have no means of forcing a file manager to show things from your app.
Am I right it's rather an ADDITIONAL way to share the files with other client apps which use Storage Access Framework (like Gmail, photo editor, etc), but was not designed to be used as the main method for accessing the files?
Correct. A DocumentsProvider is for making documents available via the Storage Access Framework. A file manager could elect to show documents from the registered DocumentsProvider implementations on the device. In practice, I am not surprised that few file managers do this. You might consider contributing that feature to open source file managers.
Am I right that I have to implement my own Activities with UI and full functionality of file explorer as a main approach for user to access his files?
That is up to you. If you want your app to be guaranteed to do something for all of your users, having your own UI for traversing your collection of documents would be a good idea.

Android 11: How/where to write mixed media files that should survive uninstall

I am writing a specific use-case camera app that targets Android 11. When I hit record I would like to create a new directory somewhere (with a name based on the timestamp etc) that contains the resulting video as well as a whole heap of other custom YAML/JSON/CSV files that also get written during the recording process (belongs logically to the "output" of the recording).
I would like all of the generated files to survive an app uninstall/reinstall as I do not want to risk users losing everything they've ever recorded if they uninstall the app. How do I do this with the new scoped storage changes etc in Android 11?
Looking at the overview here, I can see that:
App-specific files, App preferences and Database are clearly not suitable as amongst other things these files do not survive an uninstall
Documents and other files uses the Storage Access Framework, but this is not suitable because it requires a system file picker every time you want to write something. This would disrupt the flow of recording/user experience, and no camera app works like that.
Datasets/BlobStoreManager (here) also is not appropriate for my use case.
MediaStore API looks like it should be the one, but it can't seem to do what I want in terms of producing a whole directory of outputs, including custom YAML/JSON/CSV text files, that all belong together. My aim is that the user at all times can simply go to the file explorer, navigate to the appropriate folder, and just copy out the folder(s) with the recordings to their computer or whatever, to save/view the data. Even MediaStore.Files does not seem to guarantee you can actually do that if your app is using scoped storage.
The only option that seems to be left is using MANAGE_EXTERNAL_STORAGE and putting the data wherever I want in the home directory, but that seems like a bit of an extreme permission to be asking for just in order to be able to save some text files along with my produced videos. Also, that permission is Android 11 specific. If I want to support older Android versions, what would I need to do?
What is my best choice here? Is there an option I've missed?
but this is not suitable because it requires a system file picker every time you want to write something
No.
Use ACTION_OPEN_DOCUMENT_TREE to let the user pick a document tree. In there, you can create your own sub-tree and put your own documents into that sub-tree. You do not need the "system file picker" for anything beyond the initial ACTION_OPEN_DOCUMENT_TREE request itself. And the resulting documents will survive an uninstall.
You can create your own directory in a public directory like DCIM, Pictures, Music or Movies with classic File methods.

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...

Programmatically get storage directory of an app knowing its package name

I am developing an app which needs access to the directory of an android app (whether internal or external storage) where its files are stored (such as media), starting from its package name.
Foe example:
Say my app wants to acces media files of WhatsApp. I know that WhatsApp package name is "com.whatsapp", and i know that whatsapp is currently installed. How can i find its media folder inside the file system?
I've already searched a lot and I couldn't find a satisfying answer. Many of the answers were about finding the .apk and stuff like that.
I thought about a possible solution was using PackageManager (as suggested in the answers about .apk) but I don't feel this is the right way to get what I want.

Categories

Resources