質問編集履歴
1
質問の改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -12,574 +12,578 @@
|
|
12
12
|
|
13
13
|
### 発生している問題・エラーメッセージ
|
14
14
|
|
15
|
+
写真を撮ってみると、トースト表示していることもあり
|
16
|
+
|
17
|
+
写真を撮れていることの確認とファイル名がとれていることがわかっております。
|
18
|
+
|
19
|
+
しかし、この後imageviewで表示するために
|
20
|
+
|
21
|
+
どうすればいいのかわかっておりません。
|
22
|
+
|
23
|
+
こちらはギャラリーを一度開く必要があるのでしょうか。それともファイル名が取得できてさえいればそのまま表示できるのでしょうか。
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
### 該当のソースコード
|
28
|
+
|
29
|
+
|
30
|
+
|
31
|
+
```MainActivity
|
32
|
+
|
33
|
+
import android.Manifest.permission.CAMERA
|
34
|
+
|
35
|
+
import android.Manifest.permission.WRITE_EXTERNAL_STORAGE
|
36
|
+
|
37
|
+
import android.app.AlertDialog
|
38
|
+
|
39
|
+
import android.content.ContentValues
|
40
|
+
|
41
|
+
import android.content.Intent
|
42
|
+
|
43
|
+
import android.content.pm.PackageManager
|
44
|
+
|
45
|
+
import android.hardware.Camera
|
46
|
+
|
47
|
+
import android.net.Uri
|
48
|
+
|
49
|
+
import android.os.Build
|
50
|
+
|
51
|
+
import android.os.Bundle
|
52
|
+
|
53
|
+
import android.os.Environment
|
54
|
+
|
55
|
+
import android.provider.MediaStore
|
56
|
+
|
57
|
+
import android.view.SurfaceHolder
|
58
|
+
|
59
|
+
import android.view.SurfaceView
|
60
|
+
|
61
|
+
import android.view.View
|
62
|
+
|
63
|
+
import android.widget.Button
|
64
|
+
|
65
|
+
import android.widget.ImageView
|
66
|
+
|
67
|
+
import android.widget.Toast
|
68
|
+
|
69
|
+
import androidx.appcompat.app.AppCompatActivity
|
70
|
+
|
71
|
+
import androidx.core.app.ActivityCompat
|
72
|
+
|
73
|
+
import androidx.core.content.ContextCompat
|
74
|
+
|
75
|
+
import java.io.File
|
76
|
+
|
77
|
+
import java.io.FileNotFoundException
|
78
|
+
|
79
|
+
import java.io.FileOutputStream
|
80
|
+
|
81
|
+
import java.io.IOException
|
82
|
+
|
83
|
+
import java.util.*
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
class MainActivity : AppCompatActivity(), SurfaceHolder.Callback, Camera.PictureCallback {
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
private var surfaceHolder: SurfaceHolder? = null
|
92
|
+
|
93
|
+
private var camera: Camera? = null
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
private var surfaceView: SurfaceView? = null
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
private val neededPermissions = arrayOf(CAMERA, WRITE_EXTERNAL_STORAGE)
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
private var _imageUri: Uri? = null
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
override fun onCreate(savedInstanceState: Bundle?) {
|
110
|
+
|
111
|
+
super.onCreate(savedInstanceState)
|
112
|
+
|
113
|
+
setContentView(R.layout.activity_main)
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
surfaceView = findViewById(R.id.surfaceView)
|
118
|
+
|
119
|
+
val result = checkPermission()
|
120
|
+
|
121
|
+
if (result) {
|
122
|
+
|
123
|
+
setupSurfaceHolder()
|
124
|
+
|
125
|
+
}
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
// public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
130
|
+
|
131
|
+
// super.onActivityResult(requestCode, resultCode, data)
|
132
|
+
|
133
|
+
// if(requestCode == 200 && resultCode == RESULT_OK) {
|
134
|
+
|
135
|
+
//
|
136
|
+
|
137
|
+
//
|
138
|
+
|
139
|
+
// }
|
140
|
+
|
141
|
+
// }
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
private fun checkPermission(): Boolean {
|
146
|
+
|
147
|
+
val currentAPIVersion = Build.VERSION.SDK_INT
|
148
|
+
|
149
|
+
if (currentAPIVersion >= android.os.Build.VERSION_CODES.M) {
|
150
|
+
|
151
|
+
val permissionsNotGranted = ArrayList<String>()
|
152
|
+
|
153
|
+
for (permission in neededPermissions) {
|
154
|
+
|
155
|
+
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
|
156
|
+
|
157
|
+
permissionsNotGranted.add(permission)
|
158
|
+
|
159
|
+
}
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
if (permissionsNotGranted.size > 0) {
|
164
|
+
|
165
|
+
var shouldShowAlert = false
|
166
|
+
|
167
|
+
for (permission in permissionsNotGranted) {
|
168
|
+
|
169
|
+
shouldShowAlert = ActivityCompat.shouldShowRequestPermissionRationale(this, permission)
|
170
|
+
|
171
|
+
}
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
val arr = arrayOfNulls<String>(permissionsNotGranted.size)
|
176
|
+
|
177
|
+
val permissions = permissionsNotGranted.toArray(arr)
|
178
|
+
|
179
|
+
if (shouldShowAlert) {
|
180
|
+
|
181
|
+
showPermissionAlert(permissions)
|
182
|
+
|
183
|
+
} else {
|
184
|
+
|
185
|
+
requestPermissions(permissions)
|
186
|
+
|
187
|
+
}
|
188
|
+
|
189
|
+
return false
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
return true
|
196
|
+
|
197
|
+
}
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
private fun showPermissionAlert(permissions: Array<String?>) {
|
202
|
+
|
203
|
+
val alertBuilder = AlertDialog.Builder(this)
|
204
|
+
|
205
|
+
alertBuilder.setCancelable(true)
|
206
|
+
|
207
|
+
alertBuilder.setTitle(R.string.permission_required)
|
208
|
+
|
209
|
+
alertBuilder.setMessage(R.string.permission_message)
|
210
|
+
|
211
|
+
alertBuilder.setPositiveButton(android.R.string.yes) { _, _ -> requestPermissions(permissions) }
|
212
|
+
|
213
|
+
val alert = alertBuilder.create()
|
214
|
+
|
215
|
+
alert.show()
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
private fun requestPermissions(permissions: Array<String?>) {
|
222
|
+
|
223
|
+
ActivityCompat.requestPermissions(this@MainActivity, permissions, REQUEST_CODE)
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
|
230
|
+
|
231
|
+
when (requestCode) {
|
232
|
+
|
233
|
+
REQUEST_CODE -> {
|
234
|
+
|
235
|
+
for (result in grantResults) {
|
236
|
+
|
237
|
+
if (result == PackageManager.PERMISSION_DENIED) {
|
238
|
+
|
239
|
+
Toast.makeText(this@MainActivity, R.string.permission_warning, Toast.LENGTH_LONG).show()
|
240
|
+
|
241
|
+
View.VISIBLE.setViewVisibility(R.id.showPermissionMsg)
|
242
|
+
|
243
|
+
return
|
244
|
+
|
245
|
+
}
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
setupSurfaceHolder()
|
252
|
+
|
253
|
+
}
|
254
|
+
|
255
|
+
}
|
256
|
+
|
257
|
+
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
258
|
+
|
259
|
+
}
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
private fun Int.setViewVisibility(id: Int) {
|
264
|
+
|
265
|
+
val view = findViewById<View>(id)
|
266
|
+
|
267
|
+
view!!.visibility = this
|
268
|
+
|
269
|
+
}
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
private fun setupSurfaceHolder() {
|
274
|
+
|
275
|
+
View.VISIBLE.setViewVisibility(R.id.startBtn)
|
276
|
+
|
277
|
+
View.VISIBLE.setViewVisibility(R.id.surfaceView)
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
surfaceHolder = surfaceView!!.holder
|
282
|
+
|
283
|
+
surfaceHolder!!.addCallback(this)
|
284
|
+
|
285
|
+
setBtnClick()
|
286
|
+
|
287
|
+
}
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
private fun setBtnClick() {
|
292
|
+
|
293
|
+
val startBtn = findViewById<Button>(R.id.startBtn)
|
294
|
+
|
295
|
+
startBtn?.setOnClickListener { captureImage() }
|
296
|
+
|
297
|
+
}
|
298
|
+
|
299
|
+
|
300
|
+
|
301
|
+
private fun captureImage() {
|
302
|
+
|
303
|
+
if (camera != null) {
|
304
|
+
|
305
|
+
camera!!.takePicture(null, null, this)
|
306
|
+
|
307
|
+
}
|
308
|
+
|
309
|
+
}
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
override fun surfaceCreated(surfaceHolder: SurfaceHolder) {
|
314
|
+
|
315
|
+
startCamera()
|
316
|
+
|
317
|
+
}
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
private fun startCamera() {
|
322
|
+
|
323
|
+
camera = Camera.open()
|
324
|
+
|
325
|
+
camera!!.setDisplayOrientation(90)
|
326
|
+
|
327
|
+
try {
|
328
|
+
|
329
|
+
camera!!.setPreviewDisplay(surfaceHolder)
|
330
|
+
|
331
|
+
camera!!.startPreview()
|
332
|
+
|
333
|
+
} catch (e: IOException) {
|
334
|
+
|
335
|
+
e.printStackTrace()
|
336
|
+
|
337
|
+
}
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
}
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
override fun surfaceChanged(surfaceHolder: SurfaceHolder, i: Int, i1: Int, i2: Int) {
|
346
|
+
|
347
|
+
resetCamera()
|
348
|
+
|
349
|
+
}
|
350
|
+
|
351
|
+
|
352
|
+
|
353
|
+
private fun resetCamera() {
|
354
|
+
|
355
|
+
if (surfaceHolder!!.surface == null) {
|
356
|
+
|
357
|
+
// Return if preview surface does not exist
|
358
|
+
|
359
|
+
return
|
360
|
+
|
361
|
+
}
|
362
|
+
|
363
|
+
|
364
|
+
|
365
|
+
// Stop if preview surface is already running.
|
366
|
+
|
367
|
+
camera!!.stopPreview()
|
368
|
+
|
369
|
+
try {
|
370
|
+
|
371
|
+
// Set preview display
|
372
|
+
|
373
|
+
camera!!.setPreviewDisplay(surfaceHolder)
|
374
|
+
|
375
|
+
} catch (e: IOException) {
|
376
|
+
|
377
|
+
e.printStackTrace()
|
378
|
+
|
379
|
+
}
|
380
|
+
|
381
|
+
|
382
|
+
|
383
|
+
// Start the camera preview...
|
384
|
+
|
385
|
+
camera!!.startPreview()
|
386
|
+
|
387
|
+
}
|
388
|
+
|
389
|
+
|
390
|
+
|
391
|
+
override fun surfaceDestroyed(surfaceHolder: SurfaceHolder) {
|
392
|
+
|
393
|
+
releaseCamera()
|
394
|
+
|
395
|
+
}
|
396
|
+
|
397
|
+
|
398
|
+
|
399
|
+
private fun releaseCamera() {
|
400
|
+
|
401
|
+
camera!!.stopPreview()
|
402
|
+
|
403
|
+
camera!!.release()
|
404
|
+
|
405
|
+
camera = null
|
406
|
+
|
407
|
+
}
|
408
|
+
|
409
|
+
|
410
|
+
|
411
|
+
override fun onPictureTaken(bytes: ByteArray, camera: Camera) {
|
412
|
+
|
413
|
+
saveImage(bytes)
|
414
|
+
|
415
|
+
resetCamera()
|
416
|
+
|
417
|
+
}
|
418
|
+
|
419
|
+
|
420
|
+
|
421
|
+
private fun saveImage(bytes: ByteArray) {
|
422
|
+
|
423
|
+
val outStream: FileOutputStream
|
424
|
+
|
425
|
+
try {
|
426
|
+
|
427
|
+
val fileName = "TUTORIALWING_" + System.currentTimeMillis() + ".jpg"
|
428
|
+
|
429
|
+
val file = File(Environment.getExternalStorageDirectory(), fileName)
|
430
|
+
|
431
|
+
|
432
|
+
|
433
|
+
|
434
|
+
|
435
|
+
|
436
|
+
|
437
|
+
val values = ContentValues()
|
438
|
+
|
439
|
+
values.put(MediaStore.Images.Media.TITLE, fileName)
|
440
|
+
|
441
|
+
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
|
442
|
+
|
443
|
+
_imageUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
|
444
|
+
|
445
|
+
// val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
|
446
|
+
|
447
|
+
intent.putExtra(MediaStore.EXTRA_OUTPUT, _imageUri)
|
448
|
+
|
449
|
+
// startActivityForResult(intent, 200)
|
450
|
+
|
451
|
+
|
452
|
+
|
453
|
+
outStream = FileOutputStream(file)
|
454
|
+
|
455
|
+
outStream.write(bytes)
|
456
|
+
|
457
|
+
outStream.close()
|
458
|
+
|
459
|
+
Toast.makeText(this@MainActivity, "Picture Saved: $fileName", Toast.LENGTH_LONG).show()
|
460
|
+
|
461
|
+
|
462
|
+
|
463
|
+
val ivCamera = findViewById<ImageView>(R.id.ivCamera)
|
464
|
+
|
465
|
+
ivCamera.setImageURI(_imageUri)
|
466
|
+
|
467
|
+
|
468
|
+
|
469
|
+
} catch (e: FileNotFoundException) {
|
470
|
+
|
471
|
+
e.printStackTrace()
|
472
|
+
|
473
|
+
} catch (e: IOException) {
|
474
|
+
|
475
|
+
e.printStackTrace()
|
476
|
+
|
477
|
+
}
|
478
|
+
|
479
|
+
}
|
480
|
+
|
481
|
+
|
482
|
+
|
483
|
+
companion object {
|
484
|
+
|
485
|
+
const val REQUEST_CODE = 100
|
486
|
+
|
487
|
+
}
|
488
|
+
|
489
|
+
}
|
490
|
+
|
491
|
+
```
|
492
|
+
|
493
|
+
```xml
|
494
|
+
|
495
|
+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
496
|
+
|
497
|
+
xmlns:app="http://schemas.android.com/apk/res-auto"
|
498
|
+
|
499
|
+
xmlns:tools="http://schemas.android.com/tools"
|
500
|
+
|
501
|
+
android:layout_width="match_parent"
|
502
|
+
|
503
|
+
android:layout_height="match_parent"
|
504
|
+
|
505
|
+
android:orientation="vertical">
|
506
|
+
|
507
|
+
|
508
|
+
|
509
|
+
<SurfaceView
|
510
|
+
|
511
|
+
android:id="@+id/surfaceView"
|
512
|
+
|
513
|
+
android:layout_width="match_parent"
|
514
|
+
|
515
|
+
android:layout_height="match_parent"
|
516
|
+
|
517
|
+
android:visibility="gone" />
|
518
|
+
|
519
|
+
|
520
|
+
|
521
|
+
<Button
|
522
|
+
|
523
|
+
android:id="@+id/startBtn"
|
524
|
+
|
525
|
+
android:layout_width="wrap_content"
|
526
|
+
|
527
|
+
android:layout_height="wrap_content"
|
528
|
+
|
529
|
+
android:layout_gravity="center_horizontal|bottom"
|
530
|
+
|
531
|
+
android:text="@string/take_photo"
|
532
|
+
|
533
|
+
android:visibility="gone" />
|
534
|
+
|
535
|
+
|
536
|
+
|
537
|
+
<TextView
|
538
|
+
|
539
|
+
android:id="@+id/showPermissionMsg"
|
540
|
+
|
541
|
+
android:layout_width="wrap_content"
|
542
|
+
|
543
|
+
android:layout_height="wrap_content"
|
544
|
+
|
545
|
+
android:layout_gravity="center"
|
546
|
+
|
547
|
+
android:padding="@dimen/show_permission_padding"
|
548
|
+
|
549
|
+
android:text="@string/permission_message"
|
550
|
+
|
551
|
+
android:textAlignment="center"
|
552
|
+
|
553
|
+
android:textStyle="bold|italic"
|
554
|
+
|
555
|
+
android:visibility="gone" />
|
556
|
+
|
557
|
+
|
558
|
+
|
559
|
+
<ImageView
|
560
|
+
|
561
|
+
android:id="@+id/ivCamera"
|
562
|
+
|
563
|
+
android:layout_width="match_parent"
|
564
|
+
|
565
|
+
android:layout_height="match_parent"
|
566
|
+
|
567
|
+
android:layout_gravity="center"
|
568
|
+
|
569
|
+
android:scaleType="center"/>
|
570
|
+
|
571
|
+
|
572
|
+
|
573
|
+
</FrameLayout>
|
574
|
+
|
575
|
+
```
|
576
|
+
|
577
|
+
### 試したこと
|
578
|
+
|
579
|
+
|
580
|
+
|
15
|
-
以前、ただimageviewに
|
581
|
+
以前、撮った写真をただimageviewに表示するには
|
16
582
|
|
17
583
|
setImageURIを使えば解決しておりましたが、
|
18
584
|
|
19
585
|
今回はこちらを追加しても何も変化が起きませんでした。
|
20
586
|
|
21
|
-
|
22
|
-
|
23
|
-
### 該当のソースコード
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
```MainActivity
|
28
|
-
|
29
|
-
import android.Manifest.permission.CAMERA
|
30
|
-
|
31
|
-
import android.Manifest.permission.WRITE_EXTERNAL_STORAGE
|
32
|
-
|
33
|
-
import android.app.AlertDialog
|
34
|
-
|
35
|
-
import android.content.ContentValues
|
36
|
-
|
37
|
-
import android.content.Intent
|
38
|
-
|
39
|
-
import android.content.pm.PackageManager
|
40
|
-
|
41
|
-
import android.hardware.Camera
|
42
|
-
|
43
|
-
import android.net.Uri
|
44
|
-
|
45
|
-
import android.os.Build
|
46
|
-
|
47
|
-
import android.os.Bundle
|
48
|
-
|
49
|
-
import android.os.Environment
|
50
|
-
|
51
|
-
import android.provider.MediaStore
|
52
|
-
|
53
|
-
import android.view.SurfaceHolder
|
54
|
-
|
55
|
-
import android.view.SurfaceView
|
56
|
-
|
57
|
-
import android.view.View
|
58
|
-
|
59
|
-
import android.widget.Button
|
60
|
-
|
61
|
-
import android.widget.ImageView
|
62
|
-
|
63
|
-
import android.widget.Toast
|
64
|
-
|
65
|
-
import androidx.appcompat.app.AppCompatActivity
|
66
|
-
|
67
|
-
import androidx.core.app.ActivityCompat
|
68
|
-
|
69
|
-
import androidx.core.content.ContextCompat
|
70
|
-
|
71
|
-
import java.io.File
|
72
|
-
|
73
|
-
import java.io.FileNotFoundException
|
74
|
-
|
75
|
-
import java.io.FileOutputStream
|
76
|
-
|
77
|
-
import java.io.IOException
|
78
|
-
|
79
|
-
import java.util.*
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
class MainActivity : AppCompatActivity(), SurfaceHolder.Callback, Camera.PictureCallback {
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
private var surfaceHolder: SurfaceHolder? = null
|
88
|
-
|
89
|
-
private var camera: Camera? = null
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
private var surfaceView: SurfaceView? = null
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
private val neededPermissions = arrayOf(CAMERA, WRITE_EXTERNAL_STORAGE)
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
private var _imageUri: Uri? = null
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
override fun onCreate(savedInstanceState: Bundle?) {
|
106
|
-
|
107
|
-
super.onCreate(savedInstanceState)
|
108
|
-
|
109
|
-
setContentView(R.layout.activity_main)
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
surfaceView = findViewById(R.id.surfaceView)
|
114
|
-
|
115
|
-
val result = checkPermission()
|
116
|
-
|
117
|
-
if (result) {
|
118
|
-
|
119
|
-
setupSurfaceHolder()
|
120
|
-
|
121
|
-
}
|
122
|
-
|
123
|
-
}
|
124
|
-
|
125
|
-
// public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
126
|
-
|
127
|
-
// super.onActivityResult(requestCode, resultCode, data)
|
128
|
-
|
129
|
-
// if(requestCode == 200 && resultCode == RESULT_OK) {
|
130
|
-
|
131
|
-
//
|
132
|
-
|
133
|
-
//
|
134
|
-
|
135
|
-
// }
|
136
|
-
|
137
|
-
// }
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
private fun checkPermission(): Boolean {
|
142
|
-
|
143
|
-
val currentAPIVersion = Build.VERSION.SDK_INT
|
144
|
-
|
145
|
-
if (currentAPIVersion >= android.os.Build.VERSION_CODES.M) {
|
146
|
-
|
147
|
-
val permissionsNotGranted = ArrayList<String>()
|
148
|
-
|
149
|
-
for (permission in neededPermissions) {
|
150
|
-
|
151
|
-
if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
|
152
|
-
|
153
|
-
permissionsNotGranted.add(permission)
|
154
|
-
|
155
|
-
}
|
156
|
-
|
157
|
-
}
|
158
|
-
|
159
|
-
if (permissionsNotGranted.size > 0) {
|
160
|
-
|
161
|
-
var shouldShowAlert = false
|
162
|
-
|
163
|
-
for (permission in permissionsNotGranted) {
|
164
|
-
|
165
|
-
shouldShowAlert = ActivityCompat.shouldShowRequestPermissionRationale(this, permission)
|
166
|
-
|
167
|
-
}
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
val arr = arrayOfNulls<String>(permissionsNotGranted.size)
|
172
|
-
|
173
|
-
val permissions = permissionsNotGranted.toArray(arr)
|
174
|
-
|
175
|
-
if (shouldShowAlert) {
|
176
|
-
|
177
|
-
showPermissionAlert(permissions)
|
178
|
-
|
179
|
-
} else {
|
180
|
-
|
181
|
-
requestPermissions(permissions)
|
182
|
-
|
183
|
-
}
|
184
|
-
|
185
|
-
return false
|
186
|
-
|
187
|
-
}
|
188
|
-
|
189
|
-
}
|
190
|
-
|
191
|
-
return true
|
192
|
-
|
193
|
-
}
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
private fun showPermissionAlert(permissions: Array<String?>) {
|
198
|
-
|
199
|
-
val alertBuilder = AlertDialog.Builder(this)
|
200
|
-
|
201
|
-
alertBuilder.setCancelable(true)
|
202
|
-
|
203
|
-
alertBuilder.setTitle(R.string.permission_required)
|
204
|
-
|
205
|
-
alertBuilder.setMessage(R.string.permission_message)
|
206
|
-
|
207
|
-
alertBuilder.setPositiveButton(android.R.string.yes) { _, _ -> requestPermissions(permissions) }
|
208
|
-
|
209
|
-
val alert = alertBuilder.create()
|
210
|
-
|
211
|
-
alert.show()
|
212
|
-
|
213
|
-
}
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
private fun requestPermissions(permissions: Array<String?>) {
|
218
|
-
|
219
|
-
ActivityCompat.requestPermissions(this@MainActivity, permissions, REQUEST_CODE)
|
220
|
-
|
221
|
-
}
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
|
226
|
-
|
227
|
-
when (requestCode) {
|
228
|
-
|
229
|
-
REQUEST_CODE -> {
|
230
|
-
|
231
|
-
for (result in grantResults) {
|
232
|
-
|
233
|
-
if (result == PackageManager.PERMISSION_DENIED) {
|
234
|
-
|
235
|
-
Toast.makeText(this@MainActivity, R.string.permission_warning, Toast.LENGTH_LONG).show()
|
236
|
-
|
237
|
-
View.VISIBLE.setViewVisibility(R.id.showPermissionMsg)
|
238
|
-
|
239
|
-
return
|
240
|
-
|
241
|
-
}
|
242
|
-
|
243
|
-
}
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
setupSurfaceHolder()
|
248
|
-
|
249
|
-
}
|
250
|
-
|
251
|
-
}
|
252
|
-
|
253
|
-
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
254
|
-
|
255
|
-
}
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
private fun Int.setViewVisibility(id: Int) {
|
260
|
-
|
261
|
-
val view = findViewById<View>(id)
|
262
|
-
|
263
|
-
view!!.visibility = this
|
264
|
-
|
265
|
-
}
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
private fun setupSurfaceHolder() {
|
270
|
-
|
271
|
-
View.VISIBLE.setViewVisibility(R.id.startBtn)
|
272
|
-
|
273
|
-
View.VISIBLE.setViewVisibility(R.id.surfaceView)
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
surfaceHolder = surfaceView!!.holder
|
278
|
-
|
279
|
-
surfaceHolder!!.addCallback(this)
|
280
|
-
|
281
|
-
setBtnClick()
|
282
|
-
|
283
|
-
}
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
private fun setBtnClick() {
|
288
|
-
|
289
|
-
val startBtn = findViewById<Button>(R.id.startBtn)
|
290
|
-
|
291
|
-
startBtn?.setOnClickListener { captureImage() }
|
292
|
-
|
293
|
-
}
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
private fun captureImage() {
|
298
|
-
|
299
|
-
if (camera != null) {
|
300
|
-
|
301
|
-
camera!!.takePicture(null, null, this)
|
302
|
-
|
303
|
-
}
|
304
|
-
|
305
|
-
}
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
override fun surfaceCreated(surfaceHolder: SurfaceHolder) {
|
310
|
-
|
311
|
-
startCamera()
|
312
|
-
|
313
|
-
}
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
private fun startCamera() {
|
318
|
-
|
319
|
-
camera = Camera.open()
|
320
|
-
|
321
|
-
camera!!.setDisplayOrientation(90)
|
322
|
-
|
323
|
-
try {
|
324
|
-
|
325
|
-
camera!!.setPreviewDisplay(surfaceHolder)
|
326
|
-
|
327
|
-
camera!!.startPreview()
|
328
|
-
|
329
|
-
} catch (e: IOException) {
|
330
|
-
|
331
|
-
e.printStackTrace()
|
332
|
-
|
333
|
-
}
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
}
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
override fun surfaceChanged(surfaceHolder: SurfaceHolder, i: Int, i1: Int, i2: Int) {
|
342
|
-
|
343
|
-
resetCamera()
|
344
|
-
|
345
|
-
}
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
private fun resetCamera() {
|
350
|
-
|
351
|
-
if (surfaceHolder!!.surface == null) {
|
352
|
-
|
353
|
-
// Return if preview surface does not exist
|
354
|
-
|
355
|
-
return
|
356
|
-
|
357
|
-
}
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
// Stop if preview surface is already running.
|
362
|
-
|
363
|
-
camera!!.stopPreview()
|
364
|
-
|
365
|
-
try {
|
366
|
-
|
367
|
-
// Set preview display
|
368
|
-
|
369
|
-
camera!!.setPreviewDisplay(surfaceHolder)
|
370
|
-
|
371
|
-
} catch (e: IOException) {
|
372
|
-
|
373
|
-
e.printStackTrace()
|
374
|
-
|
375
|
-
}
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
// Start the camera preview...
|
380
|
-
|
381
|
-
camera!!.startPreview()
|
382
|
-
|
383
|
-
}
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
override fun surfaceDestroyed(surfaceHolder: SurfaceHolder) {
|
388
|
-
|
389
|
-
releaseCamera()
|
390
|
-
|
391
|
-
}
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
private fun releaseCamera() {
|
396
|
-
|
397
|
-
camera!!.stopPreview()
|
398
|
-
|
399
|
-
camera!!.release()
|
400
|
-
|
401
|
-
camera = null
|
402
|
-
|
403
|
-
}
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
override fun onPictureTaken(bytes: ByteArray, camera: Camera) {
|
408
|
-
|
409
|
-
saveImage(bytes)
|
410
|
-
|
411
|
-
resetCamera()
|
412
|
-
|
413
|
-
}
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
private fun saveImage(bytes: ByteArray) {
|
418
|
-
|
419
|
-
val outStream: FileOutputStream
|
420
|
-
|
421
|
-
try {
|
422
|
-
|
423
|
-
val fileName = "TUTORIALWING_" + System.currentTimeMillis() + ".jpg"
|
424
|
-
|
425
|
-
val file = File(Environment.getExternalStorageDirectory(), fileName)
|
426
|
-
|
427
|
-
|
428
|
-
|
429
|
-
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
val values = ContentValues()
|
434
|
-
|
435
|
-
values.put(MediaStore.Images.Media.TITLE, fileName)
|
436
|
-
|
437
|
-
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
|
438
|
-
|
439
|
-
_imageUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
|
440
|
-
|
441
|
-
// val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
|
442
|
-
|
443
|
-
intent.putExtra(MediaStore.EXTRA_OUTPUT, _imageUri)
|
444
|
-
|
445
|
-
// startActivityForResult(intent, 200)
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
outStream = FileOutputStream(file)
|
450
|
-
|
451
|
-
outStream.write(bytes)
|
452
|
-
|
453
|
-
outStream.close()
|
454
|
-
|
455
|
-
Toast.makeText(this@MainActivity, "Picture Saved: $fileName", Toast.LENGTH_LONG).show()
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
val ivCamera = findViewById<ImageView>(R.id.ivCamera)
|
460
|
-
|
461
|
-
ivCamera.setImageURI(_imageUri)
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
} catch (e: FileNotFoundException) {
|
466
|
-
|
467
|
-
e.printStackTrace()
|
468
|
-
|
469
|
-
} catch (e: IOException) {
|
470
|
-
|
471
|
-
e.printStackTrace()
|
472
|
-
|
473
|
-
}
|
474
|
-
|
475
|
-
}
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
companion object {
|
480
|
-
|
481
|
-
const val REQUEST_CODE = 100
|
482
|
-
|
483
|
-
}
|
484
|
-
|
485
|
-
}
|
486
|
-
|
487
|
-
```
|
488
|
-
|
489
|
-
```xml
|
490
|
-
|
491
|
-
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
492
|
-
|
493
|
-
xmlns:app="http://schemas.android.com/apk/res-auto"
|
494
|
-
|
495
|
-
xmlns:tools="http://schemas.android.com/tools"
|
496
|
-
|
497
|
-
android:layout_width="match_parent"
|
498
|
-
|
499
|
-
android:layout_height="match_parent"
|
500
|
-
|
501
|
-
android:orientation="vertical">
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
<SurfaceView
|
506
|
-
|
507
|
-
android:id="@+id/surfaceView"
|
508
|
-
|
509
|
-
android:layout_width="match_parent"
|
510
|
-
|
511
|
-
android:layout_height="match_parent"
|
512
|
-
|
513
|
-
android:visibility="gone" />
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
<Button
|
518
|
-
|
519
|
-
android:id="@+id/startBtn"
|
520
|
-
|
521
|
-
android:layout_width="wrap_content"
|
522
|
-
|
523
|
-
android:layout_height="wrap_content"
|
524
|
-
|
525
|
-
android:layout_gravity="center_horizontal|bottom"
|
526
|
-
|
527
|
-
android:text="@string/take_photo"
|
528
|
-
|
529
|
-
android:visibility="gone" />
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
<TextView
|
534
|
-
|
535
|
-
android:id="@+id/showPermissionMsg"
|
536
|
-
|
537
|
-
android:layout_width="wrap_content"
|
538
|
-
|
539
|
-
android:layout_height="wrap_content"
|
540
|
-
|
541
|
-
android:layout_gravity="center"
|
542
|
-
|
543
|
-
android:padding="@dimen/show_permission_padding"
|
544
|
-
|
545
|
-
android:text="@string/permission_message"
|
546
|
-
|
547
|
-
android:textAlignment="center"
|
548
|
-
|
549
|
-
android:textStyle="bold|italic"
|
550
|
-
|
551
|
-
android:visibility="gone" />
|
552
|
-
|
553
|
-
|
554
|
-
|
555
|
-
<ImageView
|
556
|
-
|
557
|
-
android:id="@+id/ivCamera"
|
558
|
-
|
559
|
-
android:layout_width="match_parent"
|
560
|
-
|
561
|
-
android:layout_height="match_parent"
|
562
|
-
|
563
|
-
android:layout_gravity="center"
|
564
|
-
|
565
|
-
android:scaleType="center"/>
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
</FrameLayout>
|
570
|
-
|
571
|
-
```
|
572
|
-
|
573
|
-
### 試したこと
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
以前、撮った写真をただimageviewに表示するには
|
578
|
-
|
579
|
-
setImageURIを使えば解決しておりましたが、
|
580
|
-
|
581
|
-
今回はこちらを追加しても何も変化が起きませんでした。
|
582
|
-
|
583
587
|
何が足りていないのか、もしくは場所を間違えているのか
|
584
588
|
|
585
589
|
ご教示願えないでしょうか。
|