Android: How to open a file from inside your application - android

I've done my fair share of searching for tutorials and guides online, but the only stuff I have found is from 2007 and out of date.
I wanted to know how to open a file from within my current application. For instance, if I click a "Browse" button, it will start another activity of a default file browser on the device (I don't have to implement my own, right?). Obviously I would need it to return the file so I could parse through it as I pleased.
I figured it would be some for of Intent binded to the the button, but I'm now sure.

Bad news - there is not a native "file selector". Good news - this guy made one so you don't have to.

Use Environment.getExternalStorageDirectory() instead of hard-coding the directory path to the SD card. There also seems to be a problem with getting the exact SD card directory using Android 3.0 and 3.1. I have a test app that reads and writes fine in Android 2.3.3 but the same piece of code doesn't work anymore (see https://stackoverflow.com/questions/6545478/android-3-1-sd-card-emulation).

Related

Permission denied loading html from Downloads

I'm loading HTML from the downloads folder on Android and showing it in a WebView. If the HTML there are some built-in HTML files that are in the assets for the app so I copy them to the Downloads folder and load them from there. These load just fine. If I try to load an HTML file that was copy into the Downloads folder via ADB it says ERR_PERMISSIONS_DENIED in the WebView. When adb shell and look at the file owners and privileges they are all the same so I can only think that Android remembers that my app put some of the files there and loads those but not the others?
I have android:requestLegacyExternalStorage="true" in my manifest and I'm requesting Manifest.permission.READ_EXTERNAL_STORAGE and Manifest.permission.WRITE_EXTERNAL_STORAGE when the app loads. What else do I need to do to make this work?
For context:
I'm working on an app that connects to a custom BLE device and allows users to control various demo applications. We want the demos to be written by UX folks on our team using HTML and JavaScript so the app. To support this, the app has a WebView that just loads the index html. The UX folks need to be able to make changes and try new things. The easiest way for them to get it on the phone is to put them in the Downloads folder. I the app creates a folder in Downloads and copies any built-in demos from assets to that folder and then loads them from there. That way it can auto-detect any new demos the UX team adds.
Android remembers that my app put some of the files there and loads those but not the others
On Android 11+ (and perhaps 10), this is absolutely the case.
What else do I need to do to make this work?
The simplest solution is to stop using Downloads/. Instead, use the directory available as getExternalFilesDir(null) on Context. You have full read-write access there, and your designers can drag-and-drop files to that directory (Android/data/your.application.id/files/) if they're using a USB cable to transfer files from a desktop or notebook.

Android - how prompt user to choose an external program for rendering a file

I am writing a program for Android which has PDF / ODT file with instructions (yes, those are complex). Actually, I am planning how to do it... So, let's say, a user taps on menu entry "View Instructions" and he should get a prompt which asks to choose an external app for viewing the file (I assume that viewing PDF/ODT file inside my program is extremely complex task, so I find it acceptable to delegate it to another app), - and chosen program pops up with opened file for viewing. How do I implement such behavior?
I have very limited experience at programming Android, I have written a program which interfaced with camera. From that time I remember that I called something like Activity... or Intent... I don't remember. Anyway, it opened Android-specific Camera-related system application. It is almost what I'd like to achieve.
So, how do I open a file in some 3rd party program from my code, giving user a choice?

Associate a directory with my app in internal storage

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.

Xamarin.Forms - Best way to open dialog to search file

I created an little application in Xamarin.Forms to get the images in my file with XLabs. It work with android and IOS.
But now, i want to import file and i search the best plugin to do that.
I found this : https://developer.xamarin.com/recipes/android/data/files/browse_files/
But it dosent exist with IOS. And i don't know if it's possible (to search and get file)
And it's why i come here, to get answers.
Can you give me a plugin or a solution to get file/path of any file with OpenDialog, intent, or page custom
Thank you
Are you wanting something that can search files outside of your app's directory on iOS or only files within the app's directory?
If you want the former, iOS severely restricts this kind of thing, unlike Android. So it is not possible to do the same thing on iOS that you can do on Android.
Look at the second paragraph here and see that the app is sandboxed which means it cannot view files outside of it's own directories.
That being said other apps can make files available to be shared with other applications, see here.
You can also get access to other files from the device's iCloud account. See this for pre-iOS 8 and this for post iOS 8.

database file does not appear under data/data/packagename/databases or anywhere else

First I have been searching for a solution for acouple of days now. It is very strange as the answer to my problem maybe very simple.
I am trying to make the simplest code to create sqlite database. there is plenty of examples in the web. the code does not generate any error and the application appears on the device. but when I try to locate the database in the specified location as in the title it does not seem to be created.
my specific questions
1-does the device has to be rooted to create a database file.
2-do I need special manualy entered permissions in the manifest file to create the database file.
3-Is there any prior settings that I have to do in android studio or a plug-in that I have to add.
4-Do I have to create the sub folders manualy because some devices dont even have the second data subdirectory of the main data directory in the main internal storage. Any way I created the folders and subfolders but no database appeared after running the application. I use a file explorer in the target device to inspect the existance of the database file.
many thanks for any attempts to help me as I seem to be stuck for few days now.
I found the files in the internal device memory using a root explorer.
The strange thing I have not seen any tutorial which alerts me to the hidden memory and root explorer. time wasted but along the way I learned many new things.
thanks all

Categories

Resources