Skip to content

Commit 11871b1

Browse files
updated pick multiple files
1 parent 8229f5d commit 11871b1

5 files changed

Lines changed: 7 additions & 48 deletions

File tree

CaptureAndPickImageAndroid/.idea/misc.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CaptureAndPickImageAndroid/app/src/main/AndroidManifest.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.example.captureandpickimageandroid">
44

5-
<uses-permission android:name="android.permission.CAMERA"/>
6-
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
75

86
<application
97
android:allowBackup="true"

CaptureAndPickImageAndroid/app/src/main/java/com/example/captureandpickimageandroid/ImageAdapter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ImageAdapter : RecyclerView.Adapter<ImageViewHolder>() {
2020
override fun onBindViewHolder(holder: ImageViewHolder, position: Int) {
2121
val imagePath = selectedImagePath[position]
2222
holder.image.setImageBitmap(BitmapFactory.decodeFile(imagePath))
23-
holder.filename.text = imagePath
23+
holder.filename.text = imagePath.split("/").last().toString()
2424
}
2525

2626
override fun getItemCount(): Int {

CaptureAndPickImageAndroid/app/src/main/java/com/example/captureandpickimageandroid/MainActivity.kt

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,27 @@
11
package com.example.captureandpickimageandroid
22

3-
import android.Manifest
43
import android.app.Activity
4+
import android.content.ContentResolver
55
import android.content.Context
66
import android.content.Intent
77
import android.content.Intent.ACTION_GET_CONTENT
8-
import android.content.pm.PackageManager
98
import android.net.Uri
109
import android.os.Bundle
11-
import android.provider.MediaStore
1210
import android.util.Log
11+
import android.webkit.MimeTypeMap
1312
import android.widget.Button
1413
import androidx.activity.result.contract.ActivityResultContracts
1514
import androidx.appcompat.app.AppCompatActivity
1615
import androidx.recyclerview.widget.RecyclerView
1716
import org.apache.commons.io.FileUtils
1817
import java.io.File
19-
import android.webkit.MimeTypeMap
20-
21-
import android.content.ContentResolver
22-
import android.provider.MediaStore.Images
23-
24-
import android.graphics.Bitmap
2518

2619

2720
class MainActivity : AppCompatActivity() {
2821

29-
lateinit var btnCaptureImage: Button
3022
lateinit var btnSelectImages: Button
3123
lateinit var rvImages: RecyclerView
3224

33-
private val cameraPermissionCode = 201
34-
private val cameraCaptureRequest = 301
3525
lateinit var imageAdapter: ImageAdapter
3626
var selectedPaths = mutableListOf<String>()
3727

@@ -40,26 +30,17 @@ class MainActivity : AppCompatActivity() {
4030
super.onCreate(savedInstanceState)
4131
setContentView(R.layout.activity_main)
4232

43-
btnCaptureImage = findViewById(R.id.btnCaptureImage)
4433
btnSelectImages = findViewById(R.id.btnSelectImages)
4534
rvImages = findViewById(R.id.rvImages)
4635
imageAdapter = ImageAdapter()
4736
rvImages.adapter = imageAdapter
4837

49-
val captureImageActivityResult =
50-
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
51-
if (result.resultCode == Activity.RESULT_OK) {
52-
val data: Intent? = result.data
53-
54-
}
55-
}
5638

5739
val selectImagesActivityResult =
5840
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
5941
if (result.resultCode == Activity.RESULT_OK) {
6042
val data: Intent? = result.data
6143
//If multiple image selected
62-
6344
if (data?.clipData != null) {
6445
val count = data.clipData?.itemCount ?: 0
6546

@@ -84,11 +65,6 @@ class MainActivity : AppCompatActivity() {
8465
}
8566
}
8667

87-
btnCaptureImage.setOnClickListener {
88-
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
89-
captureImageActivityResult.launch(cameraIntent)
90-
}
91-
9268
btnSelectImages.setOnClickListener {
9369

9470
val intent = Intent(ACTION_GET_CONTENT)
@@ -101,7 +77,6 @@ class MainActivity : AppCompatActivity() {
10177
} catch (e: Exception) {
10278

10379
}
104-
checkCameraPermission()
10580

10681
}
10782

@@ -160,11 +135,4 @@ class MainActivity : AppCompatActivity() {
160135
return file.delete()
161136
}
162137

163-
164-
private fun checkCameraPermission() {
165-
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
166-
requestPermissions(arrayOf(Manifest.permission.CAMERA), cameraPermissionCode)
167-
}
168-
}
169-
170138
}

CaptureAndPickImageAndroid/app/src/main/res/layout/activity_main.xml

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,13 @@
88
android:orientation="vertical"
99
android:gravity="top">
1010

11-
<androidx.appcompat.widget.AppCompatButton
12-
android:id="@+id/btnCaptureImage"
13-
android:layout_width="200dp"
14-
android:layout_height="wrap_content"
15-
android:text="Capture Image"
16-
android:layout_gravity="center"
17-
app:layout_constraintBottom_toBottomOf="parent"
18-
app:layout_constraintLeft_toLeftOf="parent"
19-
app:layout_constraintRight_toRightOf="parent"
20-
app:layout_constraintTop_toTopOf="parent" />
11+
2112

2213
<androidx.appcompat.widget.AppCompatButton
2314
android:id="@+id/btnSelectImages"
2415
android:layout_width="200dp"
2516
android:layout_height="wrap_content"
26-
android:text="Select Images"
17+
android:text="Select Files"
2718
android:layout_gravity="center"
2819
app:layout_constraintBottom_toBottomOf="parent"
2920
app:layout_constraintLeft_toLeftOf="parent"

0 commit comments

Comments
 (0)