AndroidManifest.xml添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
申请外部存储读写权限
private void requestStoragePermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "存储权限已被允许");
} else {
Log.d(TAG, "申请存储权限");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, GET_STORAGE_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == GET_STORAGE_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "存储权限已被授予");
} else {
Log.d(TAG, "存储权限被拒绝");
}
}
}
编码
public void copyVideoToGallery(Context context, String sourceFilePath) {
File sourceFile = new File(sourceFilePath);
if (!sourceFile.exists()) {
Log.d(TAG, "!sourceFile.exists():" + !sourceFile.exists());
return;
}
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File storageDir = new File(path,appName);
if (!storageDir.exists()) {
storageDir.mkdirs();
}
File destinationFile = new File(storageDir, sourceFile.getName());
try {
FileChannel sourceChannel = new FileInputStream(sourceFile).getChannel();
FileChannel destinationChannel = new FileOutputStream(destinationFile).getChannel();
destinationChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
sourceChannel.close();
destinationChannel.close();
addVideoToGallery(context, destinationFile.getAbsolutePath());
Log.d(TAG, "video copied to gallery:" + destinationFile.getAbsolutePath());
} catch (IOException e) {
Log.d(TAG, "copyVideoToGallery error:" + e);
}
}
private void addVideoToGallery(Context context, String videoFilePath) {
MediaScannerConnection.scanFile(context, new String[]{videoFilePath}, new String[]{"video/mp4"},
(path, uri) -> Log.d(TAG, "Video scanned and added to gallery:" + path));
}
工具类
package com.yimturm.camera.utils;
import android.content.Context;
import android.media.MediaScannerConnection;
import android.os.Build;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
public class VideoSave {
private static final String TAG = "VideoSave";
private static final String appName = "cameraFile";
public static void copyVideoToGallery(Context context, String sourceFilePath) {
File sourceFile = new File(sourceFilePath);
if (!sourceFile.exists()) {
Log.d(TAG, "!sourceFile.exists():" + !sourceFile.exists());
return;
}
File destinationFile;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
File appSpecificDir = context.getExternalFilesDir(null);
File storageDir = new File(appSpecificDir, appName);
if (!storageDir.exists()) {
storageDir.mkdirs();
}
destinationFile = new File(storageDir, sourceFile.getName());
} else {
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
File storageDir = new File(path, appName);
if (!storageDir.exists()) {
storageDir.mkdirs();
}
destinationFile = new File(storageDir, sourceFile.getName());
}
try {
FileChannel sourceChannel = new FileInputStream(sourceFile).getChannel();
FileChannel destinationChannel = new FileOutputStream(destinationFile).getChannel();
destinationChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
sourceChannel.close();
destinationChannel.close();
addVideoToGallery(context, destinationFile.getAbsolutePath());
} catch (IOException e) {
Log.d(TAG, "copyVideoToGallery error:" + e);
}
}
private static void addVideoToGallery(Context context, String videoFilePath) {
MediaScannerConnection.scanFile(context, new String[]{videoFilePath}, new String[]{"video/mp4"},
(path, uri) -> Log.d(TAG, "Video scanned and added to gallery:" + path));
}
}
使用例子:
VideoSave.copyVideoToGallery(Constant.instance, "/data/data/com.yimturm.camera/files/IOTDCC-509687-PPVKX_1700019964255.mp4");
或者
File internalFilesDir = new File(getFilesDir(Constant.instance));
String path = internalFilesDir.getAbsolutePath() + File.separator + did + "_" + System.currentTimeMillis() + ".prv";
VideoSave.copyVideoToGallery(Constant.instance,path);