Advertisement
Mujiburrohman

function setPic & compress

Jul 30th, 2020
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. override fun onRequestPermissionsResult(
  2. requestCode: Int,
  3. permissions: Array<String>,
  4. grantResults: IntArray
  5. ) {
  6. when (requestCode) {
  7. PermissionsRequestCode -> {
  8. val isPermissionsGranted = managePermissions
  9. .processPermissionsResult(grantResults)
  10. if (isPermissionsGranted) {
  11. // Do the task now
  12. Toast.makeText(context, "Permissions granted.", Toast.LENGTH_LONG).show()
  13. } else {
  14. Toast.makeText(context, "Permissions denied.", Toast.LENGTH_LONG).show()
  15. }
  16. return
  17. }
  18. }
  19.  
  20. }
  21.  
  22. override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
  23. super.onActivityResult(requestCode, resultCode, data)
  24. pickImageStatus = 1
  25. when (requestCode) {
  26. RequestCode.REQUEST_TAKE_PICTURE_CAMERA -> {
  27. if (resultCode == Activity.RESULT_OK) {
  28. imageViewUploadFragmentIV.visibility = View.GONE
  29. uploadImageTV.visibility = View.GONE
  30. file = Utils.INSTANCE.file
  31. filePath = Utils.INSTANCE.filePath!!
  32. mCurrentPhotoPath = Utils.INSTANCE.mCurrentPhotoPath
  33. setPic()
  34. }
  35. }
  36. RequestCode.REQUEST_TAKE_PICTURE_GALERY -> {
  37. if (resultCode == Activity.RESULT_OK) {
  38. imageViewUploadFragmentIV.visibility = View.GONE
  39. uploadImageTV.visibility = View.GONE
  40. if (data != null) {
  41. file = Utils.INSTANCE.getBitmapFile(data, this)
  42. filePath = data.data!!
  43. setPicFromGalery()
  44. }
  45. }
  46. }
  47. }
  48. }
  49.  
  50. private fun setPic() {
  51. // Get the dimensions of the View
  52. val targetW: Int = addInfoguruImageFragmentIV.width
  53. val targetH: Int = addInfoguruImageFragmentIV.height
  54.  
  55. val bmOptions = BitmapFactory.Options().apply {
  56. // Get the dimensions of the bitmap
  57. inJustDecodeBounds = true
  58.  
  59. BitmapFactory.decodeFile(mCurrentPhotoPath, this)
  60.  
  61. val photoW: Int = outWidth
  62. val photoH: Int = outHeight
  63. // Determine how much to scale down the image
  64. val scaleFactor: Int = Math.min(photoW / targetW, photoH / targetH)
  65.  
  66. // Decode the image file into a Bitmap sized to fill the View
  67. inJustDecodeBounds = false
  68. inSampleSize = scaleFactor
  69. inPurgeable = true
  70. }
  71. BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions)?.also { bitmap ->
  72. addInfoguruImageFragmentIV.setImageBitmap(bitmap)
  73. galleryAddPic(bitmap)
  74. }
  75. }
  76.  
  77. private fun setPicFromGalery() {
  78.  
  79. var parcelFD: ParcelFileDescriptor? = null
  80. try {
  81. parcelFD = context?.contentResolver?.openFileDescriptor(filePath, "r")
  82. val imageSource = parcelFD?.fileDescriptor
  83. // Decode image size
  84. val o = BitmapFactory.Options()
  85. o.inJustDecodeBounds = true
  86. BitmapFactory.decodeFileDescriptor(imageSource, null, o)
  87. // the new size we want to scale to
  88. val requiredSize = 1024
  89. // Find the correct scale value. It should be the power of 2.
  90. var widthTmp = o.outWidth
  91. var heightTmp = o.outHeight
  92. var scale = 1
  93. while (true) {
  94. if (widthTmp < requiredSize && heightTmp < requiredSize) {
  95. break
  96. }
  97. widthTmp /= 2
  98. heightTmp /= 2
  99. scale *= 2
  100. }
  101. // decode with inSampleSize
  102. val o2 = BitmapFactory.Options()
  103. o2.inSampleSize = scale
  104. val bitmap = BitmapFactory.decodeFileDescriptor(imageSource, null, o2)
  105. addInfoguruImageFragmentIV.setImageBitmap(bitmap)
  106. galleryAddPic(bitmap)
  107. } catch (e: FileNotFoundException) {
  108. // handle errors
  109. } catch (e: IOException) {
  110. // handle errors
  111. } finally {
  112. if (parcelFD != null)
  113. try {
  114. parcelFD.close()
  115. } catch (e: IOException) {
  116. // ignored
  117. }
  118. }
  119. }
  120.  
  121. private val FILE_MAX_SIZE = 350 * 1024
  122. private var COMPRESS_QUALITY = 99
  123.  
  124. @SuppressLint("LongLogTag", "LogNotTimber")
  125. private fun galleryAddPic(bitmap: Bitmap) {
  126. try {
  127. var bmpStream = ByteArrayOutputStream()
  128. bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bmpStream)
  129. var bmpPicByteArray = bmpStream.toByteArray()
  130. var streamLength = bmpPicByteArray.size
  131. if (streamLength > FILE_MAX_SIZE) {
  132. while (streamLength > FILE_MAX_SIZE) {
  133. bmpStream = ByteArrayOutputStream()
  134.  
  135. bitmap.compress(Bitmap.CompressFormat.JPEG, COMPRESS_QUALITY, bmpStream)
  136. bmpPicByteArray = bmpStream.toByteArray()
  137. streamLength = bmpPicByteArray.size
  138. COMPRESS_QUALITY -= 5
  139. if (COMPRESS_QUALITY == 10) {
  140. break
  141. }
  142. }
  143. if (file!!.exists()) {
  144. val fOut = FileOutputStream(file)
  145. bitmap.compress(Bitmap.CompressFormat.JPEG, COMPRESS_QUALITY, fOut)
  146. fOut.flush()
  147. fOut.close()
  148. }
  149. } else {
  150. if (file!!.exists()) {
  151. val fOut = FileOutputStream(file)
  152. bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut)
  153. fOut.flush()
  154. fOut.close()
  155. }
  156. }
  157. } catch (e: FileNotFoundException) {
  158. Log.e(TAG, "galleryAddPic: error file = ${e.message}")
  159. } catch (e: IOException) {
  160. Log.e(TAG, "galleryAddPic: error IO = ${e.message}")
  161. }
  162. }
  163.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement