1

I am developing a file manager with USB OTG flash drives support. My file manager works fine with USB OTG on devices with API < 23 (Marshmallow) and I am going to implement new Android 6.0 runtime permissions with a code below:

@Override
public void onCreate(Bundle savedInstanceState) {   
    super.onCreate(savedInstanceState);
    ...
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
        checkForPermissionGrantStatus();
        if (!Settings.System.canWrite(this))
            askWriteSettingsPermission();
    }
    ...
}

@TargetApi(Build.VERSION_CODES.M)
private void askWriteSettingsPermission(){
    new AlertDialog.Builder(this)
            .setMessage(R.string.write_settings_permission)
            .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
                    intent.setData(Uri.parse("package:" + getPackageName()));
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                    try {
                        startActivity(intent);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).show();
}

private void checkForPermissionGrantStatus() {
    String[] dangerousPermissionsArray = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE};
    List<String> notIssuedPermissions = new ArrayList<>();
    for (String permission : dangerousPermissionsArray) {
        if (ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_DENIED)
            notIssuedPermissions.add(permission);
    }
    if (notIssuedPermissions.size() > 0)
        ActivityCompat.requestPermissions(this, notIssuedPermissions.toArray(new String[notIssuedPermissions.size()]), 95);
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case 95:
            for (int grantResult : grantResults)
                if (grantResult != PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(this, "Not all permissions obtained.", Toast.LENGTH_SHORT).show();
                    return;
                }
            Toast.makeText(this, "All permissions obtained.", Toast.LENGTH_SHORT).show();
            break;
    }
}

My project has target

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

and it is the only change I have made in Manifest.xml in comparison with previous application version, which works fine with USB OTG on devices with API < 23.

I am accepting two system dialogs and one AlertDialog which appear after application starts, but my USB OTG flash drive is still unreadable. The question is: Do I need to implement some additional steps to force USB OTG flash drive be readable under Android 6.0.1 on Nexus 5 device?

P.S. I do not use Android Studio and Gradle - is it crusial?

Floern
  • 33,559
  • 24
  • 104
  • 119
isabsent
  • 3,683
  • 3
  • 25
  • 46
  • Under wich path do you see your usb drive mounted? – greenapps Jan 20 '16 at 10:22
  • I can't find USB OTG mount point at all. It is absent in /storage. I suppose it is in /mnt/media_rw but this path is unreadable. The only way to see USB OTG mount point and read its content is to use "adb shell sm set-force-adoptable true" from terminal. – isabsent Jan 20 '16 at 10:34
  • 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 10:39
  • There is no path to begin with. So don't tell that it is unreadable. You should look at the Storage Access Framework instead of the File class. There have been several posts already in this forum about usb drives not accessable with file explorers. – greenapps Jan 20 '16 at 10:53
  • No, it is not a duplicate. My app goes through the Storage Access Framework to interact with files on removable SD-card on devices with API 21, 22. I can view its content and I am using the same way to access a content on a portable storage (USB OTG flash drive). – isabsent Jan 20 '16 at 10:55
  • And I can read its content pretty easy after "adb shell sm set-force-adoptable true" from terminal. – isabsent Jan 20 '16 at 10:59
  • Well then its unclear why you mentioned file system paths. What code do you use to determine that that drive is unreadable with saf? – greenapps Jan 20 '16 at 10:59
  • What happens if you do that: http://stackoverflow.com/questions/32699129 ? – cuihtlauac Jan 20 '16 at 11:03
  • @cuihtlauac - thanks, will try it. – isabsent Jan 20 '16 at 11:10
  • @greenapps - I am able to see mounted USB OTG device in the path /storage/A076-8D6F after reformatting flash drive (FAT32), but I still can't read its content. It seems that initial formatting was erroneous and prevented me from seeing flash drive mount point. – isabsent Jan 20 '16 at 17:57

0 Answers0