在 Android 中格式化 SD 卡

发布于 2024-12-04 21:33:17 字数 1110 浏览 5 评论 0原文

事情应该很简单,但在大多数情况下,在 Android 中却并非如此。如果用户在我的应用程序中选择该选项,我需要格式化 SD 卡。不要问我为什么我需要这样做,如果它已经在操作系统中......不切实际,但这是我需要实现的要求。您可能知道,设置\存储\擦除 SD 卡中有一个选项。我看了一下 froyo 源代码,它是这样的:

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

它使用 android.os.storage.IMountServiceandroid.os.ServiceManager 并且我似乎没有可以访问它。因此,正如我所见,我可以递归搜索每个文件并将其删除,但这“不符合我的口味”……或者我可以从“擦除 SD 卡”开始向用户显示屏幕。

任何帮助都非常受欢迎,因为我被困住了。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷了相思 2024-12-11 21:33:17

首先,我认为无论您采用哪种方法,在格式化 SD 卡之前您可能需要卸载 .android_secure 文件系统。

然后,

尝试在您的应用中包含以下权限:

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 设置应用已使用第二个权限。

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

当您执行 AOSP 或任何其他分发代码的构建时,会自动生成 IMountService.java 文件。我猜它包含以下函数,它实际上将格式化命令发送到 vold 守护进程:

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

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;
  }
}
夏末的微笑 2024-12-11 21:33:17

我无法再次在这里找到这个问题,但它有一个可行的解决方案。所以所有的功劳都归于那个人;)

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

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();
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文