2

I am developing a file manager under Intellij IDEA 14.1.6 and I have to support USB OTG flash drives. My file manager works fine with USB OTG on devices with API < 23 (Marshmallow). I have seen multiple references with the statement (1, 2, 3):

...it doesn’t require full support until you choose to target version 23 in your application. If you are targeting version 22 or below, your application will request all permissions at install time just as it would on any device running an OS below Marshmallow.

My project has target:

 <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="22"/>

but Nexus 5 (upgraded to Android 6.0.1) can't see USB OTG flash drive, unlike to a few other devices with API 19, 21, 22 which can read and write to flash drive pretty easy.

The question is: Does anybody have checked this statement? Does this statement apply to API 23 or to its preview version (M) only? Does this statement apply to Android Studio only?

EDIT: The only way to get Nexus 5 with Marshmallow works with USB OTG as a previous API level devices is to use a command:

adb shell sm set-force-adoptable true

from terminal.

Community
  • 1
  • 1
isabsent
  • 3,683
  • 3
  • 25
  • 46
  • have you implemented runtime permission model for M version?? – Vivek Mishra Jan 18 '16 at 12:37
  • No. I just want to get it working as a previous API level devices by targeting project to API level 22. – isabsent Jan 18 '16 at 12:46
  • Possible duplicate of [How app can access files on USB OTG storages in Android 6.0 (API level 23) without root?](http://stackoverflow.com/questions/32201169/how-app-can-access-files-on-usb-otg-storages-in-android-6-0-api-level-23-witho) – cuihtlauac Jan 20 '16 at 12:18
  • No, it isn't a duplicate. In my question I try to clarify a usage of targeting to lower API to avoid of making changes in a code working fine on previous API levels. It isn't discussed in your link. – isabsent Jan 20 '16 at 13:29

1 Answers1

0

In Android 6.0+, the user needs to grant Storage access to the app else the application can't access Internal or External Storage.

You can do this in your app's MainActivity's onCreate(Bundle savedInstanceState) Method :

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
    checkForPermissionsGrantStatus();

Than :

private void checkForPermissionsGrantStatus() {
    int storagePermissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if(storagePermissionCheck == PackageManager.PERMISSION_DENIED)
        requestForPermissionAccess(Manifest.permission.WRITE_EXTERNAL_STORAGE, 1);
}

private void requestForPermissionAccess(String permission, int requestCode) {
    ActivityCompat.requestPermissions(this, new String[]{permission}, requestCode);
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
        Toast.makeText(this, "Storage Permission Granted", Toast.LENGTH_SHORT).show();
    }
}

On every run your application will check if the device runs Android 6.0+. If it does than it will check if it has Storage Permission. If yes than it will stop there else show a popup asking user to grant storage access.

Screenshot : Screenshot showing Storage Permission Request Popup

Arnab Jain
  • 247
  • 1
  • 5
  • Do you mean of using with above code? Will it work for android:targetSdkVersion="22"? – isabsent Jan 18 '16 at 13:47
  • I haven't tried it with android:targetSdkVersion="22" but you can give it a shot. Android Studio will most probably warn you if it's not possible. Else I can try it for you. But I am not near my work PC so it will take time. – Arnab Jain Jan 18 '16 at 13:49
  • I am not under Android Studio and I would like do not do changes in my existing code to get it working on Marshmallow - just point to API 22. – isabsent Jan 18 '16 at 13:55
  • How are you building the app than? Command Line? Don't change anything. Just add the above code block in onCreate and add those 3 functions in MainActivity[Or whatever you call it]. Keep android:targetSdkVersion="22" and run the app on a API 23 device. It should ask for permission and than you will be able to access Storage [SD Card and USB OTG] – Arnab Jain Jan 18 '16 at 13:57
  • I just checked. You need to set android:targetSdkVersion="23" in order to use the Permission check I shared. I am sorry my answer does not serves your exact need. If you are willing to set android:targetSdkVersion="23" than my answer will work. – Arnab Jain Jan 18 '16 at 14:15
  • Did you have checked your code on **real** USB OTG flash drive on a **real** Nexus 5 (API 23 Marshmallow) device!? I am not able to read flash drive with android:targetSdkVersion="23" in Manifest after implementing your code... – isabsent Jan 20 '16 at 06:58
  • I don't have Nexus 5. So, no I haven't checked on a real Nexus 5. – Arnab Jain Jan 20 '16 at 08:20