【Introduction】
In MSDK v5, the pullOriginalMediaFileFromCamera will pull the original data from the photos. If you want to download a complete photo into a specified location of your Android device, you need to set the input parameter offset as 0 and write the media data from the MediaFileDownloadListener callback to the byteStreamBuffer.
【Demo code】
fun downloadFile(){
//Define the path of the saved photo, create it if it does not exist. Be careful that the authorization of the shared storage is changed from Android 11.
val dirs: File = File(DiskUtil.getExternalCacheDirPath(ContextUtil.getContext(), mediaFileDir))
if (!dirs.exists()) {
dirs.mkdirs()
}
//Define the name of the saved photo, override it if it does exist. Be careful that the authorization of the shared storage is changed from Android 11
val filepath = DiskUtil.getExternalCacheDirPath(ContextUtil.getContext(), mediaFileDir + "/" + mediaFile?.fileName)
val file: File = File(filepath)
if (file.exists()) {
file.delete()
}
//Open up the byte output stream.
val outputStream = FileOutputStream(file)
val bos = BufferedOutputStream(outputStream)
var beginTime = System.currentTimeMillis()
mediaFile?.pullOriginalMediaFileFromCamera(0 , object :MediaFileDownloadListener {
override fun onStart() {
showProgress()
}
override fun onProgress(total: Long, current: Long) {
updateProgress(current , total)
}
//Write the data when the callback has returned each time.
override fun onRealtimeDataUpdate(data: ByteArray, position: Long) {
try {
bos.write(data, 0, data.size)
bos.flush()
} catch (e: IOException) {
LogUtils.e("MediaFile" , "write error" + e.message)
}
}
//The downlaod process is completed, close the byte output stream.
override fun onFinish() {
var spendTime = (System.currentTimeMillis() - beginTime)
var speedBytePerMill : Float? = mediaFile?.fileSize?.div(spendTime.toFloat())
var divs = 1000.div(1024 * 1024.toFloat());
var speedKbPerSecond : Float?= speedBytePerMill?.times(divs)
ToastUtils.showToast(getString(R.string.msg_download_compelete_tips) + "${speedKbPerSecond }Mbps"
+ getString(R.string.msg_download_save_tips) + "${filepath}" )
hideProgress()
try {
outputStream.close()
bos.close()
} catch (error: IOException) {
LogUtils.e("MediaFile" , "close error$error" )
}
}
override fun onFailure(error: IDJIError?) {
LogUtils.e("MediaFile" , "download error$error" )
}
})
}
Comments
3 comments
Hello, I'm using the v5 version of the msdk and I can't find the button that calls the downloadFile() function. My goal is to be able to retrieve the images directly on the remote and find them in its local files, each time the drone takes photos, but I can't figure out when should I call this method
Thank you William for this sample code. But it's not clear from this where to get mediaFile in the first place. I can get a GeneratedMediaFileInfo object by listening on KeyNewlyGeneratedMediaFile .. But it's not clear how to turn this into a MediaFile object. Appreciate any guidance you can provide.
How I can download files of a high res grid photo folder? Media Type returned "PHOTO_FOLDER" but "subMediaFile" returns empty list
Please sign in to leave a comment.