外置内存卡:(方法1)String waizhiString=System.getenv("SECONDARY_STORAGE");注意:这个功能只能在API 22及以下使用,API 23版本一移除SECONDARY_STORAGE;
(方法2)
private static String getStoragePath(Context mContext,boolean is_remove)
{
String path=null;
StorageManager mStorageManager = (StorageManager)mContext.getSystemService(Context.STORAGE_SERVICE);
Class<?>storageVolumeClass = null;
try{
storageVolumeClass=Class.forName("android.os.storage.StorageVolume");
Method getVolumeList=mStorageManager.getClass().getMethod("getVolumeList");
Method isRemovable = storageVolumeClass.getMethod("isRemovable");
Method getPath = storageVolumeClass.getMethod("getPath");
Object result=getVolumeList.invoke(mStorageManager);
final int length =Array.getLength(result);
for(int i=0;i<length;i++){
Object storageVolumeElement=Array.get(result, i);
path = (String)getPath.invoke(storageVolumeElement);
boolean removable=(Boolean)isRemovable.invoke(storageVolumeElement);
if(is_remove==removable){
return path;
}
}
System.out.println("Second---->"+path);
}catch(ClassNotFoundException e)
{
e.printStackTrace();
}catch (InvocationTargetException e) {
// TODO: handle exception
e.printStackTrace();
}catch (NoSuchMethodException e) {
// TODO: handle exception
e.printStackTrace();
}catch (IllegalAccessException e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
在开发的时候应该尽量用方法二。
内置存储卡:Environment.getExternalStorageDirectory().getPath();
(方法2)
private static String getStoragePath(Context mContext,boolean is_remove)
{
String path=null;
StorageManager mStorageManager = (StorageManager)mContext.getSystemService(Context.STORAGE_SERVICE);
Class<?>storageVolumeClass = null;
try{
storageVolumeClass=Class.forName("android.os.storage.StorageVolume");
Method getVolumeList=mStorageManager.getClass().getMethod("getVolumeList");
Method isRemovable = storageVolumeClass.getMethod("isRemovable");
Method getPath = storageVolumeClass.getMethod("getPath");
Object result=getVolumeList.invoke(mStorageManager);
final int length =Array.getLength(result);
for(int i=0;i<length;i++){
Object storageVolumeElement=Array.get(result, i);
path = (String)getPath.invoke(storageVolumeElement);
boolean removable=(Boolean)isRemovable.invoke(storageVolumeElement);
if(is_remove==removable){
return path;
}
}
System.out.println("Second---->"+path);
}catch(ClassNotFoundException e)
{
e.printStackTrace();
}catch (InvocationTargetException e) {
// TODO: handle exception
e.printStackTrace();
}catch (NoSuchMethodException e) {
// TODO: handle exception
e.printStackTrace();
}catch (IllegalAccessException e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}
在开发的时候应该尽量用方法二。
内置存储卡:Environment.getExternalStorageDirectory().getPath();