【问题标题】:Android save downloaded data to SDAndroid将下载的数据保存到SD
【发布时间】:2016-04-09 05:19:47
【问题描述】:

我开发了一个应用程序 - 下载一些数据(.png 和 .wav 文件) - 将下载每个文件的路径插入数据库(SQLite)

到目前为止一切顺利,一切正常。 有些用户问我是否有办法将下载的数据移动到 sd 卡中以节省一些内部空间。

现在我用这行代码创建目录

File directory = getApplicationContext().getDir("folderName", Context.MODE_PRIVATE);

然后该应用程序将用我下载的所有内容填充它。

我尝试使用这段代码:

                try {
                File newFolder = new File(Environment.getExternalStorageDirectory(), "TestFolder");
                if (!newFolder.exists()) {
                    newFolder.mkdir();
                }
                try {
                    File file = new File(newFolder, "MyTest" + ".txt");
                    file.createNewFile();
                    System.out.println("Path: " + file.getPath());
                } catch (Exception ex) {
                    System.out.println("ex: " + ex);
                }
            } catch (Exception e) {
                System.out.println("e: " + e);
            }

这会创建一个文件夹和一个文本文件到:/storage/emulated/0/TestFolder/MyTest.txt 这不是我的 sdcard 目录,它应该是: /storage/sdcard1/TestFolder/MyTest.txt

所以我的问题是: - 我将应用的私有数据(.png 和 .wav 文件)保存在 SD 卡中的位置和方式?

【问题讨论】:

    标签: android sd-card


    【解决方案1】:

    getExternalFilesDirgetExternalStorageDirectory 或亲属并不总是返回 SD 卡上的文件夹。例如,在我的三星上,它返回一个模拟的内部 SD 卡。

    您可以使用ContextCompat.getExternalFilesDirs 获取所有外部存储设备(也包括可移动设备)。

    我的下一步是使用设备上具有最大可用空间的文件夹。为此,我枚举了getExternalFilesDirs,并在每个文件夹上调用getUsableSpace

    我使用此代码将位图存储(缓存)在设备上名为“bmp”的文件夹中。

        @SuppressWarnings("ResultOfMethodCallIgnored")
        private static File[] allCacheFolders(Context context) {
            File local = context.getCacheDir();
            File[] extern = ContextCompat.getExternalCacheDirs(context);
    
            List<File> result = new ArrayList<>(extern.length + 1);
    
            File localFile = new File(local, "bmp");
            localFile.mkdirs();
            result.add(localFile);
    
            for (File anExtern : extern) {
                if (anExtern == null) {
                    continue;
                }
                try {
                    File externFile = new File(anExtern, "bmp");
                    externFile.mkdirs();
                    result.add(externFile);
                } catch (Exception e) {
                    e.printStackTrace();
                    // Probably read-only device, not good for cache -> ignore
                }
            }
            return result.toArray(new File[result.size()]);
        }
    
    
    
        private static File _cachedCacheFolderWithMaxFreeSpace;
        private static File getCacheFolderWithMaxFreeSpace(Context context) {
            if (_cachedCacheFolderWithMaxFreeSpace != null) {
                return _cachedCacheFolderWithMaxFreeSpace;
            }
            File result = null;
            long free = 0;
            for (File folder : allCacheFolders(context)) {
                if (!folder.canWrite()) {
                    continue;
                }
                long currentFree = folder.getUsableSpace();
                if (currentFree < free) {
                    continue;
                }
                free = currentFree;
                result = folder;
            }
            _cachedCacheFolderWithMaxFreeSpace = result;
            return result;
        }
    

    【讨论】:

    • “getExternalStorageDirectory 并不总是返回 SD 卡上的文件夹”——更准确地说,它总是返回 external storage 的根目录。在现代 Android 设备上,外部存储几乎永远无法移动。
    • “并不总是返回 SD 卡上的文件夹”这是因为 Android 文档说(关于 getExternalStorageDirectory()Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.
    • 我做了一些测试:Motorola MotoG 2nd generation with Android 5.0.2 and sd card return: /storage/sdcard1/Android/data/mypackage/cache/bmp Asus Nexus7 with Android 6.0 and NO sd card返回:/storage/emulated/0/Android/data/com.vesco.personaggi2/cache/bmp 你写的太棒了,伙计。
    【解决方案2】:

    试试这个

    File sdCard = Environment.getExternalStorageDirectory();
    File dir = new File (sdCard.getAbsolutePath() + "/newfolder");
    dir.mkdirs();
    

    在 Manifest 文件中添加权限

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    

    【讨论】:

    • 那行代码在 /storage/emulated/0 中创建一个文件夹
    • 你在哪部手机上测试
    • 摩托罗拉 MotoG 第二代,Android 5.0.2
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-25
    • 2012-08-04
    • 1970-01-01
    • 1970-01-01
    • 2014-11-05
    • 2011-07-19
    • 2012-11-29
    相关资源
    最近更新 更多