如何将媒体(视频)从应用程序沙盒存储复制到DCIM目录



我正在从应用程序沙盒存储中的服务器下载视频,路径如下:

final String filePath = this.getExternalFilesDir("videos") + "/" + name + ".mp4";

现在,我想将上面路径中的一些特定文件复制到DCIM中的另一个文件夹中,这样用户就可以在库中发现视频。

我可以创建那个文件,但我不知道如何复制和移动文件。

File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "MyFolder");
if (!dir.exists()) {
boolean rv = dir.mkdir();
Log.d(TAG, "Folder creation " + ( rv ? "success" : "failed"));
}

有人能帮忙吗?

使用标准Java IO流解决了这个问题。

String inputFile = "/" + name + ".mp4";
String inputPath = this.getExternalFilesDir("videos") + "";
String outputPath = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "MyFolder") + "";
private void copyFile(String inputFile, String inputPath, String outputPath) {
try {
File dir = new File(outputPath);
if (!dir.exists()) {
if (!dir.mkdirs()) {
return;
}
}
try (InputStream inputStream = new FileInputStream(inputPath + inputFile)) {
try (OutputStream outputStream = new FileOutputStream(outputPath + inputFile)) {
File source = new File(inputPath + inputFile);
byte[] buffer = new byte[1024];
int read;
long length = source.length();
long total = 0;
while ((read = inputStream.read(buffer)) != -1) {
total += read;
int progress = (int) ((total * 100) / length);
if (progress == 100) {
Toast.makeText(VideoActivity.this, "Completed", Toast.LENGTH_SHORT).show();
}
outputStream.write(buffer, 0, read);
}
}
}
} catch (Exception e) {
FirebaseCrashlytics.getInstance().recordException(e);
}
}

最新更新