package com.test.StorageList; import android.content.Context; import android.os.storage.StorageManager; import android.util.Log; import java.lang.reflect.Method; public class StorageUtil { private static final String TAG = "===StorageUtil==="; public static String getPrimaryStoragePath(Context context) { try { StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); Method getVolumePathsMethod = StorageManager.class.getMethod("getVolumePaths", null); String[] paths = (String[]) getVolumePathsMethod.invoke(sm, null); // first element in paths[] is primary storage path return paths[0]; } catch (Exception e) { Log.e(TAG, "getPrimaryStoragePath() failed", e); } return null; } public static String getSecondaryStoragePath(Context context) { try { StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); Method getVolumePathsMethod = StorageManager.class.getMethod("getVolumePaths", null); String[] paths = (String[]) getVolumePathsMethod.invoke(sm, null); // second element in paths[] is secondary storage path return paths[1]; } catch (Exception e) { Log.e(TAG, "getSecondaryStoragePath() failed", e); } return null; } public static String getStorageState(Context context, String path) { try { StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); Method getVolumeStateMethod = StorageManager.class.getMethod("getVolumeState", new Class[] {String.class}); String state = (String) getVolumeStateMethod.invoke(sm, path); return state; } catch (Exception e) { Log.e(TAG, "getStorageState() failed", e); } return null; } }
利用反射获取android内置外置SDCARD路径
最新推荐文章于 2022-07-28 13:33:00 发布