Android determines if an external SD card is available (gets the path to all storage devices on the phone)

Source: Internet
Author: User

获取手机里所有存储设备盘符,首先需要得到挂载在手机上的有哪些盘符,这里需要用到一个被系统隐藏的方法,即StorageManager下的getVolumePaths()方法。具体通过反射得到。方法返回值为字符串数组,getVolumeList返回所有相关信息 StorageVolume: mStorageId=131073 mPath=/storage/external //盘符(路径) mDescriptionId=17040902 mPrimary=false mRemovable=true //是否可卸载,内置SD卡无法卸载,外置SD卡可以卸载,据此可判断是否存在外置SD卡, //这样在某些APP中可以将某些数据优先存储到外置SD卡,优化存储空间资源的分配。如相机 mEmulated=false //是否是内置SD卡 mMtpReserveSpace=0 mAllowMassStorage=true mMaxFileSize=0 mOwner=null mUuid=4041-5DA0 mUserLabel=null mState=mounted //是否已载入(可用)
public class Storageutils {/* Gets all storage device information encapsulated Object */public static arraylist<volume> Getvolume (Context cont        EXT) {arraylist<volume> list_storagevolume = new arraylist<volume> ();        StorageManager StorageManager = (StorageManager) context.getsystemservice (Context.storage_service);            try {Method method_volumelist = StorageManager.class.getMethod ("Getvolumelist");            Method_volumelist.setaccessible (TRUE);            Object[] Volumelist = (object[]) Method_volumelist.invoke (StorageManager);                if (volumelist! = null) {Volume Volume;                        for (int i = 0; i < volumelist.length; i++) {try {volume = new volume ();                        Volume.setpath (String) Volumelist[i].getclass (). GetMethod ("GetPath"). Invoke (Volumelist[i])); Volume.setremovable ((Boolean) Volumelist[i].getclass (). GetMethod ("IsRemovable"). Invoke (Volumelist[i]));                        Volume.setstate (String) Volumelist[i].getclass (). GetMethod ("GetState"). Invoke (Volumelist[i]));                    List_storagevolume.add (volume);                    } catch (Illegalaccessexception e) {e.printstacktrace ();                    } catch (InvocationTargetException e) {e.printstacktrace ();                    } catch (Nosuchmethodexception e) {e.printstacktrace ();            }}}} else {LOG.E ("null", "null-------------------------------------");        }} catch (Exception E1) {e1.printstacktrace ();    } return list_storagevolume;        }/* Storage device Information Encapsulation class */public static class Volume {protected String path;        protected Boolean removable;        protected String State;        Public String GetPath () {return path; } public void SetPath (String path) {           This.path = path;        } public boolean isremovable () {return removable;        The public void setremovable (Boolean removable) {this.removable = removable;        } public String GetState () {return state;        public void SetState (String state) {this.state = state; }    }}
public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        ArrayList<StorageUtils.Volume> list_volume = StorageUtils.getVolume(this);        for (int i=0;i<list_volume.size();i++){            Log.e(i+"","path:"+list_volume.get(i).getPath()+"----"+                        "removable:"+list_volume.get(i).isRemovable()+"---"+                        "state:"+list_volume.get(i).getState());        }    }}

Remember to add permissions:

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Little Chili 4.4.4:

Huawei 6.0:

found that the external SD card path is inconsistent, only blame phone manufacturers like to blindly toss the pain from thinking of innovation. Once you get the path, you can list all of your phone's files like your phone's own file system browser.
Whether it's an internal SD card or an external SD card, it's a phone's external storage, and the phone's internal storage and external storage share the same device.
Internal Storage Path:

All apps installed to the phone will generate a package name folder in the Data/data directory, which is the path to the internal storage
Apps can read and write files only in their own package name folder
Read and write files using the path API
Getfilesdir () The path to the resulting file object is the data/data/package name/files
Files stored in this path, as long as you do not delete, it has been in
Getcachedir () The path to the resulting file object is the data/data/package name/cache

Files stored in this path are cached files and may be deleted when memory is low
The System Management application interface clears the cache, clears the contents of the cache folder, clears the data, and clears the entire package.

Reference: Android Storage

GitHub

CSDN

Pinterest

Public number

QQ Group

Android determines if an external SD card is available (gets the path to all storage devices on the phone)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.