在 Android 中格式化 SD 卡
事情应该很简单,但在大多数情况下,在 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.IMountService
和 android.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,我认为无论您采用哪种方法,在格式化 SD 卡之前您可能需要卸载
.android_secure
文件系统。然后,
尝试在您的应用中包含以下权限:
1)
MOUNT_FORMAT_FILESYSTEMS
- http://developer.android.com/reference/android/Manifest.permission.html#MOUNT_FORMAT_FILESYSTEMS2)
MOUNT_UNMOUNT_FILESYSTEMS
- http://developer.android. com/reference/android/Manifest.permission.html#MOUNT_UNMOUNT_FILESYSTEMSAndroid 设置应用已使用第二个权限。
=================================================== ==============================
当您执行 AOSP 或任何其他分发代码的构建时,会自动生成 IMountService.java 文件。我猜它包含以下函数,它实际上将格式化命令发送到 vold 守护进程:
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_FILESYSTEMS2)
MOUNT_UNMOUNT_FILESYSTEMS
- http://developer.android.com/reference/android/Manifest.permission.html#MOUNT_UNMOUNT_FILESYSTEMSAndroid 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.:
我无法再次在这里找到这个问题,但它有一个可行的解决方案。所以所有的功劳都归于那个人;)
I can't find again the question here on SO, but It had a working solution. So all credit goes to that guy ;)