Android Studio - Store or Copy a File direct to ROOT of SD Card or Phone
How can i copy a file from Application Directory to ROOT DIRECTORY of my SD CARD or Phone? With the Code i found on the inte.net i only can copy or create in following directory: "/storage/emulated/0/Android/data/com.example.filme3/files" But i want to store in "/storage/emulated/0" Or on my external sd card. "storage/9C33-6BBD"
Context context = getApplicationContext();
File path = context.getExternalFilesDir(null);
copyFile(new File(path, "filme1_alle.txt"), new File( path,"testfile.txt"));
public static boolean copyFile(File source, File dest){
try{
// Declaration et ouverture des flux
java.io.FileInputStream sourceFile = new java.io.FileInputStream(source);
try{
java.io.FileOutputStream destinationFile = null;
try{
destinationFile = new FileOutputStream(dest);
// Lecture par segment de 0.5Mo
byte buffer[] = new byte[512 * 1024];
int nbLecture;
while ((nbLecture = sourceFile.read(buffer)) != -1){
destinationFile.write(buffer, 0, nbLecture);
}
} finally {
destinationFile.close();
}
} finally {
sourceFile.close();
}
} catch (IOException e){
e.printStackTrace();
return false; // Erreur
}
return true; // Rsultat OK
}
Removable micro sd cards are read only since Android KitKat.
Only with Storage Access Framework you can write to them.
On Adroid 11+ root of external storage is not writable. Use the public directories then.
Have a look at MANAGE_EXTERNAL_STORAGE permission.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.