1111import java .lang .reflect .Array ;
1212import java .lang .reflect .Method ;
1313
14+ import androidx .annotation .NonNull ;
1415import androidx .annotation .Nullable ;
1516
1617public final class FileUtil {
1718 private static final String PRIMARY_VOLUME_NAME = "primary" ;
1819
1920 @ Nullable
20- public static String getFullPathFromTreeUri (@ Nullable final Uri treeUri , Context con ) {
21- if (treeUri == null ) {
21+ public static String getFullPathFromTreeUri (@ Nullable Uri treeUri , @ NonNull Context con ) {
22+ if (treeUri == null )
2223 return null ;
23- }
24+
2425 String volumePath = FileUtil .getVolumePath (FileUtil .getVolumeIdFromTreeUri (treeUri ), con );
25- if (volumePath == null ) {
26+ if (volumePath == null )
2627 return File .separator ;
27- }
28- if (volumePath .endsWith (File .separator )) {
28+
29+ if (volumePath .endsWith (File .separator ))
2930 volumePath = volumePath .substring (0 , volumePath .length () - 1 );
30- }
3131
3232 String documentPath = FileUtil .getDocumentPathFromTreeUri (treeUri );
33- if (documentPath .endsWith (File .separator )) {
33+ if (documentPath != null && documentPath .endsWith (File .separator ))
3434 documentPath = documentPath .substring (0 , documentPath .length () - 1 );
35- }
3635
37- if (documentPath .length () > 0 ) {
38- if (documentPath .startsWith (File .separator )) {
39- return volumePath + documentPath ;
40- } else {
41- return volumePath + File .separator + documentPath ;
42- }
36+ if (documentPath != null && documentPath .length () > 0 ) {
37+ if (documentPath .startsWith (File .separator )) return volumePath + documentPath ;
38+ else return volumePath + File .separator + documentPath ;
4339 } else {
4440 return volumePath ;
4541 }
4642 }
4743
4844 @ Nullable
4945 @ SuppressWarnings ({"ConstantConditions" , "JavaReflectionMemberAccess" })
50- private static String getVolumePath (final String volumeId , Context con ) {
46+ private static String getVolumePath (@ NonNull String volumeId , @ NonNull Context con ) {
5147 try {
5248 StorageManager mStorageManager = (StorageManager ) con .getSystemService (Context .STORAGE_SERVICE );
5349 Class <?> storageVolumeClazz = Class .forName ("android.os.storage.StorageVolume" );
@@ -65,15 +61,13 @@ private static String getVolumePath(final String volumeId, Context con) {
6561 Boolean primary = (Boolean ) isPrimary .invoke (storageVolumeElement );
6662
6763 // primary volume?
68- if (primary && PRIMARY_VOLUME_NAME .equals (volumeId )) {
64+ if (primary && PRIMARY_VOLUME_NAME .equals (volumeId ))
6965 return (String ) getPath .invoke (storageVolumeElement );
70- }
7166
7267 // other volumes?
7368 if (uuid != null ) {
74- if (uuid .equals (volumeId )) {
69+ if (uuid .equals (volumeId ))
7570 return (String ) getPath .invoke (storageVolumeElement );
76- }
7771 }
7872 }
7973
@@ -84,16 +78,18 @@ private static String getVolumePath(final String volumeId, Context con) {
8478 }
8579 }
8680
81+ @ Nullable
8782 @ TargetApi (Build .VERSION_CODES .LOLLIPOP )
88- private static String getVolumeIdFromTreeUri (final Uri treeUri ) {
83+ private static String getVolumeIdFromTreeUri (@ NonNull Uri treeUri ) {
8984 final String docId = DocumentsContract .getTreeDocumentId (treeUri );
9085 final String [] split = docId .split (":" );
9186 if (split .length > 0 ) return split [0 ];
9287 else return null ;
9388 }
9489
90+ @ Nullable
9591 @ TargetApi (Build .VERSION_CODES .LOLLIPOP )
96- private static String getDocumentPathFromTreeUri (final Uri treeUri ) {
92+ private static String getDocumentPathFromTreeUri (@ NonNull Uri treeUri ) {
9793 final String docId = DocumentsContract .getTreeDocumentId (treeUri );
9894 final String [] split = docId .split (":" );
9995 if ((split .length >= 2 ) && (split [1 ] != null )) return split [1 ];
0 commit comments