How can I restore data from instant app to installed app? - android

Keep user state after app installation
https://developer.android.com/topic/instant-apps/ux-best-practices.html#keep_user_state_after_app_installation
I want to use the data stored in instant app, after installation full app.
Does anyone know it?

UPDATE They’ve again updated the Instant Apps FAQ on how to do this.
For devices running Android 8.0 (API level 26) or higher, the instant
app's data is transferred automatically when the APK installation
begins, if the installed app is configured to use targetSandboxVersion
2.
For installed apps not configured to use targetSandboxVersion 2, or
devices running Android 7.1 (API level 25) or lower, please consider
using the Cookie API (Sample) or Storage API (Sample) to transfer the
data.
For Oreo 8.0 and higher, it should be automatically handled.
But for 7.0 and below, you've got 2 options (#1 is recommended):
PackageManagerCompat#setInstantAppCookie(), the cookie has a size limit, but it is the simplest and most suitable for shared preferences.
InstantAppsClient#getInstantAppData(), creates a ZIP of the data that you'll have to extract and parse once it's moved over to the installed-app side, a bit more complicated.
The FAQ lists a sample for each option:
googlesamples/android-instant-apps/cookie-api
googlesamples/android-instant-apps/storage-api
The Storage API/ZIP sample does not show you how to parse the ZIP files and what file type/format to expect from SharedPreferences files, so you'll have to implement that yourself, but here are some related posts:
How to read file from ZIP using InputStream?
How to transfer sharedpreferences file from internal storage to external storage?

Related

Should the Android Files apps be able to see Scoped Storage of other apps?

So I'm trying to confirm if my scoped storage usage on Android is working correctly.
Note - I'm using Xamarin but hopefully can explain this all fine so its just a general Android question.
I'm saving data to the following location....
Android.App.Application.Context.GetExternalFilesDir(Environment.DirectoryDocuments).AbsolutePath
This produces URLs similar to this...
"/storage/emulated/0/Android/data/com.xxx.xxx/files/Documents"
I believe this is correct based on the documentation (but hey, its Android documentation) here...
https://developer.android.com/about/versions/11/privacy/storage
Starting in Android 11, apps cannot create their own app-specific
directory on external storage. To access the directory that the system
provides for your app, call getExternalFilesDirs()
However, the in built files app on Android can browse and see the file all stored there? Is that...expected? I assumed scoped storage was completely hidden from view? I can go to Main Storage -> Android -> Data -> com.xxx.xxx -> Files -> Documents and see all my files in 'scoped storage' there?
Note - The phone I'm testing on is only running Android 10 currently but i'm not using requestLegacyExternalStorage and I'm targetting 30.

Keep file after wipe data in Android 10

The application I'm currently working on requires a manual setup (entering some information) on device provisioning. This information needs to be written to a file that should not be deleted when the application is uninstall or the application data is wiped (user support requirement, as they can direct users to do this in some cases)
There was a very similar old question, but the answer is now deprecated and no up-to-date answer has been posted
Keep files after uninstallation of android app
So the question is, given the deprecation of Environment.getExternalStorageDirectory() on Android 10, how do we programmatically write/read a file that will not be deleted when the application is uninstalled or the data is wiped?
For what is worth, we can not rely on app auto backup, as the users don't have google accounts configured.
Thanks
To summarize while targetting 30.
For Android 10 device: Request legacy external storage to get external storage access as usual.
And Googles step back for Android 11 devices: use directories like Download, Pictures, Movies, Documents, DCIM and so on. Read and write access for all. Android OS is very picky to use the right extensions for files to be created in those folders.

How do I create a file in the "Internal Storage" folder (Primary External Storage) of my Samsung A51 Android 10?

I understand in current SDKs that external storage files for my app are created in the /data/data/"package" folder. However, I am integrating with another app that I have no control over and it reads from the "Internal Storage" (Which is the PRIMARY EXTERNAL STORAGE? AARG! :) folder on my Android 10 (R) phone. I've searched for 2 days now how to create a simple .csv file in the folder labeled "Internal Storage" (My Files->Internal Storage) and simply can't find a solution to getting the path to that location.
I'm setting my Android Studio to minSdkVersion at 16, complileSdkVersion to 28, so at least I can use some of the earlier methods for file management. I expect my app to run on phones/tablets at least 3 years old and newer. There is NO UI necessary (picker-as new doc suggests to use) to select a folder to save the file, just simply create a file there is all I need. I'm an old Java programmer but new to Android. Most posts point to deprecated functions we lose in >28 SDKs.
I understand in current SDKs that external storage files for my app are created in the /data/data/"package" folder.
No, that is internal storage.
However, I am integrating with another app that I have no control over and it reads from the "Internal Storage" (Which is the PRIMARY EXTERNAL STORAGE? AARG! :) folder on my Android 10 (R) phone
That app will need to be updated soon.
I've searched for 2 days now how to create a simple .csv file in the folder labeled "Internal Storage" (My Files->Internal Storage) and simply can't find a solution to getting the path to that location
For Android 10 and below, you can use Environment.getExternalStorageDirectory() for the root of external storage. Note that for Android 10 itself, you will need to add android:requestLegacyExternalStorage="true" in the <application> element.
On Android 11+, you can no longer write to the root of external storage using filesystem APIs, unless you hold MANAGE_EXTERNAL_STORAGE. That permission will require justification for use, if you plan on distributing your app via the Play Store (and perhaps elsewhere).
There is NO UI necessary (picker-as new doc suggests to use) to select a folder to save the file
The user may disagree with you. Google definitely disagrees with you. On Android 11, if you do not want your app to be banned from the Play Store, you will need to use ACTION_CREATE_DOCUMENT or ACTION_OPEN_DOCUMENT_TREE and allow the user to decide where the user's file goes on the user's device.
Most posts point to deprecated functions we lose in >28 SDKs.
"Deprecated" in Android does not always mean "lose". It always means "there is another option that Google wishes for you to consider. So, for example, Environment.getExternalStorageDirectory() still exists, even in Android 11. It is just less useful, and Google would prefer that you use methods on StorageVolume instead.

Cannot find the files in phone storage of real device in android 10

I am using Samsung A30s phone for accessing phone storage files. My files location in device is /storage/emulated/0/MY_FILES/. I kept some files in MY_FIlES directory but my below code does
not show any containing files under this directory.
So how can I get all files belong to this MY_FIlES directory in phone storage?
File Directory = new File("/storage/emulated/0/MY_FIlES/");
File[] files = Directory.listFiles();
But files return null;
Note: I have a permission(READ_EXTERNAL_STORAGE) to access file.
One important thing, I updated my phone in latest API. But before update, I used this path, " /sdcard/MY_FIlES/ and it worked fine.
First, never hardcode paths for apps that you plan to distribute. For those, please use methods on Context, Environment, or StorageVolume (Android 11 only) to find the base directory to use.
For Android 10 and 11, you need to add android:requestLegacyExternalStorage="true" to your <application> element in the manifest. This opts you into the legacy storage model, and your existing external storage code will work.
Note that in Android 11+, you will not be able to write to that directory, regardless of whether you have requested WRITE_EXTERNAL_STORAGE or not. Google would vastly prefer that you use the Storage Access Framework (e.g., ACTION_OPEN_DOCUMENT), so that users have more control over where files get placed on their devices or in their chosen cloud storage providers.
I would like to add something more to the #CommonsWare answer. As per the Android's storage update, they enforce scoped storage in the Android 11
version.
But to give developers additional time for testing, apps that target Android 10(API level 29) can still request the requestLegacyExternalStorage attribute. This flag allows apps to temporarily opt-out of the changes associated with scoped storage, such as granting access to different directories and different types of media files. After you update your app to target Android 11, the system ignores the requestLegacyExternalStorage flag.
If your app opts out of scoped storage when running on Android 10 devices, it's recommended that you continue to set requestLegacyExternalStorage to true in your app's manifest file. That way, your app can continue to behave as expected on devices that run Android 10.
For more info on this, please check documentation

Android Marshmallow damage stored data and block write to internal sd card

I'm developing application that is targeted for API 18. Application download data by Bluetooth and GSM and stores data on internal sdcard (usually \emulated\sdcard - readed by Environment.getExternalStorageDirectory()).
Till this time everything works fine - files and folders are correctly saved.
Today i've faced very strange behaviour:
1) Started the app, and it works for over an hour and store data files.
2) After that, i've closed app and want to download data to the computer.
Before plugin USB, I've used Android file explorer software to check stored data.
I was shocked - new folder (any new data) was gone!
It's like system removes or hide all files and data created in last app session.
Remarks: application don't have procedures for deletion folder or files. Also it works good on previous versions of Android (mostly 4.X and 5.X).
It's very strange because app has warning mechanism when save isn't possible and that warnings wasn't displayed. So I think, write was allowed by system, but data isn't visible at this moment.
Another stranger thing is that application can't write any file in internal storage from this moment (and the warnings are displayed as should in this case).
It looks like Android Marshmallow "decides" in particular moment - your new data won't be available anymore, and your app won't write to internal card.
I've checked app permissions in system - OK.
Any ideas, why this happens? How to deal with it?
You may refer to these guides for managing permissions during runtime: http://www.howtogeek.com/230683/how-to-manage-app-permissions-on-android-6.0/
https://developer.android.com/training/permissions/requesting.html

Categories

Resources