Advertisement
edwinkr

UntukUploadGambar

Jul 26th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.18 KB | None | 0 0
  1. //inisialisasi
  2.  //untuk edit gambar
  3.     internal lateinit var bitmap: Bitmap
  4.     internal lateinit var decoded: Bitmap
  5.     internal var bitmap_size = 80 // range 1 - 100=
  6.     internal var PICK_IMAGE_REQUEST_1 = 1
  7.     internal var REQUEST_IMAGE_CAPTURE_1 = 11
  8.  
  9.  
  10. //didalam on Create
  11. image.setOnClickListener {
  12.             //pakai alert dialog
  13.             val builder = android.app.AlertDialog.Builder(activity)
  14.             builder.setTitle("Pilihan Foto")
  15.             //builder.setMessage("Silahkan memilih lihat foto atau edit lewat kamera atau galeri");
  16.  
  17.             builder.setPositiveButton(
  18.                 "Edit Foto"
  19.             ) { dialog, which ->
  20.                 showFileChooser()
  21.             }
  22.             builder.setNegativeButton(
  23.                 "Lihat Foto"
  24.             ) {
  25.                     dialog, which ->
  26.                 val fm = fragmentManager
  27.                 val args = Bundle()
  28.                 args.putString("foto", link_foto)
  29.                 val dialogFragment = DialogLihatFotoProfil()
  30.                 dialogFragment.setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogFragmentTheme)
  31.                 dialogFragment.setArguments(args)
  32.                 assert(fm != null)
  33.                 dialogFragment.show(fm, "Fragment lihat foto")
  34.             }
  35.  
  36.  
  37.             val alert = builder.create()
  38.             alert.show()
  39.         }
  40.  
  41.  
  42. //diluar OnCreate
  43. //untuk memilih gambar dari galeri
  44.     private fun showFileChooser() {
  45.         val intent = Intent()
  46.         intent.type = "image/*"
  47.         intent.action = Intent.ACTION_GET_CONTENT
  48.         startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST_1)
  49.     }
  50.  
  51.  
  52.     //untuk set ke imageview
  53.     private fun setToImageView(bmp: Bitmap) {
  54.         //compress image
  55.         val bytes = ByteArrayOutputStream()
  56.         bmp.compress(Bitmap.CompressFormat.JPEG, bitmap_size, bytes)
  57.         decoded = BitmapFactory.decodeStream(ByteArrayInputStream(bytes.toByteArray()))
  58.         //menampilkan gambar yang dipilih dari camera/gallery ke ImageView
  59.         image.setImageBitmap(decoded)
  60.     }
  61.  
  62.     override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
  63.         super.onActivityResult(requestCode, resultCode, data)
  64.  
  65.         //ini untuk gambar
  66.         if (requestCode == PICK_IMAGE_REQUEST_1 && resultCode == RESULT_OK && data != null && data.data != null) {
  67.             val filePath = data.data
  68.             try {
  69.                 //mengambil fambar dari Gallery
  70.                 bitmap = MediaStore.Images.Media.getBitmap(activity!!.contentResolver, filePath)
  71.                 // 512 adalah resolusi tertinggi setelah image di resize, bisa di ganti.
  72.                 setToImageView(getResizedBitmap(bitmap, 512))
  73.                 btn_simpan_gambar.visibility = View.VISIBLE
  74.             } catch (e: IOException) {
  75.                 e.printStackTrace()
  76.             }
  77.  
  78.         } else if (requestCode == REQUEST_IMAGE_CAPTURE_1 && resultCode == RESULT_OK) {
  79.             val extras = data?.getExtras()
  80.             val imageBitmap = extras!!.get("data") as Bitmap
  81.             //imageView.setImageBitmap(imageBitmap);
  82.             setToImageView(getResizedBitmap(imageBitmap, 512))
  83.             btn_simpan_gambar.visibility = View.VISIBLE
  84.         } else {
  85.             super.onActivityResult(requestCode, resultCode, data)
  86.         }
  87.     }
  88.  
  89.     //untuk upload image, compress .JPEG ke bitmap
  90.     fun getStringImage(bmp: Bitmap): String {
  91.         val baos = ByteArrayOutputStream()
  92.         bmp.compress(Bitmap.CompressFormat.JPEG, bitmap_size, baos)
  93.         val imageBytes = baos.toByteArray()
  94.         return Base64.encodeToString(imageBytes, Base64.DEFAULT)
  95.     }
  96.  
  97.     // fungsi resize image
  98.     fun getResizedBitmap(image: Bitmap, maxSize: Int): Bitmap {
  99.         var width = image.width
  100.         var height = image.height
  101.  
  102.         val bitmapRatio = width.toFloat() / height.toFloat()
  103.         if (bitmapRatio > 1) {
  104.             width = maxSize
  105.             height = (width / bitmapRatio).toInt()
  106.         } else {
  107.             height = maxSize
  108.             width = (height * bitmapRatio).toInt()
  109.         }
  110.         return Bitmap.createScaledBitmap(image, width, height, true)
  111.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement