How unmount programmatically USB DRIVE in Android 4.2

一世执手 提交于 2019-12-08 13:31:33

问题


I have read many posts on this topic, but I have not found a solution yet. In my application I need to unmount the USB DRIVE after copying files from USB DRIVE to the tablet, so I can safely remove it without using the Settings menu.

Right now I am using this method:

Utility.copyDirectory(file,new File(_CURR_FOLDER));
Process su;
su = Runtime.getRuntime().exec("/system/bin/su");
String cmd = "umount" +
             " " + SDPath + "\n" + "exit\n";
             su.getOutputStream().write(cmd.getBytes());

What I get in the storage settings is:

Total space 0.0 Available 0.0

but SD is still mounted.

Thank you in advance for your help.

First Edit:

Someone know how use IMountService? I read about it, and maybe is the right way to solve the unmount of USB DRIVE, but after adding classes-full-debug.jar my project is no longer compiled


回答1:


this cannot be done. Many users store all their content like songs, videos, photos there. it was a good security decision to not allow apps to unmount SD card, but use them to store data.

but u can send user to setting and user do it , not by code

Intent i = new Intent(android.provider.Settings.ACTION_MEMORY_CARD_SETTINGS);
startActivity(i);

which brings up that screen...

i think this will work




回答2:


This is what i had to do on Android 6.0.1, hopefully it will be the same for 4.2.

I had to add the library SDK/out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes-full-debug.jar to my app after compiling android. (I just downloaded the lollipop version from here and thankfully it worked for marshmallow as well)

Android studio will complain about an out of memory error, this is fixed by adding the below code to the build.gradle inside the android section.

dexOptions {
    javaMaxHeapSize "4g"
}

You also need to add multidex support by adding the below lines in build.gradle inside the defaultConfig section.

multiDexEnabled true

And adding the below line inside the dependencies section.

compile 'com.android.support:multidex:1.0.0'

And adding the below line in the application section inside the manifest.

android:name="android.support.multidex.MultiDexApplication"

And your apk has to be in the /system/priv-app directory and have the below permission.

<uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE" />

and then the below code should work.

private void unmount() {
    IMountService mountService = getMountService();
    try {
        if (mountService != null) {
            mountService.unmountVolume("/mnt/media_rw/8842-89A4", true, false);
            Log.e(TAG,"Unmounted");
        } else {
            Log.e(TAG, "Mount service is null, can't unmount");
        }
    } catch (RemoteException ex) {
        // Not much can be done
    }
}

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;
}


来源:https://stackoverflow.com/questions/24783344/how-unmount-programmatically-usb-drive-in-android-4-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!