dpa99c / cordova-diagnostic-plugin

Cordova/Phonegap plugin to manage device settings
540 stars 361 forks source link

Some devices cannot list OTG devices #356

Closed batcholi closed 5 years ago

batcholi commented 5 years ago

I'm submitting a ... (check one with "x"):

Bug report

On some android devices, the app cannot see removable devices (such as micro-sd cards or USB Flash drives via OTG adapter).

cordova.plugins.diagnostic.requestExternalStorageAuthorization(function(){
    cordova.plugins.diagnostic.getPermissionAuthorizationStatusconsole.log, console.error, cordova.plugins.diagnostic.permission.READ_EXTERNAL_STORAGE);
    cordova.plugins.diagnostic.getExternalStorageAuthorizationStatus(console.log, console.error);
    cordova.plugins.diagnostic.getExternalSdCardDetails(console.log, console.error);
}, console.error);

https://stackoverflow.com/questions/56085400/cordova-app-cant-find-removable-storage-on-some-devices-only

Current behavior: Outputs : GRANTED GRANTED [ ]

Required permissions are granted, but no drive/sdcard is listed. But we can browse the files in the OTG and SD Card with other File Manager apps. This behavior happens only on some devices including a Pixel 3, Pixel 2, Mi MIX 3, HTC U11, Samsung S9+.

Expected behavior: GRANTED GRANTED [ {...}, {...} ]

App can list the drives. This works on most devices including a Samsung Note 9 on Android 9 and a Note 3 on Android 5 and lots of other devices of all brands and android versions.

Steps to reproduce:

Code above, on certain devices only, I have not yet found a common difference between the ones that do work and the ones that do not.

Environment information

Runtime issue

Android build issue:

Related code:

cordova.plugins.diagnostic.requestExternalStorageAuthorization(function(){
    cordova.plugins.diagnostic.getPermissionAuthorizationStatusconsole.log, console.error, cordova.plugins.diagnostic.permission.READ_EXTERNAL_STORAGE);
    cordova.plugins.diagnostic.getExternalStorageAuthorizationStatus(console.log, console.error);
    cordova.plugins.diagnostic.getExternalSdCardDetails(console.log, console.error);
}, console.error);

Console output

GRANTED GRANTED [ ]


# AndroidManifest.xml ``` ```
dpa99c commented 5 years ago

The getExternalSdCardDetails() function is intended to find paths of external removable SD cards on which the SD card adapter is directly mounted on the device, such as those in the Samsung Galaxy S range of devices.

OTG access is entirely different on Android 6+: OTG storage devices must be accessed via the Storage Access Framework (see this SO post). This is how dedicated File Manager apps support OTG storage devices.

getExternalSdCardDetails() explicitly attempts to filter out non-SD card storage paths such as OTG devices, as can be seen in the source code. The reason for this is that even if the paths are found, on Android 6+ root access is required to read/write to them via the File API. Feel free to fork the plugin and comment out those lines but I think you will not be able to access the returned paths via the File API.

But in general, storage access via OTG is beyond the scope of the getExternalSdCardDetails() function. I will update the plugin documentation to make this explicitly clear.

File access via the Storage Access Framework is not trivial (as can be seen from the documentation). However it may be possible in future to add support for it as a new feature of this plugin's ExternalStorage module.