![JAR search and dependency download from the Maven repository](/logo.png)
haerzig.core.utils.FileUtil.kt Maven / Gradle / Ivy
package haerzig.core.utils
import android.content.ContentResolver
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.os.Environment
import android.webkit.MimeTypeMap
import androidx.core.content.FileProvider
import com.shockwave.pdfium.PdfiumCore
import java.io.File
class FileUtil {
companion object {
val TAG = FileUtil::class.java.simpleName
fun getExtension(context: Context, uri: Uri): String {
var extension = ""
//Check uri format to avoid null
if (uri.scheme != null && uri.scheme == ContentResolver.SCHEME_CONTENT) {
//If scheme is a content
val mime = MimeTypeMap.getSingleton()
extension = mime.getExtensionFromMimeType(context.contentResolver.getType(uri))!!
} else {
//If scheme is a File
//This will replace white spaces with %20 and also other special characters. This will avoid returning null values on file name with spaces and special characters.
if (uri.path != null) {
extension = MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(File(uri.path!!)).toString())
}
}
return extension
}
fun openFile(context: Context, path: Uri) {
var type = getMimeTypeFromExtension(getExtension(context, path))
val intent = Intent(Intent.ACTION_VIEW)
if (type == null || type.isEmpty()) {
type = "*/*"
}
intent.setDataAndType(path, type)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
context.startActivity(intent)
}
fun getMimeTypeFromExtension(extension: String?): String? {
return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension)
}
fun setExtension(path: String, extension: String, context: Context, folderName: String): Uri {
var pathString = path.substring(0, path.lastIndexOf("."))
pathString = "$pathString.$extension"
val file = File(pathString)
if (isImage(extension)) {
return FileProvider.getUriForFile(context, context.packageName + ".provider", file)
}
val directoryDocuments = File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DOCUMENTS)
val directory = File(directoryDocuments, folderName)
// Create the storage directory if it does not exist
if (!directory.exists() && !directory.mkdirs()) {
MyLogger.logDebugMessage(TAG, "failed to create directory")
}
val uuid = file.name
return FileProvider.getUriForFile(context, context.packageName + ".provider", File(directory.path, uuid))
}
fun isImageFile(context: Context, path: Uri): Boolean {
return isImage(getExtension(context, path))
}
fun isPdfFile(context: Context, path: Uri): Boolean {
return isPdf(getExtension(context, path))
}
fun isPdf(extension: String): Boolean {
val mimeType = getMimeTypeFromExtension(extension)
return mimeType != null && mimeType.contains("pdf")
}
fun isImage(extension: String): Boolean {
val mimeType = getMimeTypeFromExtension(extension)
return mimeType != null && mimeType.startsWith("image")
}
fun generateImageFromPdf(pdfUri: Uri, context: Context): Bitmap? {
val pageNumber = 0
val pdfiumCore = PdfiumCore(context)
try {
val fd = context.contentResolver.openFileDescriptor(pdfUri, "r")
val pdfDocument = pdfiumCore.newDocument(fd)
pdfiumCore.openPage(pdfDocument, pageNumber)
val width = pdfiumCore.getPageWidthPoint(pdfDocument, pageNumber)
val height = pdfiumCore.getPageHeightPoint(pdfDocument, pageNumber)
val bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
pdfiumCore.renderPageBitmap(pdfDocument, bmp, pageNumber, 0, 0, width, height)
pdfiumCore.closeDocument(pdfDocument) // important!
return bmp
} catch (e: Exception) {
MyLogger.logDebugMessage(TAG, e.localizedMessage)
}
return null
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy