Java Code Examples for android.os.storage.StorageVolume#createAccessIntent()

The following examples show how to use android.os.storage.StorageVolume#createAccessIntent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DCCManager.java    From revolution-irc with GNU General Public License v3.0 6 votes vote down vote up
private void askSystemDownloadsPermission(Runnable cb, boolean noShowDenialDialog) {
    if (cb != null) {
        if (mStoragePermissionRequestCallbacks == null)
            mStoragePermissionRequestCallbacks = new ArrayList<>();
        mStoragePermissionRequestCallbacks.add(cb);
    }
    mPermissionRequestPending = true;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        StorageManager manager = (StorageManager) mActivity
                .getSystemService(Context.STORAGE_SERVICE);
        StorageVolume volume = manager.getPrimaryStorageVolume();
        Intent intent = volume.createAccessIntent(Environment.DIRECTORY_DOWNLOADS);
        intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
        mActivity.startActivityForResult(intent, mDownloadsPermissionRequestCode);
    } else {
        if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity,
                Manifest.permission.WRITE_EXTERNAL_STORAGE) && !noShowDenialDialog) {
            showSystemDownloadsPermissionDenialDialog();
        } else {
            ActivityCompat.requestPermissions(mActivity,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    mStoragePermissionRequestCode);
        }
    }
}
 
Example 2
Source File: MainActivity.java    From Wrox-ProfessionalAndroid-4E with Apache License 2.0 5 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.N)
private void listing8_4() {
  // Listing 8-4: Requesting access with Scoped Directory Access
  StorageManager sm =
    (StorageManager) getSystemService(Context.STORAGE_SERVICE);

  StorageVolume volume = sm.getPrimaryStorageVolume();
  Intent intent = volume.createAccessIntent(Environment.DIRECTORY_PICTURES);

  startActivityForResult(intent, PICTURE_REQUEST_CODE);
}
 
Example 3
Source File: MainActivity.java    From FCM-for-Mojo with GNU General Public License v3.0 5 votes vote down vote up
private void requestPermission() {
    try {
        StorageManager sm = getSystemService(StorageManager.class);
        //noinspection ConstantConditions
        StorageVolume volume = sm.getPrimaryStorageVolume();
        Intent intent = volume.createAccessIntent(Environment.DIRECTORY_DOWNLOADS);
        startActivityForResult(intent, REQUEST_CODE);
    } catch (Exception e) {
        //Toast.makeText(this, R.string.cannot_request_permission, Toast.LENGTH_LONG).show();
        Toast.makeText(this, "Can't use Scoped Directory Access.\nFallback to runtime permission.", Toast.LENGTH_LONG).show();
        Log.wtf("FFM", "can't use Scoped Directory Access", e);

        Crashlytics.logException(e);

        // fallback to runtime permission
        if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
        }
    }
}
 
Example 4
Source File: MainActivity.java    From Android-9-Development-Cookbook with MIT License 4 votes vote down vote up
public void onAccessClick(View view) {
    StorageManager storageManager = (StorageManager)getSystemService(Context.STORAGE_SERVICE);
    StorageVolume storageVolume = storageManager.getPrimaryStorageVolume();
    Intent intent = storageVolume.createAccessIntent(Environment.DIRECTORY_MUSIC);
    startActivityForResult(intent, REQUEST_FOLDER_MUSIC);
}
 
Example 5
Source File: NearbyViewBinder.java    From fdroidclient with GNU General Public License v3.0 4 votes vote down vote up
public static void updateUsbOtg(final Context context) {
    if (Build.VERSION.SDK_INT < 24) {
        return;
    }
    if (swapView == null) {
        Utils.debugLog(TAG, "swapView == null");
        return;
    }
    TextView storageVolumeText = swapView.findViewById(R.id.storage_volume_text);
    Button requestStorageVolume = swapView.findViewById(R.id.request_storage_volume_button);
    storageVolumeText.setVisibility(View.GONE);
    requestStorageVolume.setVisibility(View.GONE);

    final StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
    for (final StorageVolume storageVolume : storageManager.getStorageVolumes()) {
        if (storageVolume.isRemovable() && !storageVolume.isPrimary()) {
            Log.i(TAG, "StorageVolume: " + storageVolume);
            final Intent intent = storageVolume.createAccessIntent(null);
            if (intent == null) {
                Utils.debugLog(TAG, "Got null Storage Volume access Intent");
                return;
            }
            storageVolumeText.setVisibility(View.VISIBLE);

            String text = storageVolume.getDescription(context);
            if (!TextUtils.isEmpty(text)) {
                requestStorageVolume.setText(text);
                UsbDevice usb = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                if (usb != null) {
                    text = String.format("%s (%s %s)", text, usb.getManufacturerName(), usb.getProductName());
                    Toast.makeText(context, text, Toast.LENGTH_LONG).show();
                }
            }

            requestStorageVolume.setVisibility(View.VISIBLE);
            requestStorageVolume.setOnClickListener(new View.OnClickListener() {
                @Override
                @RequiresApi(api = 24)
                public void onClick(View v) {
                    List<UriPermission> list = context.getContentResolver().getPersistedUriPermissions();
                    if (list != null) for (UriPermission uriPermission : list) {
                        Uri uri = uriPermission.getUri();
                        if (uri.getPath().equals(String.format("/tree/%s:", storageVolume.getUuid()))) {
                            intent.setData(uri);
                            TreeUriScannerIntentService.onActivityResult(context, intent);
                            return;
                        }
                    }
                    ((Activity) context).startActivityForResult(intent,
                        MainActivity.REQUEST_STORAGE_ACCESS);
                }
            });
        }
    }
}