[英]Format SD card in Android
事情應該很簡單,但在大多數情況下,在Android中,不是。 如果用戶在我的應用程序中選擇該選項,我需要格式化SD卡。 如果它已經在操作系統中,請不要問我為什么需要這樣做...不實用但是我需要實現它。 您可能知道, Settings \\ Storage \\ Erase SD Card中有一個選項。 我看了一下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
,我似乎無法訪問它。 所以,正如我所看到的那樣,我可以遞歸搜索每個文件並將其刪除,但這將“不符合我的口味”......或者我可以從Erase SD卡啟動屏幕給用戶。
任何幫助都是受歡迎的,因為我被困住了。
首先,我認為您可能需要在格式化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;
}
}
我在這里找不到關於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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.