2

I have tried to write the file into "/mnt/sdcard" path its return error. Code on JNI layer(C++) using normal fopen("new.txt","w")

And also tried the same with "/mnt/sdcard/Android/data/" even failed.

Device: Android Q Beta4

fopen("/mnt/sdcard/new.txt","w") fopen("/mnt/sdcard/Android/data/new.txt","w")

We already Refer the storage restriction in Android Developer page for Android Q.

Which path used for store the public information?

VMS
  • 137
  • 2
  • 8
  • Now we are migarating Android R for our testing purpose, but we are facing "Permission denied" issue. Path: /mnt/sdcard/Android/data/package_name/files/package_name/* Note: Get the public path using below code, Context context = _activity.getApplicationContext(); return context.getExternalFilesDir(null) +"/" + getPackageName(); We need to do anything special for Android R(11)? Could you please suggest? – VMS May 13 '20 at 13:57
  • @CommonsWare Please help us? – VMS May 13 '20 at 13:59

1 Answers1

2

Apps cannot store data in arbitrary locations on external or removable storage in Android Q (by default) or Android R (for all apps). The fact that you happen to be using the NDK does not change this.

Your Java code should pass down a valid filesystem path for your NDK code to use (e.g., getExternalFilesDir() on Context).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks @CommonsWare , we also tried the same. Its works fine. – VMS Jul 04 '19 at 06:34
  • @CommonsWare I think I had a similar issue with File API in android 10 beta builds. I've fixed it with storage access framework. But in Android 10 official release, `FileOutputStream(file)` is no longer throwing FileNotFound exception. I am not using `requestLegacyStorage = true` and my tagetsdk and compileSdk are 29. Did google change anything in the final release? – Aswin P Ashok Nov 12 '19 at 14:12
  • @AswinPAshok: If you requested legacy storage, then external storage behaves for you on Android 10 the way that it did from Android 4.4 through 9. Google *did* change how they implemented "scoped storage" several times through the beta series, though, so perhaps you are seeing some impact of those changes. – CommonsWare Nov 12 '19 at 14:13
  • @CommonsWare I am not using `requestLegacyStorage = true` and I am not getting the exception. Google still says apps targeting android 10 will be given a scoped view into storage and yet I am able to copy files into any arbitrary location without getting any exceptions. First I thought it was because I had a previous version targetting android 9 was installed, then I updated my app. Just to get that out of the equation, I installed fresh android 10 on the device and freshly installed app with targetSdk 29 and still no issues with File API. I am confused. – Aswin P Ashok Nov 12 '19 at 14:22
  • @AswinPAshok: "I am not using requestLegacyStorage = true" -- sorry, I misread your earlier comment. However, I cannot reproduce your findings on a Pixel 2 or a Pixel 4. What is your test environment. – CommonsWare Nov 12 '19 at 22:18
  • @CommonsWare I know what I am about to say does not make any sense, I made another app, and `FileOutputStream(file)` is throwing FileNotFoundException. But when I do the same in my application that I am currently migrating to Q, it does not crash. [Here](https://gist.github.com/AswinpAshok/85112a25192943535bf57b7ad4b2179a) is my build.gradle and [here](https://gist.github.com/AswinpAshok/bf39e88b9e7346b92d20c05e62fe8ca0) is how I copy files. I am trying this out on Pixel 2 (Build number QP1A.191105.004) – Aswin P Ashok Nov 13 '19 at 12:03
  • @AswinPAshok: Are you certain that your manifest does not contain `android:requestLegacyExternalStorage` on the `` element? – CommonsWare Nov 13 '19 at 12:04
  • @CommonsWare I am sure, I have not added it. Now I have tested by adding `android:requestLegacyExternalStorage="false"` and my app can copy files without any issues. And I have tested the same on Oneplus 7 running android 10 and it's not crashing on it. – Aswin P Ashok Nov 13 '19 at 12:07
  • 1
    @AswinPAshok: I do not know what could cause the behavior that you are describing -- sorry! – CommonsWare Nov 13 '19 at 12:10
  • @CommonsWare I was just confused about this (I still am), however I'll just continue changing everything to SAF. Thanks for your time. – Aswin P Ashok Nov 13 '19 at 12:12
  • 1
    @CommonsWare I have finally found out the cause of such behavior. My application was granted `android.permission.REQUEST_INSTALL_PACKAGES` via settings. After granting the same permission to my other app it can copy files. When I denied this permission app started crashing. I don't think the documentation mention it anywhere. – Aswin P Ashok Nov 13 '19 at 12:45
  • That is very strange. I will try to reproduce these findings next week. But, I am glad that you got to the bottom of your problem! – CommonsWare Nov 13 '19 at 13:12
  • 1
    @CommonsWare I think Google let it this way, because [getPackageArchiveInfo](https://developer.android.com/reference/android/content/pm/PackageManager#getPackageArchiveInfo(java.lang.String,%20int)) still uses file path, and Google have not provided any other SAF compatible API. Since this API is used by many apps with `REQUEST_INSTALL_PACKAGES` permission, it makes sense to let those apps use the file system with File API. (This is my assumption) – Aswin P Ashok Nov 13 '19 at 14:25
  • 1
    @AswinPAshok: OK, I finally got some time to repro the problem. What happens is that if you have been granted `REQUEST_INSTALL_PACKAGES`, you are automatically opted into legacy storage, as if you had `android:requestLegacyExternalStorage="true"`. `Environment.isExternalStorageLegacy()` will return `true`, instead of `false` if you do not hold `REQUEST_INSTALL_PACKAGES`. Presumably, this will get cleaned up in Android R, when this whole "legacy external storage" stuff is supposed to be going away. – CommonsWare Nov 23 '19 at 18:06
  • Can you help me with https://stackoverflow.com/questions/63543414/rename-file-of-the-external-storage-which-is-created-by-app-in-android-10-worki – jazzbpn Aug 24 '20 at 03:45