Dhaval2404 / ImagePicker

📸Image Picker for Android, Pick an image from Gallery or Capture a new image with Camera
Apache License 2.0
1.52k stars 341 forks source link

Support saveDir without using compression, crop, or camera #243

Open VincentJoshuaET opened 3 years ago

VincentJoshuaET commented 3 years ago

If I call saveDir(DIRECTORY) with galleryOnly() AND without crop*() OR compress(), we should be able to choose if we want to copy the selected image into the directory, so that we can get a File URI instead of a Content URI.

build3r commented 2 years ago

Meanwhile we can create the cache file manually

fun getCachedImage(uri: Uri): String {
            val compressedFile = File(externalCacheDir, "picked/IMG_${SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())}.png")
            Timber.d("compressedFile: ${compressedFile.path}")
            val originalScaleOptions = BitmapFactory.Options()
            val stream = this.contentResolver.openInputStream(uri)
            val oBitmap = BitmapFactory.decodeStream(stream, null, originalScaleOptions)
            val byteArrayOutputStream = ByteArrayOutputStream()
            oBitmap?.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream)
            val fileOutputStream = FileOutputStream(compressedFile)
            fileOutputStream.write(byteArrayOutputStream.toByteArray())
            fileOutputStream.flush()
            fileOutputStream.close()
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                val oExif = ExifInterface(stream!!)
                val nExif = ExifInterface(compressedFile)
                val ogOr = oExif.getAttribute(ExifInterface.TAG_ORIENTATION)
                nExif.setAttribute(ExifInterface.TAG_ORIENTATION, ogOr)
                nExif.setAttribute(ExifInterface.TAG_EXIF_VERSION, oExif.getAttribute(ExifInterface.TAG_EXIF_VERSION))
                nExif.saveAttributes()
            }
            return compressedFile.path
    }