将3个图像按钮中的图像排列在相机和图库中的按钮作为警报

时间:2018-06-24 12:17:44

标签: android kotlin

我已经搜索了如何从相机和画廊中挑选图像。但是,我无法在代码中实现它。有人可以告诉我如何在我的代码上实现它吗?我需要为3个图像按钮实现摄像头和图库(作为警报)。但是问题是所有图像按钮中都放置了同一图像。有人可以帮我提供代码吗?

我的活动代码

class MainActivity : AppCompatActivity() {

    private val PICK_IMAGE = 12345
    private val TAKE_PICTURE = 6352
    private var bitmap: Bitmap? = null
    private var imageView: ImageView? = null
    private var fromCamera: Button? = null
    private var fromGallery: Button? = null
    private var imageButton:ImageButton?=null
    private var imageButton4:ImageButton?=null
    private var imageButton5:ImageButton?=null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        imageButton= findViewById(R.id.imageButton) as ImageButton
        imageButton4=findViewById(R.id.imageButton4)as ImageButton
        imageButton5=findViewById(R.id.imageButton5)as ImageButton
        imageButton!!.setOnClickListener{SelectNature()}
        imageButton4!!.setOnClickListener{SelectNature()}
        imageButton5!!.setOnClickListener{SelectNature()}


    }
    private fun SelectNature() {
        val natureArray = arrayOf<String>("Camera", "Gallery")
        val builder = AlertDialog.Builder(this)
        builder.setCancelable(true)
        builder.setItems(natureArray, DialogInterface.OnClickListener { 
        dialogInterface, i ->
            if (natureArray[i] == "Camera") {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && 
                                   ActivityCompat.checkSelfPermission(this, 
                                    Manifest.permission.CAMERA) != 
                                     PackageManager.PERMISSION_GRANTED) {
                    requestPermissions(arrayOf(Manifest.permission.CAMERA),
                            REQUEST_CAMERA_ACCESS_PERMISSION)
                } else {
                    getImageFromCamera()
                }
            } else if (natureArray[i] == "Gallery") {
                getImageFromGallery()
            }
        })
        builder.show()
    }

    private fun persistImage(bitmap: Bitmap?, name: String): File {
        val filesDir = applicationContext.filesDir
        val imageFile = File(filesDir, "$name.jpg")

        val os: OutputStream
        try {
            os = FileOutputStream(imageFile)
            bitmap!!.compress(Bitmap.CompressFormat.JPEG, 100, os)
            os.flush()
            os.close()
        } catch (e: Exception) {
           // Log.e(javaClass.getSimpleName(), "Error writing bitmap", e)
        }

        return imageFile
    }


    private fun getImageFromCamera() {
        val takePicture = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        if (takePicture.resolveActivity(packageManager) != null) {
            startActivityForResult(takePicture, TAKE_PICTURE)
        }
    }

    private fun getImageFromGallery() {
        val intent = Intent()
        intent.type = "image/*"
        intent.action = Intent.ACTION_GET_CONTENT
        if (intent.resolveActivity(packageManager) != null) {
            startActivityForResult(Intent.createChooser(intent, "Select 
                                                  Picture"), PICK_IMAGE)
        }
    }

     override fun onActivityResult(requestCode: Int, resultCode: Int, data: 
                                                           Intent) {
        super.onActivityResult(requestCode, resultCode, data)
        if (imageButton?.isEnabled!!) {
            if (requestCode == PICK_IMAGE) {
                if (resultCode == Activity.RESULT_OK) {
                    try {
                        val inputStream1 = 
                                contentResolver.openInputStream(data.data!!)
                        bitmap = BitmapFactory.decodeStream(inputStream1)
                        imageButton!!.setImageBitmap(bitmap)
                    } catch (e: FileNotFoundException) {
                        e.printStackTrace()
                    }

                }
            } else if (requestCode == TAKE_PICTURE) {
                if (resultCode == Activity.RESULT_OK) {
                    val extras1 = data.extras
                    bitmap = extras1!!.get("data") as Bitmap
                    imageButton!!.setImageBitmap(bitmap)
                }
            }
        }
        if (imageButton4?.isEnabled!!) {
            if (requestCode == PICK_IMAGE) {
                if (resultCode == Activity.RESULT_OK) {
                    try {
                        val inputStream = 
                              contentResolver.openInputStream(data.data!!)
                        bitmap = BitmapFactory.decodeStream(inputStream)
                        imageButton4!!.setImageBitmap(bitmap)
                    } catch (e: FileNotFoundException) {
                        e.printStackTrace()
                    }

                }
            } else if (requestCode == TAKE_PICTURE) {
                if (resultCode == Activity.RESULT_OK) {
                    val extras = data.extras
                    bitmap = extras!!.get("data") as Bitmap
                    imageButton4!!.setImageBitmap(bitmap)
                }
            }
        }
      }

       override fun onRequestPermissionsResult(requestCode: Int, permissions: 
                        Array<String>, grantResults: IntArray) {
        when (requestCode) {
            REQUEST_CAMERA_ACCESS_PERMISSION -> if (grantResults[0] == 
                                       PackageManager.PERMISSION_GRANTED) {
                getImageFromCamera()
            }
            else -> super.onRequestPermissionsResult(requestCode, permissions, 
                         grantResults)
        }
    }

    companion object {
        private val REQUEST_CAMERA_ACCESS_PERMISSION = 5674
    }
}

0 个答案:

没有答案