开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第19天,点击查看活动详情
三、从相册中选择图片
虽然调用摄像头拍照即方便又快捷,但我们并不是每次都需要当场拍一张照片的,因为每个人的手机相册里应该都会存有许多张图片,直接从相册里选取一张现有的图片会比打开相机拍一张照片更有用。
步骤
(1)在布局文件中加入Button:fromAlbumBtn
<Button
android:id="@+id/fromAlbumBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="From Album"/>
(2)初始化fromAlbum
val fromAlbum = 2
(3)在按键fromAlbumBtn的点击事件中,打开文件选择器,显示图片
val fromAlbumBtn: Button = findViewById(R.id.fromAlbumBtn)
fromAlbumBtn.setOnClickListener {
//打开文件选择器
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
//指定只显示图片
intent.type = "image/*"
startActivityForResult(intent, fromAlbum)
}
(4)在onActivityResult()函数中,将选择的图片显示出来
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
val imageView: ImageView = findViewById(R.id.imageView)
when (requestCode) {
...
fromAlbum -> {
if (resultCode == RESULT_OK && data != null) {
data.data?.let { uri ->
//将选择的图片显示
val bitmap = getBitmapFromUri(uri)
imageView.setImageBitmap(bitmap)
}
}
}
}
}
(5)将Uri转换成Bitmap
private fun getBitmapFromUri(uri: Uri) =
contentResolver.openFileDescriptor(uri, "r")?.use{
BitmapFactory.decodeFileDescriptor(it.fileDescriptor)
}
四、播放多媒体文件
Android在播放音频和视频方面做了一个相当不错的支持,它提供了一套较为完整的API,使得开发者可以很轻松地编写一个简易地音频或者视频播放器。
1、播放音频
步骤
(1)创建一个MediaPlayer的实例
private val mediaPlayer=MediaPlayer()
(2)为MediaPlayer对象进行初始化操作
**** 通过getAssets()方法得到一个AssetManager的实例,AssetManager可用于读取assets目录下的任何资源,接着调用openFd()方法将音频文件句柄打开,后来调用setDataSource()方法设置要播放的音频文件的位置和prepare()方法完成准备工作。
private fun initMediaPlayer() {
val assetManager=assets
val fd=assetManager.openFd("music.mp3")
mediaPlayer.setDataSource(fd.fileDescriptor,fd.startOffset,fd.length)
mediaPlayer.prepare()
}
(3)设置三个按键的点击事件
val play:Button=findViewById(R.id.play)
val pause:Button=findViewById(R.id.pause)
val stop:Button=findViewById(R.id.stop)
play.setOnClickListener {
if(!mediaPlayer.isPlaying)
mediaPlayer.start()
}
pause.setOnClickListener {
if(mediaPlayer.isPlaying){
mediaPlayer.pause()
}
}
stop.setOnClickListener {
if(mediaPlayer.isPlaying){
mediaPlayer.reset()
initMediaPlayer()
}
}
(4)最后在onDestroy()方法中,我们需要调用stop()方法和release()方法将MediaPlayer相关的资源释放掉。
override fun onDestroy() {
super.onDestroy()
mediaPlayer.stop()
mediaPlayer.release()
}
2、播放视频
步骤
(1)在布局文件中放置三个按钮,分别控制视频的播放,暂停,和重新播放,在按钮下面放置视频VideoView。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/play"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="play"/>
<Button
android:id="@+id/pause"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="pause"/>
<Button
android:id="@+id/replay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="replay"/>
</LinearLayout>
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="videoView"/>
(2)点击res->New->Directory,然后输入raw,把视频资源放在这。
(3)初始化VideoView
val uri=Uri.parse("android.resource://$packageName/${R.raw.video}")
videoView.setVideoURI(uri)
(4)设置三个按键的点击事件
play.setOnClickListener {
if(!videoView.isPlaying)
videoView.start()
}
pause.setOnClickListener {
if(videoView.isPlaying){
videoView.pause()
}
}
replay.setOnClickListener {
if(videoView.isPlaying){
videoView.resume()
}
}
(5)在onDestroy()方法中,需要调用suspend()方法,将VideoView所占用的资源释放掉。
override fun onDestroy() {
super.onDestroy()
videoView.suspend()
}