看別人的帖子,最後寫出來,原帖沒提供必要的幾個文件,我這裏找到了給大家找全了。
1.在你的工程src目錄下創建Android.os.storage包,導入我上傳的幾個源文件;從這裏下載:http://download.csdn.net/detail/wangxinxxx/8269731
2.導入以下代碼,調用mount()、unMount()就行了
- package cn.steven.timeswitch;
- import android.os.IBinder;
- import android.os.RemoteException;
- import android.os.storage.*;
- import android.util.Log;
- import java.lang.reflect.Method;
- /**
- * Created by momobile on 2014-12-17.
- */
- public class Utils {
- static IMountService iMountService;
- static {
- Log.v("DWXD", android.os.Environment.getExternalStorageDirectory().toString());
- try {
- Method method = Class.forName("android.os.ServiceManager").getMethod("getService", String.class);
- IBinder binder = (IBinder) method.invoke(null, "mount");
- iMountService = IMountService.Stub.asInterface(binder);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public Utils() {
- }
- static void unMount() {
- try {
- iMountService.unmountVolume(android.os.Environment.getExternalStorageDirectory().toString(), true, true);
- } catch (RemoteException e) {
- e.printStackTrace();
- }
- }
- static void mount(){
- try {
- iMountService.mountVolume(android.os.Environment.getExternalStorageDirectory().toString());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
出處:
http://blog.csdn.net/a231930/article/details/7587880
http://blog.csdn.net/dahuaishu2010_/article/details/14127445