Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- override fun onRequestPermissionsResult(
- requestCode: Int,
- permissions: Array<String>,
- grantResults: IntArray
- ) {
- when (requestCode) {
- PermissionsRequestCode -> {
- val isPermissionsGranted = managePermissions
- .processPermissionsResult(grantResults)
- if (isPermissionsGranted) {
- // Do the task now
- Toast.makeText(context, "Permissions granted.", Toast.LENGTH_LONG).show()
- } else {
- Toast.makeText(context, "Permissions denied.", Toast.LENGTH_LONG).show()
- }
- return
- }
- }
- }
- override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
- super.onActivityResult(requestCode, resultCode, data)
- pickImageStatus = 1
- when (requestCode) {
- RequestCode.REQUEST_TAKE_PICTURE_CAMERA -> {
- if (resultCode == Activity.RESULT_OK) {
- imageViewUploadFragmentIV.visibility = View.GONE
- uploadImageTV.visibility = View.GONE
- file = Utils.INSTANCE.file
- filePath = Utils.INSTANCE.filePath!!
- mCurrentPhotoPath = Utils.INSTANCE.mCurrentPhotoPath
- setPic()
- }
- }
- RequestCode.REQUEST_TAKE_PICTURE_GALERY -> {
- if (resultCode == Activity.RESULT_OK) {
- imageViewUploadFragmentIV.visibility = View.GONE
- uploadImageTV.visibility = View.GONE
- if (data != null) {
- file = Utils.INSTANCE.getBitmapFile(data, this)
- filePath = data.data!!
- setPicFromGalery()
- }
- }
- }
- }
- }
- private fun setPic() {
- // Get the dimensions of the View
- val targetW: Int = addInfoguruImageFragmentIV.width
- val targetH: Int = addInfoguruImageFragmentIV.height
- val bmOptions = BitmapFactory.Options().apply {
- // Get the dimensions of the bitmap
- inJustDecodeBounds = true
- BitmapFactory.decodeFile(mCurrentPhotoPath, this)
- val photoW: Int = outWidth
- val photoH: Int = outHeight
- // Determine how much to scale down the image
- val scaleFactor: Int = Math.min(photoW / targetW, photoH / targetH)
- // Decode the image file into a Bitmap sized to fill the View
- inJustDecodeBounds = false
- inSampleSize = scaleFactor
- inPurgeable = true
- }
- BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions)?.also { bitmap ->
- addInfoguruImageFragmentIV.setImageBitmap(bitmap)
- galleryAddPic(bitmap)
- }
- }
- private fun setPicFromGalery() {
- var parcelFD: ParcelFileDescriptor? = null
- try {
- parcelFD = context?.contentResolver?.openFileDescriptor(filePath, "r")
- val imageSource = parcelFD?.fileDescriptor
- // Decode image size
- val o = BitmapFactory.Options()
- o.inJustDecodeBounds = true
- BitmapFactory.decodeFileDescriptor(imageSource, null, o)
- // the new size we want to scale to
- val requiredSize = 1024
- // Find the correct scale value. It should be the power of 2.
- var widthTmp = o.outWidth
- var heightTmp = o.outHeight
- var scale = 1
- while (true) {
- if (widthTmp < requiredSize && heightTmp < requiredSize) {
- break
- }
- widthTmp /= 2
- heightTmp /= 2
- scale *= 2
- }
- // decode with inSampleSize
- val o2 = BitmapFactory.Options()
- o2.inSampleSize = scale
- val bitmap = BitmapFactory.decodeFileDescriptor(imageSource, null, o2)
- addInfoguruImageFragmentIV.setImageBitmap(bitmap)
- galleryAddPic(bitmap)
- } catch (e: FileNotFoundException) {
- // handle errors
- } catch (e: IOException) {
- // handle errors
- } finally {
- if (parcelFD != null)
- try {
- parcelFD.close()
- } catch (e: IOException) {
- // ignored
- }
- }
- }
- private val FILE_MAX_SIZE = 350 * 1024
- private var COMPRESS_QUALITY = 99
- @SuppressLint("LongLogTag", "LogNotTimber")
- private fun galleryAddPic(bitmap: Bitmap) {
- try {
- var bmpStream = ByteArrayOutputStream()
- bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bmpStream)
- var bmpPicByteArray = bmpStream.toByteArray()
- var streamLength = bmpPicByteArray.size
- if (streamLength > FILE_MAX_SIZE) {
- while (streamLength > FILE_MAX_SIZE) {
- bmpStream = ByteArrayOutputStream()
- bitmap.compress(Bitmap.CompressFormat.JPEG, COMPRESS_QUALITY, bmpStream)
- bmpPicByteArray = bmpStream.toByteArray()
- streamLength = bmpPicByteArray.size
- COMPRESS_QUALITY -= 5
- if (COMPRESS_QUALITY == 10) {
- break
- }
- }
- if (file!!.exists()) {
- val fOut = FileOutputStream(file)
- bitmap.compress(Bitmap.CompressFormat.JPEG, COMPRESS_QUALITY, fOut)
- fOut.flush()
- fOut.close()
- }
- } else {
- if (file!!.exists()) {
- val fOut = FileOutputStream(file)
- bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut)
- fOut.flush()
- fOut.close()
- }
- }
- } catch (e: FileNotFoundException) {
- Log.e(TAG, "galleryAddPic: error file = ${e.message}")
- } catch (e: IOException) {
- Log.e(TAG, "galleryAddPic: error IO = ${e.message}")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement