Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.myapplication
- import android.content.Intent
- import android.support.v7.app.AppCompatActivity
- import android.os.Bundle
- import android.provider.MediaStore
- import android.view.KeyEvent
- import android.view.View
- import kotlinx.android.synthetic.main.activity_main.*
- class MainActivity : AppCompatActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- cameraButton.setOnClickListener { takePhoto()}
- imageButton.setOnClickListener { pickFromGallery() }
- }
- val GALLERY_REQUEST_CODE = 1
- private fun pickFromGallery() {
- //Create an Intent with action as ACTION_PICK
- val intent = Intent(Intent.ACTION_PICK)
- // Sets the type as image/*. This ensures only components of type image are selected
- intent.type = "image/*"
- //We pass an extra array with the accepted mime types. This will ensure only components with these MIME types as targeted.
- val mimeTypes = arrayOf("image/jpeg", "image/png")
- intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes)
- // Launching the Intent
- startActivityForResult(intent, GALLERY_REQUEST_CODE)
- }
- val REQUEST_IMAGE_CAPTURE = 1
- private fun takePhoto() {
- Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
- takePictureIntent.resolveActivity(packageManager)?.also {
- startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement