Created
April 17, 2013 04:27
-
-
Save myamamic/5401795 to your computer and use it in GitHub Desktop.
[android] アプリからマウント、アンマウントする
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* mydroid/packages/apps/などの下でビルドする | |
| * | |
| */ | |
| import android.os.IBinder; | |
| import android.os.RemoteException; | |
| import android.os.ServiceManager; // SDKでは公開されていない | |
| import android.os.storage.IMountService; // SDKでは公開されていない | |
| private static final String MOUNT_POINT = "/mnt/ext_usb" or "/mnt/sdcard/" ... | |
| private IMountService mMountService = null; | |
| private synchronized IMountService getMountService() { | |
| if (mMountService == null) { | |
| IBinder service = ServiceManager.getService("mount"); | |
| if (service != null) { | |
| mMountService = IMountService.Stub.asInterface(service); | |
| } else { | |
| Log.e(TAG, "Can't get mount service"); | |
| } | |
| } | |
| return mMountService; | |
| } | |
| private void mount() { | |
| IMountService mountService = getMountService(); | |
| try { | |
| if (mountService != null) { | |
| mountService.mountVolume(MOUNT_POINT); | |
| } else { | |
| // | |
| } | |
| } catch (RemoteException ex) { | |
| // Not much can be done | |
| } | |
| } | |
| private void unmount() { | |
| StorageManager sm = (StorageManager) getSystemService(Context.STORAGE_SERVICE); | |
| String state = sm.getVolumeState(MOUNT_POINT); | |
| if (!Environment.MEDIA_MOUNTED.equals(state) && | |
| !Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { | |
| // | |
| return; | |
| } | |
| IMountService mountService = getMountService(); | |
| try { | |
| if (mountService != null) { | |
| mountService.unmountVolume(MOUNT_POINT, true, false); | |
| } else { | |
| Log.e(TAG, "Mount service is null, can't unmount"); | |
| } | |
| } catch (RemoteException ex) { | |
| // Not much can be done | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment