2

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.

auspicious99
  • 3,902
  • 1
  • 44
  • 58
Jaydip S
  • 21
  • 2
  • Show your code for sharing. – blackapps Nov 19 '21 at 12:48
  • @blackapps can you please check in Redmi device which have 11 version. now pick any one image from xiaomi official file manager app and share to whatsapp or Facebook or Instagram. case 2. share image whatsapp to other app. If you still want me to share the code. let me know. – Jaydip S Nov 19 '21 at 13:20
  • 1
    I will gladly do that. Please send me such a device. Meanwhile you better post your code. – blackapps Nov 19 '21 at 14:51
  • @blackapps posted the code. – Jaydip S Nov 20 '21 at 04:59
  • `Whenever we share a media file with our app,` ??? But you did not post code for that. If an app shares a file it uses ACTION_SEND to ... send a file. What you posted looks more as if your app receives a shared file. Pretty confusing. You app implemented an intent-filter for ACTION_SEND? And you did not tell us? – blackapps Nov 20 '21 at 06:46
  • `String path = videoUri.getPath();` You can do nothing with getPath(). You bettee use the whole uri. Have a look at .toString(). – blackapps Nov 20 '21 at 06:51
  • You also should mention Android version of all other devices. – blackapps Nov 20 '21 at 07:09

1 Answers1

0

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.

auspicious99
  • 3,902
  • 1
  • 44
  • 58