AndroidStudio (kotlin) 버튼을 눌러 갤러리를 열고 사진을 여러 개 선택하여 뷰에 표시
14729 단어 AndroidStudioKotlin
하고 싶은 일
화면 구현
app\src\main\res\layout\activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:text="フォトギャラリーを開く"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="72dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn"
tools:srcCompat="@tools:sample/avatars" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="72dp"
app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toBottomOf="@+id/btn"
tools:srcCompat="@tools:sample/avatars" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="32dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView"
tools:srcCompat="@tools:sample/avatars" />
</androidx.constraintlayout.widget.ConstraintLayout>
메인 프로세싱 구현
MainActivity.kt
package com.example.yamato200604a
import android.content.Intent
import android.graphics.BitmapFactory
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// ボタン押下でフォトギャラリーを開く。
val getButton = findViewById(R.id.btn) as Button
getButton.setOnClickListener(object : View.OnClickListener {
override
fun onClick(view: View) {
openPhotoGallery()
}
})
}
/**
* Android端末のフォトギャラリー(写真を保存している内部フォルダ)を開く。
* @author 大和賢一郎
*/
private fun openPhotoGallery() {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.type = "image/*"
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
intent.action = Intent.ACTION_PICK
startActivityForResult(Intent.createChooser(intent, "Choose Photo"), CHOOSE_PHOTO_REQUEST_CODE)
}
companion object {
private const val CHOOSE_PHOTO_REQUEST_CODE: Int = 43
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == CHOOSE_PHOTO_REQUEST_CODE && resultCode == RESULT_OK) {
val itemCount = data?.clipData?.itemCount ?: 0
val uriList = mutableListOf<Uri>()
for (i in 0..itemCount - 1) {
val uri = data?.clipData?.getItemAt(i)?.uri
uri?.let { uriList.add(it) }
}
val inputStream = contentResolver?.openInputStream(uriList[0])
val image = BitmapFactory.decodeStream(inputStream)
imageView.setImageBitmap(image)
val inputStream2 = contentResolver?.openInputStream(uriList[1])
val image2 = BitmapFactory.decodeStream(inputStream2)
imageView2.setImageBitmap(image2)
val inputStream3 = contentResolver?.openInputStream(uriList[2])
val image3 = BitmapFactory.decodeStream(inputStream3)
imageView3.setImageBitmap(image3)
}
}
}
Reference
이 문제에 관하여(AndroidStudio (kotlin) 버튼을 눌러 갤러리를 열고 사진을 여러 개 선택하여 뷰에 표시), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/kenichiro-yamato/items/23609cce946bee7270ac
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:text="フォトギャラリーを開く"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="72dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btn"
tools:srcCompat="@tools:sample/avatars" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginStart="32dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="72dp"
app:layout_constraintStart_toEndOf="@+id/imageView"
app:layout_constraintTop_toBottomOf="@+id/btn"
tools:srcCompat="@tools:sample/avatars" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="130dp"
android:layout_height="130dp"
android:layout_marginTop="32dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView"
tools:srcCompat="@tools:sample/avatars" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
package com.example.yamato200604a
import android.content.Intent
import android.graphics.BitmapFactory
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// ボタン押下でフォトギャラリーを開く。
val getButton = findViewById(R.id.btn) as Button
getButton.setOnClickListener(object : View.OnClickListener {
override
fun onClick(view: View) {
openPhotoGallery()
}
})
}
/**
* Android端末のフォトギャラリー(写真を保存している内部フォルダ)を開く。
* @author 大和賢一郎
*/
private fun openPhotoGallery() {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.type = "image/*"
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true)
intent.action = Intent.ACTION_PICK
startActivityForResult(Intent.createChooser(intent, "Choose Photo"), CHOOSE_PHOTO_REQUEST_CODE)
}
companion object {
private const val CHOOSE_PHOTO_REQUEST_CODE: Int = 43
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == CHOOSE_PHOTO_REQUEST_CODE && resultCode == RESULT_OK) {
val itemCount = data?.clipData?.itemCount ?: 0
val uriList = mutableListOf<Uri>()
for (i in 0..itemCount - 1) {
val uri = data?.clipData?.getItemAt(i)?.uri
uri?.let { uriList.add(it) }
}
val inputStream = contentResolver?.openInputStream(uriList[0])
val image = BitmapFactory.decodeStream(inputStream)
imageView.setImageBitmap(image)
val inputStream2 = contentResolver?.openInputStream(uriList[1])
val image2 = BitmapFactory.decodeStream(inputStream2)
imageView2.setImageBitmap(image2)
val inputStream3 = contentResolver?.openInputStream(uriList[2])
val image3 = BitmapFactory.decodeStream(inputStream3)
imageView3.setImageBitmap(image3)
}
}
}
Reference
이 문제에 관하여(AndroidStudio (kotlin) 버튼을 눌러 갤러리를 열고 사진을 여러 개 선택하여 뷰에 표시), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/kenichiro-yamato/items/23609cce946bee7270ac텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)