Reputation: 138
I've developed an app which - download some data ( .png and .wav files ) - insert the path where each files is downloaded into a database (SQLite)
So far so good, everything works. Some users asked me if there was a way to move the the downloaded data in the sd card in order to save some internal space.
By now i create the directory with this line of code
File directory = getApplicationContext().getDir("folderName", Context.MODE_PRIVATE);
Then the app will fill it with all the stuff I downloaded.
I tried using this piece of code:
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);
}
And this create a folder and a text file into: /storage/emulated/0/TestFolder/MyTest.txt Which is not my sdcard directory, it should be: /storage/sdcard1/TestFolder/MyTest.txt
So my question is: - where and how I saved my app's private data (.png and .wav files) in the SD card?
Upvotes: 1
Views: 143
Reputation: 52518
The getExternalFilesDir
, getExternalStorageDirectory
or relatives, does not always return a folder on a SD card. On my Samsung for example, it returns an emulated, internal SD card.
You can get all external storage devices (also the removable) using ContextCompat.getExternalFilesDirs
.
My next step, is to use the folder on the device with the largest free space. To get that, I enumerate the getExternalFilesDirs
, and call getUsableSpace
on every folder.
I use this code to store (cache) bitmaps in a folder named "bmp" on the device.
@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;
}
Upvotes: 2
Reputation: 3763
try this
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/newfolder");
dir.mkdirs();
add permission in Manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Upvotes: 0