在 Android 中格式化 SD 卡

Format SD card in Android(在 Android 中格式化 SD 卡)
本文介绍了在 Android 中格式化 SD 卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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

它使用 android.os.storage.IMountServiceandroid.os.ServiceManager 我似乎无法访问它.因此,正如我所见,我可以递归地搜索每个文件并将其删除,但这不合我的口味"......或者我可以从擦除 SD 卡开始屏幕给用户.

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.

推荐答案

我在这里找不到关于 SO 的问题,但它有一个可行的解决方案.所以所有的功劳都归于那个人;)

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

这篇关于在 Android 中格式化 SD 卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How can I use CClistview in COCOS2d Android?(如何在 COCOS2d Android 中使用 CClistview?)
cocos2d-android: how to display score(cocos2d-android:如何显示分数)
Sqlite database not copied from asset folder Android(Sqlite 数据库未从资产文件夹 Android 复制)
SQLite Database Copy Appears Corrupted When Generated by Device and not Emulator(SQLite 数据库副本在由设备而不是模拟器生成时出现损坏)
Android file copy(安卓文件拷贝)
Android how to detect Copy event of Edittext in android(Android如何在android中检测Edittext的Copy事件)