@@ -31,6 +31,7 @@ import java.io.File
3131import java.io.FileOutputStream
3232import kotlin.math.floor
3333
34+
3435class MainActivity : AppCompatActivity() {
3536 companion object {
3637 const val PERMISSION_REQUEST_CALLBACK_CODE = 1
@@ -125,11 +126,12 @@ class MainActivity : AppCompatActivity() {
125126 requestCode: Int,
126127 permissions: Array<String >, grantResults: IntArray
127128 ) {
129+ super.onRequestPermissionsResult(requestCode, permissions, grantResults)
128130 when (requestCode) {
129131 PERMISSION_REQUEST_CALLBACK_CODE -> {
130132 // If request is cancelled, the result arrays are empty.
131133 if ((grantResults.isNotEmpty() &&
132- grantResults[0] == PackageManager.PERMISSION_GRANTED)
134+ grantResults[0] == PackageManager.PERMISSION_GRANTED)
133135 ) {
134136 // Permission is granted. Continue the action or workflow
135137 // in your app.
@@ -153,6 +155,7 @@ class MainActivity : AppCompatActivity() {
153155 }
154156 }
155157
158+
156159 override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
157160 super.onActivityResult(requestCode, resultCode, data)
158161
@@ -169,15 +172,16 @@ class MainActivity : AppCompatActivity() {
169172 if (resultCode == RESULT_OK) {
170173 val bmpImage = data?.extras?.get(" data" ) as Bitmap
171174 recognizeBarcode(bmpImage)
175+ }
172176 }
173- }
174177
175178 else -> {
176179 showErrorMessage(" No file selected" )
177180 }
178181 }
179182 }
180183
184+
181185 private fun recognizeBarcode(image: Bitmap) {
182186 try {
183187 val smallerBmp = reduceBitmapSize(image)
@@ -215,7 +219,7 @@ class MainActivity : AppCompatActivity() {
215219 stopRecognizeAnimation()
216220
217221 var message = e.message + " : " + e.details
218- if (e.code == 0) {
222+ if (e.httpCode == 0) {
219223 message = " Check ClientId and ClientSecret in ApiClient $message"
220224 }
221225 showErrorMessage(message)
@@ -260,7 +264,7 @@ class MainActivity : AppCompatActivity() {
260264 } catch (e: ApiException) {
261265 runOnUiThread {
262266 var message = e.message + " : " + e.details
263- if (e.code == 0) {
267+ if (e.httpCode == 0) {
264268 message = " Check ClientId and ClientSecret in ApiClient $message"
265269 }
266270 showErrorMessage(message)
@@ -287,8 +291,12 @@ class MainActivity : AppCompatActivity() {
287291 private fun pickFile() {
288292 val getContentIntent = Intent(Intent.ACTION_GET_CONTENT)
289293 getContentIntent.type = " image/*"
290- if (getContentIntent.resolveActivity(packageManager) != null) {
291- startActivityForResult(getContentIntent, ACTION_GET_CONTENT_CALLBACK_CODE)
294+ getContentIntent.addCategory(Intent.CATEGORY_OPENABLE)
295+ try {
296+ startActivityForResult(Intent.createChooser(getContentIntent, " Select an Image to Recognize" ), ACTION_GET_CONTENT_CALLBACK_CODE)
297+ } catch (ex: java.lang.Exception) {
298+ showErrorMessage(" Unable to start file selector" )
292299 }
300+
293301 }
294302}
0 commit comments