Skip to content

Commit

Permalink
fix: implement save file to gallery for android >= 29
Browse files Browse the repository at this point in the history
  • Loading branch information
maitrungduc1410 committed Dec 4, 2023
1 parent 0a5fd64 commit dc886c4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
26 changes: 12 additions & 14 deletions android/src/main/java/com/videotrim/utils/StorageUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,15 +222,8 @@ private static void saveVideoUsingMediaStore(Context context, File videoFile) {
if (uri != null) {
try {
OutputStream outputStream = context.getContentResolver().openOutputStream(uri);
if (outputStream != null) {
// Copy the video file to the output stream
// Here, you can use the method you have to copy the file contents
// For example, you can use FileInputStream to read from videoFile and write to outputStream

outputStream.close();
// Notify the media scanner that a new video has been added to the gallery
MediaScannerConnection.scanFile(context, new String[]{videoFile.getAbsolutePath()}, new String[]{"video/*"}, null);
}
copyFile(videoFile, outputStream);
MediaScannerConnection.scanFile(context, new String[]{uri.toString()}, new String[]{"video/*"}, null);
} catch (IOException e) {
e.printStackTrace();
}
Expand All @@ -245,14 +238,19 @@ private static void saveVideoUsingTraditionalStorage(Context context, File video
MediaScannerConnection.scanFile(context, new String[]{destinationFile.getAbsolutePath()}, new String[]{"video/*"}, null);
}

private static void copyFile(File sourceFile, OutputStream outputStream) throws IOException {
InputStream inputStream = new FileInputStream(sourceFile);
copyFile(inputStream, outputStream);
}

private static void copyFile(File sourceFile, File destFile) throws IOException {
InputStream inputStream = null;
OutputStream outputStream = null;
InputStream inputStream = new FileInputStream(sourceFile);
OutputStream outputStream = new FileOutputStream(destFile);
copyFile(inputStream, outputStream);
}

private static void copyFile(InputStream inputStream, OutputStream outputStream) throws IOException {
try {
inputStream = new FileInputStream(sourceFile);
outputStream = new FileOutputStream(destFile);

byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-video-trim",
"version": "1.0.7",
"version": "1.0.8",
"description": "Video trimmer for your React Native app",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down

0 comments on commit dc886c4

Please sign in to comment.