get external sdcard status using java reflection

http://blog.csdn.net/roserose0002/article/details/8080495

http://www.cyqdata.com/android/article-detail-36281

http://www.oschina.net/question/157093_108899

http://www.eoeandroid.com/thread-183532-1-1.html (not verified)


1.获取对象:

 mStorageManager = (StorageManager)mActivity 
                    .getSystemService(Activity.STORAGE_SERVICE); 


2.获取方法:

method = Class.forName("android.os.storage.StorageManager").getMethod("getVolumePaths");

或者...


3.调用方法:

paths = (String[]) method.invoke(mStorageManager);



package com.android.Wecfct.sdcard;

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; 
    private Method mMethodGetStatus; 
    String[] paths;
     
    public StorageList(Activity activity) { 
        mActivity = activity; 
        if (mActivity != null) { 
            mStorageManager = (StorageManager)mActivity 
                    .getSystemService(Activity.STORAGE_SERVICE); 
            try { 
               mMethodGetPaths = mStorageManager.getClass() 
                        .getMethod("getVolumePaths"); 
                mMethodGetStatus = mStorageManager.getClass() 
                        .getMethod("getVolumeState",String.class); 
            } catch (NoSuchMethodException e) { 
                e.printStackTrace(); 
            } 
        } 
    } 
     
    public String[] getVolumePaths() { 
       // String[] paths = null; 
        try { 
            paths = (String[]) mMethodGetPaths.invoke(mStorageManager); 
            System.out.println("reflect patha:"+paths[0]);
            System.out.println("reflect pathb:"+paths[1]);
        } catch (IllegalArgumentException e) { 
            e.printStackTrace(); 
        } catch (IllegalAccessException e) { 
            e.printStackTrace(); 
        } catch (InvocationTargetException e) { 
            e.printStackTrace(); 
        } 
        return paths; 
    } 
    
    public String getVolumeState(String mountPoint) {
    	String status = null; 
         
    	 try
			{
				String tt = "/mnt/external_sdcard";
				status = (String) (mMethodGetStatus.invoke(mStorageManager,"/mnt/external_sdcard"));
/*				System.out.println(tt);
				System.out.println(paths[1]);*/
				//status = (String) (mMethodGetStatus.invoke(mStorageManager,paths[1]));
			} catch (IllegalArgumentException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
    	 
             System.out.println("reflect status:"+status);
   
              return status;
   }
    
    
} 



或者

			String[] paths = (String[]) sm.getClass().getMethod("getVolumePaths", null).invoke(sm, null);
			MOBILE_MEMORY = paths[0];
			EXTERNAL_MEMORY = paths[1];
			String status = (String) sm.getClass().getMethod("getVolumeState", String.class).invoke(sm, MOBILE_MEMORY);
			String status = (String) sm.getClass().getMethod("getVolumeState", String.class).invoke(sm, EXTERNAL_MEMORY);


ps:

transfer "/mnt/external_sdcard" to getVolumeStatus(), not "/mnt/external_sdcard/"



 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值