Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format SD card in Android

Things should be simple, but as most of the time, in Android, aren't. I need to format the SD card if the user selects the option in my app. Don't ask me why I need to do this if it's already in the OS... not practical but it's a requirement that I need to implement. As you may know, there is an option in Settings \ Storage \ Erase SD Card. I took a look at the froyo source code and it's something like:

final IMountService service =
         IMountService.Stub.asInterface(ServiceManager.getService("mount"));
        if (service != null) {
            new Thread() {
                public void run() {
                try {
                        service.formatVolume(Environment.getExternalStorageDirectory().toString());
                    } catch (Exception e) {
                        // Intentionally blank - there's nothing we can do here
                    Log.w("MediaFormat", "Unable to invoke IMountService.formatMedia()");
                    }
                }
            }.start();
        } else {
            Log.w("MediaFormat", "Unable to locate IMountService");
        }

It uses android.os.storage.IMountService and android.os.ServiceManager and I don't seem to have access to it. So, as I see it I could recursively search every file and delete it but that would be "not on my taste"... or I could start the screen from Erase SD card to the user.

Any help is more then welcome, as I am stuck.

like image 295
Alin Avatar asked Sep 13 '11 16:09

Alin


People also ask

Why can't I format my SD card on my Android?

Why does the “Android unable to format SD card” error happen? Whenever you get the Android unable to format SD card error, it often happens because the SD card is already formatted with a file system it doesn't recognize, or the SD card can no longer perform write operations.

What format does Android format SD cards to?

What Format Does SD Card for Android Need? Android phones support the file systems including FAT32, ext3, ext4, and exFAT, but the last file system is not supported by some older Android phones.

Can you format an SD card FAT32 on an Android phone?

If you are formatting your SD for your Android phone or Nintendo DS or 3DS, you will have to format to FAT32. With Android, many of your apps or custom recoveries, if you are rooted, will not read exFAT.

Which devices have the option to format the SD card?

As already mentioned most devices with SD cards including phones, computers, and cameras have a formatting option. 1. Format SD Card on Android Phone Different Android versions have slightly different steps on how you go about formatting.

How to format Android memory card?

Proceed and select SD Card. Next, tap Format. Finally, in the pop-up message, select Format SD Card. Wait a few minutes for the process to terminate then begin using the card. 2. Format Android Memory Card on Computer There are two options to format an SD memory card on a computer.

How to format SD card in LG phones?

Choose Yes, and the device will start formatting the memory card. 3. Format SD Card in Mobile LG Phone Step 1: From the home screen, go to Apps > Settings > Storage. Step 2: Choose the SD/Memory Card option. Step 3: Tap on the (Menu) three dots on the upper right corner of the screen. It is the Menu. Step 4: Choose Settings. Step 5: Select Format.

How to format an SD card in Samsung Galaxy S10?

Below, we show you an example of how to format an SD card in Samsung on Android OS version 10. On your phone, navigate to Settings > Device Care. Next, select Storage. Tap on Advanced. Here, you will see Portable storage. Proceed and select SD Card. Next, tap Format.


1 Answers

First of all, I think that you may need to umount .android_secure filesystem before formatting SD card, whatever your approach may be.

Then,

Try including following permissions in your app:

1) MOUNT_FORMAT_FILESYSTEMS - http://developer.android.com/reference/android/Manifest.permission.html#MOUNT_FORMAT_FILESYSTEMS

2) MOUNT_UNMOUNT_FILESYSTEMS - http://developer.android.com/reference/android/Manifest.permission.html#MOUNT_UNMOUNT_FILESYSTEMS

Android Settings app already uses the 2nd permission.

================================================================================

When you perform a build of AOSP or any other distribution code, IMountService.java file gets generated automatically. It contains following function which actually sends formatting commands to vold daemon I guess.:

private static class Proxy implements android.os.storage.IMountService
{
  private android.os.IBinder mRemote;
  Proxy(android.os.IBinder remote)
  {
    mRemote = remote;
  }

  public android.os.IBinder asBinder()
  {
    return mRemote;
  }

  // **** A LOT OF OTHER CODE IS HERE.....

  public int formatVolume(java.lang.String mountPoint) throws android.os.RemoteException
  {
    android.os.Parcel _data = android.os.Parcel.obtain();
    android.os.Parcel _reply = android.os.Parcel.obtain();
    int _result;
    try {
      _data.writeInterfaceToken(DESCRIPTOR);
      _data.writeString(mountPoint);
      mRemote.transact(Stub.TRANSACTION_formatVolume, _data, _reply, 0);
      _reply.readException();
      _result = _reply.readInt();
    }
    finally {
      _reply.recycle();
      _data.recycle();
    }
    return _result;
  }
}
like image 184
Abhijeet Pathak Avatar answered Oct 05 '22 05:10

Abhijeet Pathak