Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //inisialisasi
- //untuk edit gambar
- internal lateinit var bitmap: Bitmap
- internal lateinit var decoded: Bitmap
- internal var bitmap_size = 80 // range 1 - 100=
- internal var PICK_IMAGE_REQUEST_1 = 1
- internal var REQUEST_IMAGE_CAPTURE_1 = 11
- //didalam on Create
- image.setOnClickListener {
- //pakai alert dialog
- val builder = android.app.AlertDialog.Builder(activity)
- builder.setTitle("Pilihan Foto")
- //builder.setMessage("Silahkan memilih lihat foto atau edit lewat kamera atau galeri");
- builder.setPositiveButton(
- "Edit Foto"
- ) { dialog, which ->
- showFileChooser()
- }
- builder.setNegativeButton(
- "Lihat Foto"
- ) {
- dialog, which ->
- val fm = fragmentManager
- val args = Bundle()
- args.putString("foto", link_foto)
- val dialogFragment = DialogLihatFotoProfil()
- dialogFragment.setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogFragmentTheme)
- dialogFragment.setArguments(args)
- assert(fm != null)
- dialogFragment.show(fm, "Fragment lihat foto")
- }
- val alert = builder.create()
- alert.show()
- }
- //diluar OnCreate
- //untuk memilih gambar dari galeri
- private fun showFileChooser() {
- val intent = Intent()
- intent.type = "image/*"
- intent.action = Intent.ACTION_GET_CONTENT
- startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST_1)
- }
- //untuk set ke imageview
- private fun setToImageView(bmp: Bitmap) {
- //compress image
- val bytes = ByteArrayOutputStream()
- bmp.compress(Bitmap.CompressFormat.JPEG, bitmap_size, bytes)
- decoded = BitmapFactory.decodeStream(ByteArrayInputStream(bytes.toByteArray()))
- //menampilkan gambar yang dipilih dari camera/gallery ke ImageView
- image.setImageBitmap(decoded)
- }
- override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
- super.onActivityResult(requestCode, resultCode, data)
- //ini untuk gambar
- if (requestCode == PICK_IMAGE_REQUEST_1 && resultCode == RESULT_OK && data != null && data.data != null) {
- val filePath = data.data
- try {
- //mengambil fambar dari Gallery
- bitmap = MediaStore.Images.Media.getBitmap(activity!!.contentResolver, filePath)
- // 512 adalah resolusi tertinggi setelah image di resize, bisa di ganti.
- setToImageView(getResizedBitmap(bitmap, 512))
- btn_simpan_gambar.visibility = View.VISIBLE
- } catch (e: IOException) {
- e.printStackTrace()
- }
- } else if (requestCode == REQUEST_IMAGE_CAPTURE_1 && resultCode == RESULT_OK) {
- val extras = data?.getExtras()
- val imageBitmap = extras!!.get("data") as Bitmap
- //imageView.setImageBitmap(imageBitmap);
- setToImageView(getResizedBitmap(imageBitmap, 512))
- btn_simpan_gambar.visibility = View.VISIBLE
- } else {
- super.onActivityResult(requestCode, resultCode, data)
- }
- }
- //untuk upload image, compress .JPEG ke bitmap
- fun getStringImage(bmp: Bitmap): String {
- val baos = ByteArrayOutputStream()
- bmp.compress(Bitmap.CompressFormat.JPEG, bitmap_size, baos)
- val imageBytes = baos.toByteArray()
- return Base64.encodeToString(imageBytes, Base64.DEFAULT)
- }
- // fungsi resize image
- fun getResizedBitmap(image: Bitmap, maxSize: Int): Bitmap {
- var width = image.width
- var height = image.height
- val bitmapRatio = width.toFloat() / height.toFloat()
- if (bitmapRatio > 1) {
- width = maxSize
- height = (width / bitmapRatio).toInt()
- } else {
- height = maxSize
- width = (height * bitmapRatio).toInt()
- }
- return Bitmap.createScaledBitmap(image, width, height, true)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement