1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
class CameraAlbumActivity : BaseActivity() {
val takePhoto = 1 lateinit var imageUri: Uri lateinit var outputImage: File
val fromAlbum = 2
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_cemara)
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager manager.cancel(1)
initData() }
private fun initData() { takePhoto() fromAlbum() }
private fun fromAlbum(){ btnFromAlbum.setOnClickListener{ val intent = Intent(Intent.ACTION_OPEN_DOCUMENT) intent.addCategory(Intent.CATEGORY_OPENABLE) intent.type = "image/*" startActivityForResult(intent,fromAlbum) } }
private fun takePhoto() { btnTakePhoto.setOnClickListener{ outputImage = File(externalCacheDir,"output_image.jpg") if (outputImage.exists()){ outputImage.delete() } outputImage.createNewFile() imageUri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){ FileProvider.getUriForFile(this,"com.example.myapplication.fileprovider",outputImage) }else { Uri.fromFile(outputImage) } val intent = Intent("android.media.action.IMAGE_CAPTURE") intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri) startActivityForResult(intent,takePhoto) } }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) when(requestCode){ takePhoto -> { if (resultCode == Activity.RESULT_OK){ val bitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(imageUri)) ivPhoto.setImageBitmap(rotateIfRequired(bitmap)) } } fromAlbum -> { if (resultCode == Activity.RESULT_OK && data != null){ data.data?.let { uri -> val bitmap = getBitmapFromUri(uri) ivPhoto.setImageBitmap(bitmap) } } } } }
private fun getBitmapFromUri(uri: Uri) = contentResolver.openFileDescriptor(uri,"r")?.use { BitmapFactory.decodeFileDescriptor(it.fileDescriptor) }
private fun rotateIfRequired(bitmap: Bitmap): Bitmap { val exif = ExifInterface(outputImage.path) val orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL) return when(orientation){ ExifInterface.ORIENTATION_ROTATE_90 -> rotateBitmap(bitmap,90) ExifInterface.ORIENTATION_ROTATE_180 -> rotateBitmap(bitmap,180) ExifInterface.ORIENTATION_ROTATE_270 -> rotateBitmap(bitmap,270) else -> bitmap } }
private fun rotateBitmap(bitmap: Bitmap, degree: Int): Bitmap { val matrix = Matrix() matrix.postRotate(degree.toFloat()) val rotatedBitmap = Bitmap.createBitmap(bitmap,0,0,bitmap.width,bitmap.height,matrix,true) bitmap.recycle() return rotatedBitmap }
companion object{ fun actionStart(context: Context){ val intent = Intent(context, CameraAlbumActivity::class.java) context.startActivity(intent) } } }
|