在 Android 中格式化 SD 卡

时间:2023-03-20
本文介绍了在 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 卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:在 iOS 上更改日期格式 下一篇:无法在 Android 中使用 ExifInterface 设置 Date Taken/DateTime 标签

相关文章