AndroidStudioを用い、kotlinでディスプレイ全体のスクリーンショットを取るアプリを作っています。
画面のキャストの許可を取ることはできたのですが、その先でつまづいています。
mediaProjectionManagerからmediaProjectionを作る
↓
imageReaderを作る
↓
virtualDisplayでimageReader.surfaceをサーフィスに指定
これでimageReader.acquireLatestImage()を行うとImageオブジェクトが取り出せるはずだと思うのですが、Logではnullだと返ってきます。なにが原因でしょうか?
画面のサイズは既知である自前のスマホの数値を入れています。(AVDも実機と同じ環境)
imageReaderとvirtualDisplayのLogではnullが表示されませんでした。
kotlin
1 private lateinit var mediaProjectionManager: MediaProjectionManager 2 3 override fun onCreate(savedInstanceState: Bundle?) { 4 super.onCreate(savedInstanceState) 5 setContentView(R.layout.activity_main) 6 mediaProjectionManager = getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager 7 startActivityForResult(mediaProjectionManager.createScreenCaptureIntent(), 1) 8 } 9 10 override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { 11 super.onActivityResult(requestCode, resultCode, data)) 12 if (requestCode == 1) { 13 if (resultCode == Activity.RESULT_OK) { 14 val mediaProjection = mediaProjectionManager.getMediaProjection(resultCode, data) 15 16 val widthPixels: Int = 1080 17 val heightPixels: Int = 1776 18 val dpi: Int = 480 19 20 val imageReader = ImageReader.newInstance(widthPixels, heightPixels, PixelFormat.RGB_888, 2) 21 if (imageReader == null) { 22 Log.d(TAG, "imageReader == null") 23 } 24 25 val virtualDisplay: VirtualDisplay = mediaProjection.createVirtualDisplay("Capturing Display", widthPixels, heightPixels, dpi, 26 DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, 27 imageReader.surface, null, null) 28 if (virtualDisplay == null){ 29 Log.d(TAG, "virtualDisplay == null") 30 } 31 32 if (imageReader.acquireLatestImage() == null) { 33 Log.d(TAG, "imageReader.acquireLatestImage() == null") 34 } 35 36 imageReader.close() 37 virtualDisplay.release() 38 } 39 } 40 }else{ 41 Log.d(TAG, "requestCode != 1") 42 }

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/13 11:29
2017/09/13 12:30