-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ANDROAPP-5857] image download implementation (#3498)
* download image files to external directory Signed-off-by: Pablo <[email protected]> * download qr/barcode files to external directory Signed-off-by: Pablo <[email protected]> * show message when file is downloaded Signed-off-by: Pablo <[email protected]> --------- Signed-off-by: Pablo <[email protected]>
- Loading branch information
Showing
6 changed files
with
131 additions
and
33 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
commons/src/main/java/org/dhis2/commons/bindings/Permissions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package org.dhis2.commons.bindings | ||
|
||
import android.content.Context | ||
import android.content.pm.PackageManager | ||
import androidx.core.content.ContextCompat | ||
|
||
fun Context.hasPermissions(permissions: Array<String>): Boolean { | ||
return permissions.all { permission -> | ||
ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
commons/src/main/java/org/dhis2/commons/data/FileHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package org.dhis2.commons.data | ||
|
||
import android.graphics.Bitmap | ||
import android.os.Environment | ||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.MutableLiveData | ||
import java.io.File | ||
import java.io.FileOutputStream | ||
|
||
class FileHandler { | ||
|
||
private val destinationResult = MutableLiveData<File>() | ||
|
||
fun saveBitmapAndOpen( | ||
bitmap: Bitmap, | ||
outputFileName: String, | ||
fileCallback: (LiveData<File>) -> Unit, | ||
) { | ||
fileCallback(destinationResult) | ||
|
||
val imagesFolder = getDownloadDirectory(outputFileName) | ||
destinationResult.value = saveBitmapAndOpen(bitmap, imagesFolder) | ||
} | ||
|
||
fun copyAndOpen( | ||
sourceFile: File, | ||
fileCallback: (LiveData<File>) -> Unit, | ||
) { | ||
fileCallback(destinationResult) | ||
|
||
val imagesFolder = getDownloadDirectory(sourceFile.name) | ||
destinationResult.value = copyFile(sourceFile, imagesFolder) | ||
} | ||
|
||
private fun saveBitmapAndOpen(bitmap: Bitmap, destinationFolder: File): File { | ||
val os = FileOutputStream(destinationFolder) | ||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, os) | ||
os.close() | ||
return destinationFolder | ||
} | ||
|
||
private fun copyFile(sourceFile: File, destinationDirectory: File): File { | ||
return sourceFile.copyTo(destinationDirectory, true) | ||
} | ||
|
||
private fun getDownloadDirectory(outputFileName: String) = File( | ||
Environment.getExternalStoragePublicDirectory( | ||
Environment.DIRECTORY_DOWNLOADS, | ||
), | ||
"dhis2" + File.separator + outputFileName, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters