Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package tgs.app.pengaduan
- import android.Manifest
- import android.app.Activity
- import android.content.Intent
- import android.content.pm.PackageManager
- import android.graphics.Bitmap
- import android.graphics.BitmapFactory
- import android.net.Uri
- import android.os.Bundle
- import android.os.Environment
- import android.provider.MediaStore
- import android.util.Base64
- import android.util.Log
- import android.view.View
- import android.widget.Toast
- import androidx.appcompat.app.AlertDialog
- import androidx.appcompat.app.AppCompatActivity
- import androidx.core.app.ActivityCompat
- import androidx.core.content.ContextCompat
- import com.androidnetworking.AndroidNetworking
- import com.androidnetworking.common.Priority
- import com.androidnetworking.error.ANError
- import com.androidnetworking.interfaces.JSONObjectRequestListener
- import kotlinx.android.synthetic.main.activity_tambah_data.*
- import org.json.JSONObject
- import java.io.ByteArrayOutputStream
- import java.io.File
- import java.io.IOException
- class TambahDataActivity : AppCompatActivity() {
- private val GALLERY_REQUEST_CODE = 101
- companion object {
- var nikk: String = "NIKK"
- }
- private var encoded_string: String? = null
- private var image_name: String? = null
- private var file: File? = null
- private var file_uri: Uri? = null
- private var bitmap: Bitmap? = null
- private val STORAGE_PERMISSION_CODE = 1
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_tambah_data)
- val niks = intent.extras?.get(nikk).toString()
- btn_tambah_foto.setOnClickListener {
- if (ContextCompat.checkSelfPermission(applicationContext,
- Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED){
- val intent = Intent(Intent.ACTION_PICK)
- intent.type = "image/*"
- val mimeTypes =
- arrayOf("image/jpeg", "image/png")
- intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes)
- intent.putExtra(MediaStore.EXTRA_OUTPUT, file_uri)
- startActivityForResult(intent, GALLERY_REQUEST_CODE)
- } else {
- requestStoragePermission()
- }
- }
- btn_kirim.setOnClickListener {
- val judul: String = edit_judul.text.toString()
- val deskripsi: String = edit_deskripsi.text.toString()
- AndroidNetworking.post(Config.url + "create_pengaduan.php")
- .addBodyParameter("nik", niks)
- .addBodyParameter("judul", judul)
- .addBodyParameter("isi_laporan", deskripsi)
- .addBodyParameter("foto", encoded_string)
- .addBodyParameter("status", "belum ditanggapi")
- .setPriority(Priority.MEDIUM)
- .build()
- .getAsJSONObject(object : JSONObjectRequestListener {
- override fun onResponse(response: JSONObject) {
- if (response.get("response") == "success"){
- Toast.makeText(applicationContext, "Sukses", Toast.LENGTH_SHORT).show()
- finish()
- } else {
- Toast.makeText(applicationContext, "Gagal", Toast.LENGTH_SHORT).show()
- }
- }
- override fun onError(error: ANError) {
- Toast.makeText(applicationContext, error.errorBody, Toast.LENGTH_SHORT).show()
- Toast.makeText(applicationContext, error.errorCode, Toast.LENGTH_SHORT).show()
- Toast.makeText(applicationContext, error.errorDetail, Toast.LENGTH_SHORT).show()
- Log.e("error1", error.errorDetail)
- Log.e("error2", error.errorBody)
- Log.e("error3", error.errorCode.toString())
- }
- })
- }
- }
- private fun requestStoragePermission() {
- if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE)) {
- AlertDialog.Builder(this)
- .setTitle("Permission needed")
- .setMessage("The permission is needed because of it and that")
- .setPositiveButton("OK"){dialog, which ->
- ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), STORAGE_PERMISSION_CODE)
- }
- .setNegativeButton("CANCEL"){dialog, which ->
- dialog.dismiss()
- }
- .create().show()
- } else {
- ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), STORAGE_PERMISSION_CODE)
- }
- }
- override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
- super.onActivityResult(requestCode, resultCode, data)
- if (resultCode == Activity.RESULT_OK) when (requestCode) {
- GALLERY_REQUEST_CODE -> {
- //data.getData returns the content URI for the selected Image
- file_uri = data?.data!!
- val fileee: String = data.data?.lastPathSegment!!
- val filename: String = fileee.substring(fileee.lastIndexOf("/") + 1)
- img_adu.setImageURI(file_uri)
- img_adu.visibility = View.VISIBLE
- file = File(
- Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
- .toString() + File.separator + filename
- )
- file_uri = Uri.fromFile(file)
- bitmap = BitmapFactory.decodeFile(file_uri!!.path)
- try {
- bitmap = MediaStore.Images.Media.getBitmap(contentResolver, file_uri)
- val stream = ByteArrayOutputStream()
- bitmap?.compress(Bitmap.CompressFormat.JPEG, 100, stream)
- bitmap?.recycle()
- val array = stream.toByteArray()
- encoded_string = Base64.encodeToString(array, 0)
- } catch (e: IOException) {
- e.printStackTrace()
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement