daily pastebin goal
65%
SHARE
TWEET

Untitled

a guest Jan 24th, 2019 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* // use side
  2.  * channelAvatarPicture.setOnClickListener {
  3.  *     startActivityForResult(Intent(this@ChannelActivity, ImagePickingActivity::class.java), ImagePickingActivity.REQUEST_IMAGE_CAPTURE)
  4.  * }
  5.  *  override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
  6.  *     super.onActivityResult(requestCode, resultCode, data)
  7.  *     if (requestCode == ImagePickingActivity.REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
  8.  *         data?.getStringExtra(ImagePickingActivity.CHOSEN_IMAGE_PATH)?.ofType<String> { chosenImagePath ->
  9.  *             // use 'chosenImagePath'
  10.  *         }
  11.  *     }
  12.  * }
  13.  * // AndroidManifest.xml
  14.  *  <uses-permission android:name="android.permission.CAMERA" />
  15.  *  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  16.  *  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  17.  */
  18. import android.Manifest
  19. import android.annotation.SuppressLint
  20. import android.app.Activity
  21. import android.content.Intent
  22. import android.content.pm.PackageManager
  23. import android.graphics.Bitmap
  24. import android.os.Bundle
  25. import android.os.Environment
  26. import android.provider.MediaStore
  27. import android.support.v4.app.ActivityCompat
  28. import android.support.v4.content.ContextCompat
  29. import android.support.v7.app.AppCompatActivity
  30. import jp.co.zeppelin.feelit.android.app.extension.ofType
  31. import java.io.ByteArrayOutputStream
  32. import java.io.File
  33. import java.io.FileOutputStream
  34. import java.io.IOException
  35. import java.text.SimpleDateFormat
  36. import java.util.*
  37.  
  38. class ImagePickingActivity : AppCompatActivity() {
  39.     companion object {
  40.         val CAMERA_REQUEST_CODE = 63254
  41.         val REQUEST_IMAGE_CAPTURE = 3737
  42.         val REQUEST_CAMERA = 23244
  43.         val CHOSEN_IMAGE_PATH = "CHOSEN_IMAGE_PATH"
  44.     }
  45.  
  46.     override fun onCreate(savedInstanceState: Bundle?) {
  47.         super.onCreate(savedInstanceState)
  48.  
  49.         if (ContextCompat.checkSelfPermission(this@ImagePickingActivity, Manifest.permission.CAMERA)
  50.             != PackageManager.PERMISSION_GRANTED) {
  51.             ActivityCompat.requestPermissions(this@ImagePickingActivity,
  52.                 arrayOf(Manifest.permission.CAMERA), CAMERA_REQUEST_CODE)
  53.         } else {
  54.             // Permission has already been granted
  55.             dispatchTakePictureIntent()
  56.         }
  57.  
  58.     }
  59.  
  60.     override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
  61.         super.onRequestPermissionsResult(requestCode, permissions, grantResults)
  62.         when (requestCode) {
  63.             CAMERA_REQUEST_CODE -> {
  64.                 // If request is cancelled, the result arrays are empty.
  65.                 if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
  66.                     dispatchTakePictureIntent()
  67.                 } else {
  68.                     finish()
  69.                 }
  70.                 return
  71.             }
  72.             else -> {
  73.             }
  74.         }
  75.     }
  76.  
  77.     override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
  78.         super.onActivityResult(requestCode, resultCode, data)
  79.         when (resultCode) {
  80.             Activity.RESULT_CANCELED -> {
  81.                 finish()
  82.             }
  83.             Activity.RESULT_OK -> {
  84.                 if (requestCode == REQUEST_CAMERA) {
  85.                     val bitmap = data?.extras?.get("data")
  86.                     bitmap?.ofType<Bitmap> {
  87.                         val filePath = bitmapToFile(it)
  88.                         setResult(Activity.RESULT_OK, Intent().apply { putExtra(CHOSEN_IMAGE_PATH, filePath) })
  89.                         finish()
  90.                     }
  91.                 }
  92.             }
  93.             else -> {
  94.  
  95.             }
  96.         }
  97.     }
  98.  
  99.     @SuppressLint("SimpleDateFormat")
  100.     @Throws(IOException::class)
  101.     private fun createImageFile(): File {
  102.         // Create an image file name
  103.         val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
  104.         val storageDir: File = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
  105.         val file = File.createTempFile(
  106.             "JPEG_${timeStamp}_", /* prefix */
  107.             ".jpg", /* suffix */
  108.             storageDir /* directory */
  109.         )
  110.         return file
  111.     }
  112.  
  113.     private fun bitmapToFile(bitmap: Bitmap): String {
  114.         //create a file to write bitmap data
  115.         val file = createImageFile()
  116.  
  117.         //Convert bitmap to byte array
  118.         val byteArrayOutputStream: ByteArrayOutputStream = ByteArrayOutputStream()
  119.         bitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, byteArrayOutputStream)
  120.         val bitmapdata = byteArrayOutputStream.toByteArray()
  121.  
  122.         //write the bytes in file
  123.         val fileOutputStream = FileOutputStream(file)
  124.         fileOutputStream.write(bitmapdata)
  125.         fileOutputStream.flush()
  126.         fileOutputStream.close()
  127.         return file.absolutePath
  128.     }
  129.  
  130.     private fun dispatchTakePictureIntent() {
  131.         Intent(MediaStore.ACTION_IMAGE_CAPTURE).also { takePictureIntent ->
  132.             takePictureIntent.resolveActivity(packageManager)?.also {
  133.                 startActivityForResult(takePictureIntent, REQUEST_CAMERA)
  134.             }
  135.         }
  136.     }
  137. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top