前提・実現したいこと
surfaceciewでカメラpreviewを表示しているのですが、
撮った写真をBitmapでimageviewに表示したいです。
該当のソースコード
MainActivity
1import android.Manifest.permission.CAMERA 2import android.Manifest.permission.WRITE_EXTERNAL_STORAGE 3import android.app.AlertDialog 4import android.content.pm.PackageManager 5import android.graphics.BitmapFactory 6import android.hardware.Camera 7import android.net.Uri 8import android.os.Build 9import android.os.Bundle 10import android.os.Environment 11import android.util.Log 12import android.view.SurfaceHolder 13import android.view.SurfaceView 14import android.view.View 15import android.widget.Button 16import android.widget.ImageView 17import android.widget.Toast 18import androidx.appcompat.app.AppCompatActivity 19import androidx.core.app.ActivityCompat 20import androidx.core.content.ContextCompat 21import java.io.File 22import java.io.FileOutputStream 23import java.io.IOException 24import java.util.* 25 26class MainActivity : AppCompatActivity(), SurfaceHolder.Callback, Camera.PictureCallback { 27 28 private var surfaceHolder: SurfaceHolder? = null 29 private var camera: Camera? = null 30 31 private var surfaceView: SurfaceView? = null 32 33 private val neededPermissions = arrayOf(CAMERA, WRITE_EXTERNAL_STORAGE) 34 35 private var _imageUri: Uri? = null 36 37 override fun onCreate(savedInstanceState: Bundle?) { 38 super.onCreate(savedInstanceState) 39 setContentView(R.layout.activity_main) 40 41 surfaceView = findViewById(R.id.surfaceView) 42 val result = checkPermission() 43 if (result) { 44 setupSurfaceHolder() 45 } 46 } 47 48 49 50 private fun checkPermission(): Boolean { 51 val currentAPIVersion = Build.VERSION.SDK_INT 52 if (currentAPIVersion >= android.os.Build.VERSION_CODES.M) { 53 val permissionsNotGranted = ArrayList<String>() 54 for (permission in neededPermissions) { 55 if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) { 56 permissionsNotGranted.add(permission) 57 } 58 } 59 if (permissionsNotGranted.size > 0) { 60 var shouldShowAlert = false 61 for (permission in permissionsNotGranted) { 62 shouldShowAlert = ActivityCompat.shouldShowRequestPermissionRationale(this, permission) 63 } 64 65 val arr = arrayOfNulls<String>(permissionsNotGranted.size) 66 val permissions = permissionsNotGranted.toArray(arr) 67 if (shouldShowAlert) { 68 showPermissionAlert(permissions) 69 } else { 70 requestPermissions(permissions) 71 } 72 return false 73 } 74 } 75 return true 76 } 77 78 private fun showPermissionAlert(permissions: Array<String?>) { 79 val alertBuilder = AlertDialog.Builder(this) 80 alertBuilder.setCancelable(true) 81 alertBuilder.setTitle(R.string.permission_required) 82 alertBuilder.setMessage(R.string.permission_message) 83 alertBuilder.setPositiveButton(android.R.string.yes) { _, _ -> requestPermissions(permissions) } 84 val alert = alertBuilder.create() 85 alert.show() 86 } 87 88 private fun requestPermissions(permissions: Array<String?>) { 89 ActivityCompat.requestPermissions(this@MainActivity, permissions, REQUEST_CODE) 90 } 91 92 override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) { 93 when (requestCode) { 94 REQUEST_CODE -> { 95 for (result in grantResults) { 96 if (result == PackageManager.PERMISSION_DENIED) { 97 Toast.makeText(this@MainActivity, R.string.permission_warning, Toast.LENGTH_LONG).show() 98 View.VISIBLE.setViewVisibility(R.id.showPermissionMsg) 99 return 100 } 101 } 102 setupSurfaceHolder() 103 } 104 } 105 super.onRequestPermissionsResult(requestCode, permissions, grantResults) 106 } 107 108 private fun Int.setViewVisibility(id: Int) { 109 val view = findViewById<View>(id) 110 view!!.visibility = this 111 } 112 113 private fun setupSurfaceHolder() { 114 View.VISIBLE.setViewVisibility(R.id.startBtn) 115 View.VISIBLE.setViewVisibility(R.id.surfaceView) 116 117 surfaceHolder = surfaceView!!.holder 118 surfaceHolder!!.addCallback(this) 119 setBtnClick() 120 } 121 122 private fun setBtnClick() { 123 val startBtn = findViewById<Button>(R.id.startBtn) 124 startBtn?.setOnClickListener { captureImage() } 125 } 126 127 private fun captureImage() { 128 if (camera != null) { 129 camera!!.takePicture(null, null, this) 130 } 131 } 132 133 override fun surfaceCreated(surfaceHolder: SurfaceHolder) { 134 startCamera() 135 } 136 137 private fun startCamera() { 138 camera = Camera.open() 139 camera!!.setDisplayOrientation(90) 140 try { 141 camera!!.setPreviewDisplay(surfaceHolder) 142 camera!!.startPreview() 143 } catch (e: IOException) { 144 e.printStackTrace() 145 } 146 } 147 148 override fun surfaceChanged(surfaceHolder: SurfaceHolder, i: Int, i1: Int, i2: Int) { 149 resetCamera() 150 } 151 152 private fun resetCamera() { 153 if (surfaceHolder!!.surface == null) { 154 // Return if preview surface does not exist 155 return 156 } 157 158 // Stop if preview surface is already running. 159 camera!!.stopPreview() 160 try { 161 // Set preview display 162 camera!!.setPreviewDisplay(surfaceHolder) 163 } catch (e: IOException) { 164 e.printStackTrace() 165 } 166 167 // Start the camera preview... 168 camera!!.startPreview() 169 } 170 171 override fun surfaceDestroyed(surfaceHolder: SurfaceHolder) { 172 releaseCamera() 173 } 174 175 private fun releaseCamera() { 176 camera!!.stopPreview() 177 camera!!.release() 178 camera = null 179 } 180 181 override fun onPictureTaken(bytes: ByteArray, camera: Camera) { 182 saveImage(bytes) 183 resetCamera() 184 } 185 186 private fun saveImage(bytes: ByteArray) { 187 val outStream: FileOutputStream 188 189 try { 190 val fileName = "TUTORIALWING_" + System.currentTimeMillis() + ".jpg" 191 val file = File(Environment.getExternalStorageDirectory(), fileName) 192 193 194 195 outStream = FileOutputStream(file) 196 outStream.write(bytes) 197 outStream.close() 198 Toast.makeText(this@MainActivity, "Picture Saved: $fileName", Toast.LENGTH_LONG).show() 199 200 val ivCamera = findViewById<ImageView>(R.id.ivCamera) 201 println(fileName) 202 Log.i("MainActivity","fileName: $fileName" ) 203 val bmImg = BitmapFactory.decodeFile(fileName) 204 ivCamera.setImageBitmap(bmImg) 205 206 } //catch (e: FileNotFoundException) { 207// e.printStackTrace() 208// } 209 catch (e: IOException) { 210 e.printStackTrace() 211 } 212 } 213 companion object { 214 const val REQUEST_CODE = 100 215 } 216}
xml
1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:orientation="vertical"> 8 9 <FrameLayout 10 android:layout_width="match_parent" 11 android:layout_height="match_parent" 12 android:layout_weight="1"> 13 14 <SurfaceView 15 android:id="@+id/surfaceView" 16 android:layout_width="match_parent" 17 android:layout_height="match_parent" 18 android:visibility="gone" /> 19 20 <Button 21 android:id="@+id/startBtn" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:layout_gravity="center_horizontal|bottom" 25 android:text="@string/take_photo" 26 android:visibility="gone" /> 27 28 <TextView 29 android:id="@+id/showPermissionMsg" 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" 32 android:layout_gravity="center" 33 android:padding="@dimen/show_permission_padding" 34 android:text="@string/permission_message" 35 android:textAlignment="center" 36 android:textStyle="bold|italic" 37 android:visibility="gone" /> 38 </FrameLayout> 39 40 <FrameLayout 41 android:layout_width="match_parent" 42 android:layout_height="match_parent" 43 android:layout_weight="1"> 44 45 <ImageView 46 android:id="@+id/ivCamera" 47 android:layout_width="match_parent" 48 android:layout_height="match_parent" 49 android:layout_gravity="center" 50 android:scaleType="center" /> 51 </FrameLayout> 52 53</LinearLayout>
試したこと
val bmImg = BitmapFactory.decodeFile(fileName) ivCamera.setImageBitmap(bmImg)
上記のようにBitmapを使ってみたのですが、
写真を撮っても画像は表示されませんでした。
原因がわかる方いましたらご教示ください。
回答1件
あなたの回答
tips
プレビュー