2016-01-05 115 views
1

我已经开发了一个应用程序,它 - 下载一些数据(巴纽和.wav文件) - 插入其中每个文件下载到一个数据库(SQLite的)路径的Android下载数据保存到SD

所以非常好,一切正常。 有些用户问我是否有方法将下载的数据移动到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 这是不是我的SD卡目录,它应该是: /storage/sdcard1/TestFolder/MyTest.txt

所以我的问题是: - 在那里,我如何在SD卡中保存我的应用程序的私有数据(png格式和.wav文件)?

回答

2

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; 
    } 
+1

“的getExternalStorageDirectory并不总是一个SD返回一个文件夹卡“ - 更准确地说,它总是返回[external storage]的根目录(https://commonsware.com/blog/2014/04/08/storage-situation-external-storage.html)。外部存储在现代Android设备上几乎不可移动。 – CommonsWare

+0

“并不总是返回SD卡上的文件夹”,这是因为Android文档说(关于['getExternalStorageDirectory()'](http://developer.android.com/intl/ko/reference/android/os /Environment.html#getExternalStorageDirectory%28%29))'传统上这是一张SD卡,但它也可以作为内置存储设备来实现,该设备不同于受保护的内部存储设备,并且可以作为文件系统安装一个电脑.' – Sufian

+0

我做了一些测试: 摩托罗拉MotoG第二代Android 5.0.2和SD卡返回:/ 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 你写的东西真是太棒了,男人。 – Vesco

0

试试这个

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

添加权限清单文件

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

这行代码创建/存储的文件夹/模拟/ 0 – Vesco

+0

上的电话,你测试 – Gaurav

+0

摩托罗拉MotoG第二代采用Android 5.0.2 – Vesco