Advertisement
myLoveOnlyForYou

Untitled

May 3rd, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. package com.example.myapplication
  2.  
  3. import android.content.Intent
  4. import android.support.v7.app.AppCompatActivity
  5. import android.os.Bundle
  6. import android.provider.MediaStore
  7. import android.view.KeyEvent
  8. import android.view.View
  9. import kotlinx.android.synthetic.main.activity_main.*
  10.  
  11. class MainActivity : AppCompatActivity() {
  12.  
  13. override fun onCreate(savedInstanceState: Bundle?) {
  14. super.onCreate(savedInstanceState)
  15. setContentView(R.layout.activity_main)
  16. cameraButton.setOnClickListener { takePhoto()}
  17. imageButton.setOnClickListener { pickFromGallery() }
  18. }
  19. val GALLERY_REQUEST_CODE = 1
  20. private fun pickFromGallery() {
  21. //Create an Intent with action as ACTION_PICK
  22. val intent = Intent(Intent.ACTION_PICK)
  23. // Sets the type as image/*. This ensures only components of type image are selected
  24. intent.type = "image/*"
  25. //We pass an extra array with the accepted mime types. This will ensure only components with these MIME types as targeted.
  26. val mimeTypes = arrayOf("image/jpeg", "image/png")
  27. intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes)
  28. // Launching the Intent
  29. startActivityForResult(intent, GALLERY_REQUEST_CODE)
  30. }
  31. val REQUEST_IMAGE_CAPTURE = 1
  32.  
  33. private fun takePhoto() {
  34. Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
  35. takePictureIntent.resolveActivity(packageManager)?.also {
  36. startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
  37. }
  38. }
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement