How to take access to internal storage in android 11? - java

I want to access full access of internal storage but I have tried so many things didn't able to access in android 11. then I used solid explorer also available in the play store which is giving full access to internal storage. can someone tell me how to achieve this in android 11.

You have two options, either acquire MANAGE_EXTERNAL_STORAGE which needs some approval from google play AFAIK!
Or set the targetSdk to 29 and use requestLegacyExternalStorage
https://developer.android.com/about/versions/11/privacy/storage
Apps that run on Android 11 but 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.
But remember, you can use targetSdk 29 until August 2021 (November if it's an update) and after that you have to targetSdk 30
https://developer.android.com/distribute/best-practices/develop/target-sdk
In other words, Google is trying to prevent apps from accessing external storage for privacy concerns. I guess we should follow Google's guidelines (forget about the user's files)

Related

Unable to share media file outside app with our app in Redmi device for android 11

Whenever we share a media file with our app, that file we should have access to display, but in the Xioami device we are unable to access those files, whereas for other device like vivo, oppo, realme, oneplus etc. is working fine and able to display or read those file.
For android 10 version it's working fine in redmi device but for android 11 (SDK 30) it's not working.
Even popular app like Whatsapp, facebook, Instagram, Telegram and Singal also unable to attach file from out side the application.
Can anyone please give your feedback and help how we can fix this problem in future.
Below is my code to get media uri
String action = getIntent().getAction();
String type = getIntent().getType();
if (Intent.ACTION_SEND.equals(action) {
if (type.equals("text/plain")) {
String textMessage = getIntent().getStringExtra(Intent.EXTRA_TEXT);
} else if(type.startsWith("image/")) {
Uri imageUri = (Uri) getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
String path = imageUri.getPath();
} else if(type.startsWith("video/")) {
Uri videoUri = (Uri) getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
String path = videoUri.getPath();
}
}
We also have used custom parser to get file path from uri but didn't work.
File provider also included in xml directory.
If this is only happening with targeting API 30 and was working fine before with API 29, then it may be due to one of the changes introduced with API 30, as listed by Google. In particular, from your description, it may be because of APP_DATA_DIRECTORY_ISOLATION.
As https://developer.android.com/about/versions/11/privacy/storage#other-private-dirs explains:
Android 9 (API level 28) started to restrict which apps could make the files in their data directories on internal storage world-accessible to other apps. Apps that target Android 9 or higher cannot make the files in their data directories world-accessible.
Android 11 expands upon this restriction. If your app targets Android 11, it cannot access the files in any other app's data directory, even if the other app targets Android 8.1 (API level 27) or lower and has made the files in its data directory world-readable.
and
On Android 11, apps can no longer access files in any other app's dedicated, app-specific directory within external storage.
How can you test if it is APP_DATA_DIRECTORY_ISOLATION or some other API level 30 feature that is related to the issue (at least on the Xiaomi device)? You can actually built a debug app targeting API level 29 and toggle on/off individual API level 30 features to check, as explained in My Android app is not working properly once I set targetSDK as API 30; how do I figure out the reason(s)?
Why only on Xiaomi and not other devices? Maybe related to differences in implementation, but if you can at least test the API level 30 feature changes on the Xiaomi device, that can help in the troubleshooting.

How to access and rename files using scoped storage within an app targeting Android 11

I am looking for a way to rename media files in the internal storage of the device from my app. I am told that there are some major changes with permissions in apps that target SDK 30 and there scoped storage is now compulsory. I previously used the MANAGE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE and from what I've read using MANAGE_EXTERNAL_STORAGE makes the app ineligible for the
Playstore.
So what permission can I use now which can allow me to access and rename media files while staying in bounds of the new privacy changes needed to make my app acceptable for the Playstore while targeting SDK 30.
Also is using the File class and renameTo method the best way to rename a file?

requestLegacyExternalStorage is not working in Android 11 - API 30

Google has introduced some changes recently related to storage APIs in API 29 like scoped storage and we opted out by adding 'requestLegacyExternalStorage=true' in Manifest. But now when I targetSdkVersion 30, this no longer seems to work. Some of the files in the download directories were not listing (File.listFiles) after this change.
But now when I targetSdkVersion 30, this no longer seems to work
That is correct. Android 11 (API 30+) requestLegacyExternalStorage=true does nothing and you can no longer "opt-out". It was available in Android 10 to give developers a transition/grace period to be able to migrate to the scoped storage model.
Option 1: Migrate data in your app whilst still targeting API 29, then once you're migrated data is compatible with scoped storage you should be able to release an update targetting API 30 - https://developer.android.com/training/data-storage/use-cases
This can come with its own problems if users skip this version and updates directly from a previous version to the latest and you're stuck with un-migrated data you can't access.
Option 2: It seems that Google sees this obvious caveat and has included a preserveLegacyExternalStorage=true option when targetting API 30 to allow you to migrate data. https://developer.android.com/reference/android/R.attr#preserveLegacyExternalStorage
Going forward you can reference this table for deciding what storage "framework" to use based on the use-case: https://developer.android.com/training/data-storage
There is a potential that some apps simply won't be able to successfully migrate, based on how they interacted with the File API as Google's solution will not encompass all current use-cases and there might not be a migration path.
For instance, I released an app a couple of years ago that allowed users to update album artwork using MediaStore and ContentResolver to update the data for the album artwork image - this was stored in shared storage. Having looked at the Android 10+ AOSP MediaProvider source code it seems that apps that used to use MediaStore to update album artwork to point to a data file no longer works, simply because the MediaProvider internally creates its own artwork in a hidden .thumbnails folder looking directly at the mp3's and using a MediaExtractor, and never references the ContentValues that were inserted to reference the artwork. So even though you can update the artwork yourself, query the MediaStore for it and see it, other apps have to use ContentResolver#loadThumbnail in API 29+ that does not reference your updated values and either creates an artwork lazily, or picks the already generated file in the .thumbnails folder. Obviously, none of this is documented, and I got a massive backlash to my app with negative reviews, yet these changes were breaking changes and completely out of my control and took me looking through AOSP source code to find that Android had fundamentally changed behaviour.
(This wasn't a rant, but an example of how these changes offered no migration path because of fundamental undocumented behaviour to AOSP).
As stated in https://developer.android.com/about/versions/11/privacy/storage there are some changes regarding storage on Android 11:
Android 10 devices
requestLegacyExternalStorage will continue to work regardless of target sdk
Android 11 devices
new installation targetting sdk 29: requestLegacyExternalStorage value is respected
new installation targetting sdk 30: requestLegacyExternalStorage is always false
upgrade from targetting sdk 29 to 30: if preserveLegacyExternalStorage is set then requestLegacyExternalStorage is true (this is pure migration case and this state won't be preserved should user uninstall/reinstall the app)
You're pretty much forced to implement scoped storage at this point. Unless you're ready to migrate just keep targetting sdk 29 as there's no way to enforce legacy storage on Android 11 devices with target sdk 30.
update: play store requires target sdk 30 as of August 2021
Don't do this until early 2021 (said by google):-
If you want to target to android 11, you should use the MANAGE_EXTERNAL_STORAGE permission.
Visit this page for more details: https://developer.android.com/training/data-storage/manage-all-files
Applications that run on Android 11 but target Android 10 (API level 29) can still request the requestLegacyExternalStorage attribute. This flag allows applications 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 updating application to target Android 11, the system ignores the requestLegacyExternalStorage flag.
No need to call "requestLegacyExternalStorage=true" which is not working for Android 11+.
There is a new update in https://github.com/apache/cordova-plugin-media to cover the saving file path issue for android 11+.
If you update "/platforms/android/app/src/main/java/org/apache/cordova/media/AudioHandler.java" and "/platforms/android/app/src/main/java/org/apache/cordova/media/AudioPlayer.java" in your project, then it should be working.
https://raw.githubusercontent.com/apache/cordova-plugin-media/4093f7e14fe65f94ffbef072ed188a0205e78a59/src/android/AudioHandler.java
https://raw.githubusercontent.com/apache/cordova-plugin-media/4093f7e14fe65f94ffbef072ed188a0205e78a59/src/android/AudioPlayer.java

How do I create API level specific UI (a different UI for tablet and older versions of Android for the same app)?

I want my app display to be consistent across all android devices.
What code do I need to determine API level of user's device, and display appropriate UI Layout?
Example (pseudocode):
if Android 6 (API level 23) {
Display UI-A
}
if Android 10 (API level 29) {
Display UI-B
}
I have perused documentation and other SO answers and there is no clear way to do this. I want my simple app to have a clean UI accross multiple API levels. This probably involves changing
res->layout files and AndroidManifest.xml, but I am unclear on how to do this properly. I want the UI to change based on device API level, with target level set as default.
You need to create different resources directories. So let us say that you need a layout for API 23 and a layout for API 28, you need to create two directories inside your resources folder, one with the name layout-v23 and the other with the name layout-v28 and you need to place your layouts in the respecting directories.
You can read more about this here
https://developer.android.com/guide/topics/resources/providing-resources#AlternativeResources

RecyclerView not show the data when I run on android 6.0 (Real Device or Emulator)

Related on my prev issue : RecyclerView can not display the data retrieved from the server
I want to ask if there are differences in using android 6.0 with RecyclerView? Once I found my subject matter is because I use the emulator is intended for API 23. However, when I use the emulator is set using API 19, then my problems disappear and the data is displayed properly in RecyclerView.
For detail my configuration
compileSdkVersion 24
buildToolsVersion 23.0.3
minSdkVersion 19
targetSdkVersion 24
I only use the internet permissions in my applications. And to my knowledge the internet permissions have been given full access and no need to do this manually access exemption. Maybe I'm wrong, if yes, please help this problem so that my application could run well on Android 6.0 or higher
Thanks
For API 23, check if the permissions are given under APP Permissions in the app settings.Each permission has to be given explicitly in API23 and higher
If the data is being shown in API 19, it ideally should be visible in API 23.

Categories

Resources