2016-01-05 30 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