第一种,通过DocumentProvider:文章来源:https://www.toymoban.com/news/detail-541661.html
private void handleDocumentsProvider() { String authority = "com.android.externalstorage.documents"; final Uri rootsUri = DocumentsContract.buildRootsUri("com.android.externalstorage.documents"); ContentResolver resolver = getContentResolver(); final ContentProviderClient client = resolver.acquireUnstableContentProviderClient( authority); final PackageManager pm = getPackageManager(); ProviderInfo provider = pm.resolveContentProvider( authority, PackageManager.GET_META_DATA); if (provider == null) { Log.w(TAG, "Failed to get provider info for " + authority); } if (!provider.exported) { Log.w(TAG, "Provider is not exported. Failed to load roots for " + authority); } if (!provider.grantUriPermissions) { Log.w(TAG, "Provider doesn't grantUriPermissions. Failed to load roots for " + authority); } if (!android.Manifest.permission.MANAGE_DOCUMENTS.equals(provider.readPermission) || !android.Manifest.permission.MANAGE_DOCUMENTS.equals(provider.writePermission)) { Log.w(TAG, "Provider is not protected by MANAGE_DOCUMENTS. Failed to load roots for " + authority); } Cursor cursor = null; try { cursor = client.query(rootsUri, null, null, null, null); } catch (RemoteException e) { e.printStackTrace(); } while (cursor.moveToNext()) { String rootId = getCursorString(cursor, DocumentsContract.Root.COLUMN_ROOT_ID); String title = getCursorString(cursor, DocumentsContract.Root.COLUMN_TITLE); String summary = getCursorString(cursor, DocumentsContract.Root.COLUMN_SUMMARY); String documentId = getCursorString(cursor, DocumentsContract.Root.COLUMN_DOCUMENT_ID); long availableBytes = getCursorLong(cursor, DocumentsContract.Root.COLUMN_AVAILABLE_BYTES); long capacityBytes = getCursorLong(cursor, DocumentsContract.Root.COLUMN_CAPACITY_BYTES); String mimeTypes = getCursorString(cursor, DocumentsContract.Root.COLUMN_MIME_TYPES); String flags = getCursorString(cursor, DocumentsContract.Root.COLUMN_FLAGS); DocumentPathBean documentPathBean = new DocumentPathBean(rootId, title, documentId, availableBytes, flags, "/storage/" + rootId); Log.e(TAG, "rootId:" + rootId + "flags:" + flags); if(!"primary".equals(rootId) && !"home".equals(rootId)){ Log.e(TAG, "rootId:" + rootId + "title:" + title +"summary:" + summary +"documentId:" + documentId +"availableBytes:" + availableBytes +"capacityBytes:" + capacityBytes +"mimeTypes:" + mimeTypes); } } }
第二种,通过StorageManager,无法使用的记得用反射;文章来源地址https://www.toymoban.com/news/detail-541661.html
private HashMap<String, String> getUsbDrivePath() { final StorageManager sm = getSystemService(StorageManager.class); final List<StorageVolume> volumes = sm.getStorageVolumes(); HashMap<String, String> paths = new HashMap<>(); volumes.forEach(new Consumer<StorageVolume>() { @Override public void accept(StorageVolume volume) { File path = volume.getPathFile(); if (volume.isPrimary()) { Log.v("storageVolume:", "put desc primary"); // pathMap.put("primary",path.getAbsolutePath()); } else { String state = volume.getState(); Log.v("storageVolume:", "path" + path.exists()); Log.v("storageVolume:", "path isDirectory" + path.isDirectory()); Log.v("storageVolume:", "path isFile " + path.isFile()); Log.v("storageVolume:", "path " + path.getAbsolutePath()); Log.v("storageVolume:", "put desc " + volume.getDescription(DocumentActivity.this) + "volume.isPrimary:" + volume.isPrimary()); // String mediaStoreVolumeName = storageVolume.getMediaStoreVolumeName(); // String absolutePath = storageVolume.getDirectory().getAbsolutePath(); } } }); return paths; }
到了这里,关于android 9.0以上版本,获取U盘路径的两种方式的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!