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
,我似乎无法访问它。所以,正如我所看到的那样,我可以递归搜索每个文件并将其删除,但这将“不符合我的口味”......或者我可以从Erase SD卡启动屏幕到用户。
任何帮助都会受到欢迎,因为我被困住了。
答案 0 :(得分:4)
首先,我认为您可能需要在格式化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;
}
}
答案 1 :(得分:4)
我在SO上找不到这个问题,但它有一个有效的解决方案。所以所有的功劳归于那个人;)
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();
}
}