private void requestPermission() { try { StorageManager sm = getSystemService(StorageManager.class); 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); } } }
public AppPagerAdapter(FragmentManager fm) { super(fm); titles.add("Clone Apps"); dirs.add(null); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { Context ctx = VApp.getApp(); StorageManager storage = (StorageManager) ctx.getSystemService(Context.STORAGE_SERVICE); for (StorageVolume volume : storage.getStorageVolumes()) { //Why the fuck are getPathFile and getUserLabel hidden?! //StorageVolume is kinda useless without those... File dir = Reflect.on(volume).call("getPathFile").get(); String label = Reflect.on(volume).call("getUserLabel").get(); if (dir.listFiles() != null) { titles.add(label); dirs.add(dir); } } } else { // Fallback: only support the default storage sources File storageFir = Environment.getExternalStorageDirectory(); if (storageFir.list() != null) { titles.add("Ghost Installation"); dirs.add(storageFir); } } }
/** * Get all available storage directories. * Inspired by CyanogenMod File Manager: * https://github.com/CyanogenMod/android_packages_apps_CMFileManager */ public static Set<File> getStorageDirectories(Context context) { if (storageDirectories == null) { try { // Use reflection to retrieve storage volumes because required classes and methods are hidden in AOSP. StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); Method method = storageManager.getClass().getMethod("getVolumeList"); StorageVolume[] storageVolumes = (StorageVolume[]) method.invoke(storageManager); if (storageVolumes != null && storageVolumes.length > 0) { storageDirectories = new HashSet<>(); for (StorageVolume volume : storageVolumes) { storageDirectories.add(new File(volume.getPath())); } } } catch (Exception e) { Log.e(LOG_TAG, e.getMessage()); } } return storageDirectories; }
@TargetApi(Build.VERSION_CODES.N) private static List<MountVolume> getVolumeListApi24(Context context) { StorageManager storageManager = context.getSystemService(StorageManager.class); List<StorageVolume> volumes = storageManager.getStorageVolumes(); List<MountVolume> list = new ArrayList<MountVolume>(volumes.size()); List<String> fileList = getMountsList(); for (StorageVolume volume : volumes) { MountVolume mountVolume = new MountVolume(); mountVolume.setUuid(volume.isPrimary() ? "primary" : volume.getUuid()); mountVolume.setPathFile(volume.isPrimary() ? new File(getFilePath(fileList, "emulated") + "/0") : new File(getFilePath(fileList, volume.getUuid()))); mountVolume.setPrimary(volume.isPrimary()); mountVolume.setEmulated(volume.isEmulated()); mountVolume.setRemovable(volume.isRemovable()); mountVolume.setDescription(volume.getDescription(context)); mountVolume.setState(volume.getState()); list.add(mountVolume); } return list; }
private static void printStorageVolumeMethods(Context context){ try { StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); Method method = StorageManager.class.getMethod("getVolumeList"); StorageVolume[] storageVolumes = (StorageVolume[]) method.invoke(storageManager); System.out.println("storageVolumes : " + storageVolumes.length); Method[] methods = storageVolumes[0].getClass().getMethods(); for(Method m : methods){ System.out.println("storageVolumes : methodName = " + m.getName() + " methodParams = " + getParams(m.getParameterTypes()) + " returnType = " + m.getReturnType().getSimpleName()); } }catch (Exception e){ e.printStackTrace(); } }
public static StorageVolume[] getStorageVolumes(Context context){ StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); try { Method method = StorageManager.class.getMethod("getVolumeList"); return (StorageVolume[]) method.invoke(storageManager); }catch (Exception e){ e.printStackTrace(); } return null; }
public static void formatSDcard(Context context) { StorageManager storageManager = StorageManager.from(context); final StorageVolume[] storageVolumes = storageManager.getVolumeList(); Intent intent = new Intent(ExternalStorageFormatter.FORMAT_ONLY); intent.setComponent(ExternalStorageFormatter.COMPONENT_NAME); intent.putExtra(StorageVolume.EXTRA_STORAGE_VOLUME, storageVolumes[1]); context.startService(intent); }
public static List<Storage> getStorageList(Context context){ List<Storage> result = new ArrayList<>(); StorageVolume[] storageVolumes = getStorageVolumes(context); if(storageVolumes==null || storageVolumes.length<=0) return result; StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); try { Storage storage; Method invokeMethod; for(int i=0;i<storageVolumes.length;i++){ invokeMethod = storageVolumes[i].getClass().getMethod("getState"); if(Environment.MEDIA_MOUNTED.equals(invokeMethod.invoke(storageVolumes[i]).toString())){ storage = new Storage(); //getDescriptionMethod = storageVolumes[i].getClass().getMethod("getDescription",Context.class); //allowMassStorageMethod = storageVolumes[i].getClass().getMethod("allowMassStorage"); //isRemovableMethod = storageVolumes[i].getClass().getMethod("isRemovable"); invokeMethod = storageVolumes[i].getClass().getMethod("getPath"); String path = (String) invokeMethod.invoke(storageVolumes[i]); //set storage path storage.setPath(path); //set storage description invokeMethod = storageVolumes[i].getClass().getMethod("getDescription",Context.class); storage.setDescription((String) invokeMethod.invoke(storageVolumes[i],context)); //set storage is removable invokeMethod = storageVolumes[i].getClass().getMethod("isRemovable"); storage.setRemovableStorage((Boolean) invokeMethod.invoke(storageVolumes[i])); //set storage is allowMassStorage invokeMethod = storageVolumes[i].getClass().getMethod("allowMassStorage"); storage.setAllowMassStorage((boolean) invokeMethod.invoke(storageVolumes[i])); //set storage is emulate invokeMethod = storageVolumes[i].getClass().getMethod("isEmulated"); storage.setEmulated((boolean) invokeMethod.invoke(storageVolumes[i])); //set storage is primary invokeMethod = storageVolumes[i].getClass().getMethod("isPrimary"); storage.setPrimary((boolean) invokeMethod.invoke(storageVolumes[i])); //set storage lowBytes limit invokeMethod = StorageManager.class.getMethod("getStorageLowBytes",File.class); storage.setLowBytesLimit((Long) invokeMethod.invoke(storageManager,new File(path))); //set storage fullBytes limit invokeMethod = StorageManager.class.getMethod("getStorageFullBytes",File.class); storage.setFullBytesLimit((Long) invokeMethod.invoke(storageManager,new File(path))); //set storage total size and available size long[] sizeInfo = getStorageSizeInfo(path); storage.setTotalSize(sizeInfo[0]); storage.setAvailableSize(sizeInfo[1]); result.add(storage); } } }catch (Exception e){ e.printStackTrace(); } return result; }
static boolean checkStoragePermission(Fragment fragment) { if (Build.VERSION.SDK_INT < 23) { // runtime permissions not supported return false; } final Context context = fragment.getActivity(); final String prefName = context.getString(R.string.download_dir_pref); final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); final String dDir = prefs.getString(prefName, ""); try { if (Build.VERSION.SDK_INT >= 24) { if (!TextUtils.isEmpty(dDir)) { final File file = new File(dDir); final StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); final StorageVolume sv = sm.getStorageVolume(file); if (sv == null || !Environment.MEDIA_MOUNTED.equals(sv.getState())) { return false; } } } else { // use old-fashioned stat() check try { final File primaryStorage = Environment.getExternalStorageDirectory(); if (primaryStorage != null) { final StructStat dirStat = Os.stat(dDir); final StructStat primaryStat = Os.stat(primaryStorage.getAbsolutePath()); if (dirStat.st_dev != primaryStat.st_dev) { // the directory is not on primary external storage, bail return false; } } } catch (Exception ignored) { } } if (context.checkSelfPermission(WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { fragment.requestPermissions(new String[] { WRITE_EXTERNAL_STORAGE }, R.id.req_file_permission); return true; } } catch (RuntimeException e) { e.printStackTrace(); } return false; }
private Api24StorageVolumeImpl(String path, StorageVolume volume) { mPath = path; mStorageVolume = volume; }
private Api24StorageVolumeImpl(Parcel in) { this.mStorageVolume = in.readParcelable(StorageVolume.class.getClassLoader()); this.mPath = in.readString(); }