Skip to content

Commit

Permalink
Save large video fix
Browse files Browse the repository at this point in the history
There is a fix for saving large videos.
Now we allocate a buffer with the size of 8 mb that we use for reading and writing files.
Also, now we are deleting video file from cache after save is completed.

CWF-377
CWF-360
  • Loading branch information
lecicdjuro authored and ailic88 committed Sep 6, 2019
1 parent ec04308 commit 0b0bd66
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 51 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.0.2

* Saving large video files - fix

## 1.0.1

* Changed description
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ internal object FileUtils {

private const val TAG = "FileUtils"
private const val SCALE_FACTOR = 50.0

private const val BUFFER_SIZE = 1024 * 1024 * 8
private const val DEGREES_90 = 90
private const val DEGREES_180 = 180
private const val DEGREES_270 = 270
private const val EOF = -1

/**
* Inserts image into external storage
Expand Down Expand Up @@ -84,55 +85,6 @@ internal object FileUtils {
return true
}

/**
* @param contentResolver - content resolver
* @param path - path to temp file that needs to be stored
* @return true if video was saved successfully
*/
fun insertVideo(contentResolver: ContentResolver, path: String): Boolean {

val file = File(path)
val mimeType = MimeTypeMap.getFileExtensionFromUrl(file.toString())
val source = getBytesFromFile(file)

val values = ContentValues()
values.put(MediaStore.Images.Media.TITLE, file.name)
values.put(MediaStore.Images.Media.DISPLAY_NAME, file.name)
values.put(MediaStore.Images.Media.MIME_TYPE, mimeType)
// Add the date meta data to ensure the image is added at the front of the gallery
values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis())
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis())

var url: Uri? = null

try {
url = contentResolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values)

if (source != null) {
var videoOutStream: OutputStream? = null
if (url != null) {
videoOutStream = contentResolver.openOutputStream(url)
}

videoOutStream?.use {
videoOutStream.write(source)
}
} else {
if (url != null) {
contentResolver.delete(url, null, null)
}
url = null
}
} catch (e: Exception) {
if (url != null) {
contentResolver.delete(url, null, null)
}
return false
}

return true
}

/**
* @param source - array of bytes that will be rotated if it needs to be done
* @param path - path to image that needs to be checked for rotation
Expand Down Expand Up @@ -249,4 +201,54 @@ internal object FileUtils {

return bytes
}

/**
* @param contentResolver - content resolver
* @param path - path to temp file that needs to be stored
* @return true if video was saved successfully
*/
fun insertVideo(contentResolver: ContentResolver, inputPath: String, bufferSize: Int = BUFFER_SIZE): Boolean {

val inputFile = File(inputPath)
val inputStream: InputStream?
val outputStream: OutputStream?

val mimeType = MimeTypeMap.getFileExtensionFromUrl(inputFile.toString())

val values = ContentValues()
values.put(MediaStore.Images.Media.TITLE, inputFile.name)
values.put(MediaStore.Images.Media.DISPLAY_NAME, inputFile.name)
values.put(MediaStore.Images.Media.MIME_TYPE, mimeType)
// Add the date meta data to ensure the image is added at the front of the gallery
values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis())
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis())

val url: Uri?

try {
url = contentResolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values)
inputStream = FileInputStream(inputFile)
if (url != null) {
outputStream = contentResolver.openOutputStream(url)
val buffer = ByteArray(bufferSize)
inputStream.use {
outputStream?.use {
while (inputStream.read(buffer) != EOF) {
outputStream.write(buffer)
}
}
}
}
// delete the temp video file
inputFile.delete()

} catch (fnfE: FileNotFoundException) {
Log.e("GallerySaver", fnfE.message)
return false
} catch (e: Exception) {
Log.e("GallerySaver", e.message)
return false
}
return true
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: gallery_saver
description: Saves images and videos from network or temporary file to external storage. Both images and videos will be visible in Android Gallery and iOS Photos.
version: 1.0.1
version: 1.0.2
author: Carnegie Technologies d.o.o <[email protected]>
homepage: https://github.com/CarnegieTechnologies/gallery_saver

Expand Down

0 comments on commit 0b0bd66

Please sign in to comment.