当前位置:操作系统 > 安卓/Android >>

android上通过反射,获取存储器列表

  各种android设备的存储器路径,是不一样的,比如T卡路径,可能是/mnt/sdcard、/mnt/extsd、/mnt/external_sd或者/mnt/sdcard2,有时内置存储器的路径也可能是/mnt/sdcard,而host usb存储器的路径也是各种各样的。
     因此,想要运行在各种android设备上的应用,就必须在运行期间动态获取各个可用的存储器路径,避免一个存储器空间不足就不能运行的问题。
     下面方法是通过反射,调用StorageManager的隐藏接口getVolumePaths(),实现获取存储器列表。
[java]
package ckl.storage.list; 
 
import java.lang.reflect.InvocationTargetException; 
import java.lang.reflect.Method; 
 
import android.app.Activity; 
import android.os.storage.StorageManager; 
 
public class StorageList { 
    private Activity mActivity; 
    private StorageManager mStorageManager; 
    private Method mMethodGetPaths; 
     
    public StorageList(Activity activity) { 
        mActivity = activity; 
        if (mActivity != null) { 
            mStorageManager = (StorageManager)mActivity 
                    .getSystemService(Activity.STORAGE_SERVICE); 
            try { 
                mMethodGetPaths = mStorageManager.getClass() 
                        .getMethod("getVolumePaths"); 
            } catch (NoSuchMethodException e) { 
                e.printStackTrace(); 
            } 
        } 
    } 
     
    public String[] getVolumePaths() { 
        String[] paths = null; 
        try { 
            paths = (String[]) mMethodGetPaths.invoke(mStorageManager); 
        } catch (IllegalArgumentException e) { 
            e.printStackTrace(); 
        } catch (IllegalAccessException e) { 
            e.printStackTrace(); 
        } catch (InvocationTargetException e) { 
            e.printStackTrace(); 
        } 
        return paths; 
    } 

另外,附上StorageManager.java
[java] 
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 
 
package android.os.storage; 
 
import android.os.Handler; 
import android.os.Looper; 
import android.os.Message; 
import android.os.Parcelable; 
import android.os.RemoteException; 
import android.os.ServiceManager; 
import android.util.Log; 
import android.util.SparseArray; 
 
import java.lang.ref.WeakReference; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.concurrent.atomic.AtomicInteger; 
 
/**
 * StorageManager is the inte易做图ce to the systems storage service. The storage
 * manager handles storage-related items such as Opaque Binary Blobs (OBBs).
 * <p>
 * OBBs contain a filesystem that maybe be encrypted on disk and mounted
 * on-demand from an application. OBBs are a good way of providing large amounts
 * of binary assets without packaging them into APKs as they may be multiple
 * gigabytes in size. However, due to their size, they're most likely stored in
 * a shared storage pool accessible from all programs. The system does not
 * guarantee the security of the OBB file itself: if any program modifies the
 * OBB, there is no guarantee that a read from that OBB will produce the
 * expected output.
 * <p>
 * Get an instance of this class by calling
 * {@link android.content.Context#getSystemService(java.lang.String)} with an
 * argument of {@link android.content.Context#STORAGE_SERVICE}.
 */ 
 
public class StorageManager 

    private static final String TAG = "StorageManager"; 
 
    /*
     * Our internal MountService binder reference
     */ 
    private IMountService mMountService; 
 
    /*
     * The looper target for callbacks
     */ 
    Looper mTgtLooper; 
 
    /*
     * Target listener for binder callbacks
     */ 
    private MountServiceBinderListener mBinderListener; 
 
    /*
     * List of our listeners
     */ 
    private List<ListenerDelegate> mListeners = new ArrayList<ListenerDelegate>(); 
 
    /*
     * Next available nonce
     */ 
    final private AtomicInteger mNextNonce = new AtomicInteger(0); 
 
    private class MountServiceBinderListener extends IMountServiceListener.Stub { 
        public void onUsbMassStorageConnectionChanged(boolean available) { 
            final int size = mListeners.size(); 
            for (int i = 0; i < size; i++) { 
        &nbs

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,