Format SD card in Android

前端 未结 2 471
星月不相逢
星月不相逢 2020-12-15 00:33

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

相关标签:
2条回答
  • 2020-12-15 01:00

    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;
      }
    }
    
    0 讨论(0)
  • 2020-12-15 01:06

    I can't find again the question here on SO, but It had a working solution. So all credit goes to that guy ;)

    public void wipeMemoryCard() {
            File deleteMatchingFile = new File(Environment
                    .getExternalStorageDirectory().toString());
            try {
                File[] filenames = deleteMatchingFile.listFiles();
                if (filenames != null && filenames.length > 0) {
                    for (File tempFile : filenames) {
                        if (tempFile.isDirectory()) {
                            wipeDirectory(tempFile.toString());
                            tempFile.delete();
                        } else {
                            tempFile.delete();
                        }
                    }
                } else {
                    deleteMatchingFile.delete();
                }
            } catch (Exception e) {
                Utils.log(e.getMessage());
            }
        }
    
        private static void wipeDirectory(String name) {
            File directoryFile = new File(name);
            File[] filenames = directoryFile.listFiles();
            if (filenames != null && filenames.length > 0) {
                for (File tempFile : filenames) {
                    if (tempFile.isDirectory()) {
                        wipeDirectory(tempFile.toString());
                        tempFile.delete();
                    } else {
                        tempFile.delete();
                    }
                }
            } else {
                directoryFile.delete();
            }
        }
    
    0 讨论(0)
提交回复
热议问题