Get the phone SD card path (native machine multiple SD card)

Source: Internet
Author: User

Through the system's
Environment.getexternalstoragedirectory (). Getabsolutefile ();
Can only get the system SD card path, for the SD card of the domestic God machine, want to external SD card is powerless.
Here is a written tool class that gets all the SD card paths by reflecting the system-hidden get all Mount Path method:
 Package com.itheima.mobilesafe.utils;
import java.lang.reflect.InvocationTargetException;
import Java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import Android.content.Context;
import Android.os.storage.StorageManager;
 Public class externalmemoryutils {
    /**
     * Gets all the external storage paths, returned as a collection
     
     * @param context
     *            Context
     * @return SD Card Path Collection
     */
     Public Static List<string> getexternalmemorypaths (Context context) {
        null;
        StorageManager StorageManager = (StorageManager) context
. Getsystemservice (Context.storage_service);
        Try {
            Class<?>[] paramclasses = {};
                Method Getvolumepathsmethod = StorageManager. class. GetMethod (
"getvolumepaths", paramclasses); //Returns a list of all installed volume paths
Getvolumepathsmethod.setaccessible (true);
Object[] params = {};
Object invoke = Getvolumepathsmethod.invoke (StorageManager, params);
New Arraylist<string> ();
 for (int i = 0; I < (string[]) invoke). length; i++) {
    String Path = ((string[)) invoke) [i];
    List.add (path);
}
Catch (IllegalArgumentException e) {
    E.printstacktrace ();
Catch (Nosuchmethodexception e) {
    E.printstacktrace ();
Catch (Illegalaccessexception e) {
    E.printstacktrace ();
Catch (InvocationTargetException e) {
    E.printstacktrace ();
}
    return list;
    }
}

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.