Skip to content

Commit

Permalink
Added extension function to copy all files to SD card.
Browse files Browse the repository at this point in the history
  • Loading branch information
shichen85 committed Aug 28, 2024
1 parent 6bb350b commit 9b80451
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@
import java.io.OutputStream;
import java.io.IOException;

import android.os.Environment;
import java.io.File;

import java.nio.file.StandardCopyOption.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.stream.Stream;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;



public class Olympus extends RunnerSocial {
public static Activity activity = null;
public static Intent launchIntent = null;
Expand Down Expand Up @@ -71,4 +83,54 @@ public void _olympus_android_write_custom_output(String data) {
}
}
}

private void copyFolder(Path src, Path dest) throws IOException {
try (Stream<Path> stream = Files.walk(src)) {
stream.forEach(source -> copy(source, dest.resolve(src.relativize(source))));
}
}

private void copy(Path source, Path dest) {
try {
Files.copy(source, dest, REPLACE_EXISTING);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(), e);
}
}

// Function to copy all files from internal storage to SD card
public void _olympus_android_copy_files_to_SD_card() {
File sourceDir = activity.getFilesDir(); // Get internal files directory
Log.i("yoyo", "Source directory: " + sourceDir);
String externalStorageState = Environment.getExternalStorageState();
if (!externalStorageState.equals(Environment.MEDIA_MOUNTED)) {
Log.e("yoyo", "External storage not mounted");
return;
}
else{
Log.i("yoyo", "External storage state: " + externalStorageState);
}

File[] externalStorageVolumes = activity.getExternalFilesDirs(null);
File primaryExternalStorage = externalStorageVolumes[0];
File secondaryExternalStorage = externalStorageVolumes[1];
Log.i("yoyo", "Primary external storage: " + primaryExternalStorage);
Log.i("yoyo", "Secondary external storage: " + secondaryExternalStorage);


File targetDir = new File(secondaryExternalStorage, "MyAppFiles"); // Destination on SD card
Log.i("yoyo", "Target directory: " + targetDir);
if (!targetDir.exists()) {
if (!targetDir.mkdirs()) {
Log.i("yoyo", "Failed to create target directory");
return;
}
}

try {
copyFolder(sourceDir.toPath(), targetDir.toPath());
} catch (IOException e) {
Log.e("yoyo", "Error copying files", e);
}
}
}
1 change: 1 addition & 0 deletions Ganary/extensions/_olympus_extension/_olympus_extension.yy

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9b80451

Please sign in to comment.