我想格式化SD卡,我正在使用以下代码。但是SD卡无法安装。
public boolean formatSD() {
boolean bool = false;
IMountService mountService = null;
try{
IBinder ibi = ServiceManager.getService("mount");
if(ibi!=null) {
mountService = IMountService.Stub.asInterface(ibi);
String extern_sd = Environment.getFlashStroageDirectory().getAbsolutePath();
mountService.unmountVolume(extern_sd, true);
Thread.sleep(4000);
int result_format = mountService.formatVolume(extern_sd);
System.out.println("result_format: "+result_format);
if(result_format==0){
Thread.sleep(4000);
int result_mount = mountService.mountVolume(extern_sd);
System.out.println("result_mount: "+ result_mount);
}
bool = true;
}
}catch(Exception e){
System.out.println("Exception format: "+e.getMessage());
}
return bool;
}
有人可以告诉我我做错了什么吗?谢谢
答案 0 :(得分:0)
试试这个
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();
}
}