`
Leif_冬
  • 浏览: 46675 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

android 6.0後讀取外部SD卡的路徑的方法

阅读更多

android 中獲取內部SD卡存儲路徑是有標準API的如下:

String internalSDPath = Environment.getExternalStorageDirectory().getAbsolutePath();

 

外部SD卡路徑的獲取方法如下:

先遍歷掛載的外部設備
public ArrayList<String> getExternalStorageList() {
        ArrayList storageList = new ArrayList();

        try {
            StorageManager mStorageManager = (StorageManager)getSystemService("storage");

            try {
                Method mMethodGetPaths = mStorageManager.getClass().getMethod("getVolumeList");
                Object[] list = (Object[])((Object[])mMethodGetPaths.invoke(mStorageManager));
                if (list != null && list.length > 0) {
                    Object[] arr$ = list;
                    int len$ = list.length;

                    for(int i$ = 0; i$ < len$; ++i$) {
                        Object item = arr$[i$];
                        StorageVolumeReflection storageVolume = new StorageVolumeReflection(this, item);
                        Log.i("TEST", "storageVolume = " + storageVolume);
                        if (!storageVolume.mPrimary && storageVolume.mRemovable && storageVolume.mState.equals("mounted")) {
                            storageList.add(storageVolume.mPath);
                        }
                    }
                }
            } catch (Exception var10) {
                var10.printStackTrace();
            }
        } catch (Exception var11) {
            var11.printStackTrace();
        }

        return storageList;
    }


外部SD卡的路徑:

ArrayList<String> sdcardPath;
sdcardPath = getExternalStorageList();
Log.i("TEST", "------------>sdcardPath size: " + sdcardPath.size());
if(sdcardPath.size()>0) {
      Log.i("TEST", "------------>sdcardPath: " + sdcardPath.get(0));
}

 創建StorageVolumeReflection 類:

public class StorageVolumeReflection {
        public int mStorageId;
        public String mPath;
        public String mDescription;
        public boolean mPrimary;
        public boolean mRemovable;
        public boolean mEmulated;
        public int mMtpReserveSpace;
        public boolean mAllowMassStorage;
        public long mMaxFileSize;
        public String mState;

        public StorageVolumeReflection(Context context, Object reflectItem) {
            Method fState;
            try {
                fState = reflectItem.getClass().getDeclaredMethod("getStorageId");
                fState.setAccessible(true);
                this.mStorageId = (Integer)fState.invoke(reflectItem);
            } catch (Exception var16) {
                ;
            }

            try {
                fState = reflectItem.getClass().getDeclaredMethod("getPath");
                fState.setAccessible(true);
                this.mPath = (String)fState.invoke(reflectItem);
            } catch (Exception var15) {
                ;
            }

            try {
                fState = reflectItem.getClass().getDeclaredMethod("getDescription");
                fState.setAccessible(true);
                this.mDescription = (String)fState.invoke(reflectItem);
            } catch (Exception var14) {
                ;
            }

            if (this.mDescription == null || TextUtils.isEmpty(this.mDescription)) {
                try {
                    fState = reflectItem.getClass().getDeclaredMethod("getDescription");
                    fState.setAccessible(true);
                    this.mDescription = (String)fState.invoke(reflectItem, context);
                } catch (Exception var13) {
                    ;
                }
            }

            if (this.mDescription == null || TextUtils.isEmpty(this.mDescription)) {
                try {
                    fState = reflectItem.getClass().getDeclaredMethod("getDescriptionId");
                    fState.setAccessible(true);
                    int mDescriptionId = (Integer)fState.invoke(reflectItem);
                    if (mDescriptionId != 0) {
                        this.mDescription = context.getResources().getString(mDescriptionId);
                    }
                } catch (Exception var12) {
                    ;
                }
            }

            try {
                fState = reflectItem.getClass().getDeclaredMethod("isPrimary");
                fState.setAccessible(true);
                this.mPrimary = (Boolean)fState.invoke(reflectItem);
            } catch (Exception var11) {
                ;
            }

            try {
                fState = reflectItem.getClass().getDeclaredMethod("isRemovable");
                fState.setAccessible(true);
                this.mRemovable = (Boolean)fState.invoke(reflectItem);
            } catch (Exception var10) {
                ;
            }

            try {
                fState = reflectItem.getClass().getDeclaredMethod("isEmulated");
                fState.setAccessible(true);
                this.mEmulated = (Boolean)fState.invoke(reflectItem);
            } catch (Exception var9) {
                ;
            }

            try {
                fState = reflectItem.getClass().getDeclaredMethod("getMtpReserveSpace");
                fState.setAccessible(true);
                this.mMtpReserveSpace = (Integer)fState.invoke(reflectItem);
            } catch (Exception var8) {
                ;
            }

            try {
                fState = reflectItem.getClass().getDeclaredMethod("allowMassStorage");
                fState.setAccessible(true);
                this.mAllowMassStorage = (Boolean)fState.invoke(reflectItem);
            } catch (Exception var7) {
                ;
            }

            try {
                fState = reflectItem.getClass().getDeclaredMethod("getMaxFileSize");
                fState.setAccessible(true);
                this.mMaxFileSize = (Long)fState.invoke(reflectItem);
            } catch (Exception var6) {
                ;
            }

            try {
                fState = reflectItem.getClass().getDeclaredMethod("getState");
                fState.setAccessible(true);
                this.mState = (String)fState.invoke(reflectItem);
            } catch (Exception var5) {
                ;
            }

        }

        public String getVolumeState(Context context) {
            return "";
        }

        public boolean isMounted(Context context) {
            return this.getVolumeState(context).equals("mounted");
        }

        public String getDescription() {
            return this.mDescription;
        }

        public String getUniqueFlag() {
            return "" + this.mStorageId;
        }

        public long getAvailableSize() {
            return 0L;
        }

        public long getTotalSize() {
            return 0L;
        }

        public String toString() {
            return "MyStorageVolume{\nmStorageId=" + this.mStorageId + "\n, mPath='" + this.mPath + '\'' + "\n, mDescription=" + this.mDescription + "\n, mPrimary=" + this.mPrimary + "\n, mRemovable=" + this.mRemovable + "\n, mEmulated=" + this.mEmulated + "\n, mMtpReserveSpace=" + this.mMtpReserveSpace + "\n, mAllowMassStorage=" + this.mAllowMassStorage + "\n, mMaxFileSize=" + this.mMaxFileSize + "\n, mState='" + this.mState + '\'' + '}' + "\n";
        }
    }

 

分享到:
评论

相关推荐

    android 读取外置和内置存储卡路径和大小

    通过上述方法,你可以在Android应用中轻松读取内外部存储卡的路径和大小。在实际项目中,可以根据需求进行适当的封装和异常处理,以提供更稳定、健壮的存储管理功能。记得在使用这些代码之前,先测试它们在不同设备...

    Unity应用获取外置SD卡路径方法

    在Android 6.0(API级别23)及更高版本中,系统对存储访问进行了权限管理,因此获取外置SD卡路径前,需要确保在`AndroidManifest.xml`中添加了相应的权限: ```xml &lt;uses-permission android:name="android....

    Android6.0 storage目录sd卡存储的路径创建详解

    总之,Android 6.0在处理SD卡存储路径时采取了一种独特的挂载策略,通过`init.rc`文件的配置将`/mnt/runtime/default`与`/storage`关联,解决了路径创建的问题。这种设计虽然不常见,但对于理解Android系统如何管理...

    Android6.0的SDK的API接口

    10. **存储访问框架增强**:SDK扩展了对外部存储的访问,包括对SD卡的直接访问。 要使用Android 6.0的SDK,开发者需要将其解压,并将`android-23`目录放置到SDK Manager指定的路径下。之后,重启Eclipse或使用...

    android 读取和存储sd卡

    在Android系统中,读取和存储SD卡是应用程序与外部存储交互的重要部分,尤其是在处理大量数据或者需要离线可用内容时。本示例项目“StorageManageDemo”将帮助开发者了解如何在Android应用中有效地实现这一功能。...

    Android私有储存文件和外部储存文件读写(适配6.0以上权限,不考虑SD卡)

    读写外部存储文件的方法与私有存储类似,只是路径不同。 最后,关于`WRFile`,这可能是你项目中的一个自定义文件操作类,用于封装上述的读写操作,具体实现要看源代码。如果你需要在项目中统一处理读写操作,创建...

    安卓SD卡文件管理更新信息相关-android读取外置和内置存储卡路径和大小.rar

    在Android系统中,管理和操作文件,尤其是涉及到SD卡(外置和内置存储卡)的文件操作,是开发者经常面临的一项任务。这篇资料主要探讨的是如何在Android应用中获取SD卡的路径以及计算其存储大小。以下是一些关键知识...

    SD卡文件操作,兼容Android6.0+

    在Android系统中,SD卡(Secure Digital card)是设备扩展存储空间的重要部分,尤其是在设备内存有限的情况下。Android 6.0(Marshmallow)引入了一项名为"运行时权限"的重大改变,这使得对SD卡的文件操作变得更为...

    android读取sd卡中MP3文件

    读取外部存储(如SD卡)需要`READ_EXTERNAL_STORAGE`权限。在AndroidManifest.xml中添加权限声明,并在运行时请求用户授权。 ```xml &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt;...

    Android app s讀取外部SD卡範例

    这个例子专注于使用Delphi这一集成开发环境(IDE)来构建一个能读取外部SD卡的Android应用。Delphi,以其强大的对象 Pascal 语言为基础,为开发者提供了一个高效且功能丰富的工具集,用于创建跨平台的应用程序,包括...

    获取SD卡 TF卡 内置 外置存储卡路径例子

    在提供的压缩包文件"获取存储卡路径"中,可能包含了实现上述优化策略的代码示例,如自定义函数来获取不同类型的存储卡路径,或者实现了上述优化技术的文件读取类。通过研究这些代码,开发者可以更好地理解如何在实际...

    Android TV USB路径或手机SDCARD路径获取

    在Android系统中,无论是电视设备还是手机,都有可能需要访问外部存储介质,如USB设备或手机的SD卡,以便扩展其存储容量或者进行数据交换。本文将深入探讨如何在Android TV和手机上获取SDCARD(内部或外部)以及USB...

    android 读取SD卡图片

    首先,我们需要了解Android对外部存储(如SD卡)的访问机制。在Android 6.0(API级别23)之前,应用默认可以读写外部存储。但为了增强用户隐私保护,从那时起,应用需要获取`READ_EXTERNAL_STORAGE`或`WRITE_...

    android 从sd卡读取数据

    在AndroidManifest.xml文件中,你需要添加读取外部存储的权限,否则应用无法访问SD卡上的数据: ```xml &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt; ``` 对于Android 6.0 (API...

    android 简单的读取SD卡的所有图片

    首先,我们需要明确一点:自Android 6.0(API级别23)开始,系统引入了运行时权限管理,读取外部存储的权限不再是默认授予的,而是需要在运行时向用户请求。因此,确保在Manifest.xml中添加`&lt;uses-permission ...

    Android 读取SD卡中文件以及内存使用情况

    在Android平台上,与外部存储(通常称为SD卡)交互是应用程序开发中的常见任务。这篇博客“Android 读取SD卡中文件以及内存使用情况”详细介绍了如何在Android应用中读取SD卡上的文件以及监控内存使用状态。这篇文章...

    android 内部存储读写 sd卡存储

    本篇文章将主要探讨Android中的内部存储与外部存储之一——SD卡存储的基本概念、特点及其使用方法。 #### 二、Android内部存储 Android内部存储指的是应用自身的私有存储空间,通常位于`/data/data/&lt;package name&gt;...

    android 读取外置SDCard

    从API level 17开始,Android提供了`Environment.getExternalStorageDirectory()`方法,这个方法返回的是外部公共存储的根目录,通常对应于内部存储或SDCard。如果你需要区分内外置SDCard,可以使用`...

    Android 读取U盘或SD卡中的所有.txt文件

    本篇文章将深入探讨如何在Android应用中读取U盘或SD卡(外部存储)上的所有`.txt`文件。首先,我们需要了解Android对文件访问的权限管理和存储路径。 1. **权限管理**: 自Android 6.0(API级别23)起,系统引入了...

    android 读取和存储sd卡.zip

    在Android系统中,读取和存储SD卡数据是应用程序与外部存储交互的重要部分。随着Android系统的不断更新,对外部存储的访问策略也有所变化,因此理解如何正确地操作SD卡对于开发Android应用至关重要。本教程将详细...

Global site tag (gtag.js) - Google Analytics