Skip to content

Commit

Permalink
add gallery get data
Browse files Browse the repository at this point in the history
  • Loading branch information
AlfredoHdez1709 committed Jul 25, 2022
1 parent dd229b5 commit de317cd
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.ahrsoft.easycameraandgallery

import android.annotation.SuppressLint
import android.app.Activity
import android.content.ContentUris
import android.content.ContentValues
import android.content.Intent
Expand All @@ -13,6 +14,7 @@ import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.provider.DocumentsContract
import android.provider.MediaStore
import android.util.DisplayMetrics
import android.util.Log
Expand All @@ -21,6 +23,8 @@ import android.view.View
import android.view.WindowInsets
import android.webkit.MimeTypeMap
import android.widget.Toast
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.camera.core.*
import androidx.camera.lifecycle.ProcessCameraProvider
Expand Down Expand Up @@ -62,6 +66,8 @@ class CameraActivity : AppCompatActivity() {
private val imageList = arrayListOf<ImageModel>()
private val imageSelected = arrayListOf<ImageModel>()
private lateinit var adapter: GalleryAdapter
private lateinit var resultGallery: ActivityResultLauncher<Intent>


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -83,6 +89,9 @@ class CameraActivity : AppCompatActivity() {
flashModeOptions(optionsCamera.flash)

with(binding) {
galleryCaptureButton.setOnClickListener {
getPickImageIntent()
}
cameraCaptureButton.setOnClickListener {
takePhoto()
}
Expand Down Expand Up @@ -121,6 +130,31 @@ class CameraActivity : AppCompatActivity() {
getListPath(list as ArrayList<String>)
}
}
openCallback()
}

private fun openCallback() {
resultGallery =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val data = result.data
if (data != null) {
if (data.clipData != null) {
val mClipData = data.clipData
for (i in 0 until mClipData!!.itemCount) {
val item = mClipData.getItemAt(i)
val uri = item.uri
getPathFromURI(uri)
}
} else if (data.data != null) {
val uri = data.data
if (uri != null) {
getPathFromURI(uri)
}
}
}
}
}
}

private fun caseFlashMode(){
Expand All @@ -146,6 +180,46 @@ class CameraActivity : AppCompatActivity() {
}
}

private fun getPathFromURI(uri: Uri) {
var realPath = String()
uri.path?.let { path ->
val databaseUri: Uri
val selection: String?
val selectionArgs: Array<String>?
if (path.contains("/document/image:")) {
databaseUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
selection = "_id=?"
selectionArgs = arrayOf(DocumentsContract.getDocumentId(uri).split(":")[1])
} else {
databaseUri = uri
selection = null
selectionArgs = null
}
try {
val column = "_data"
val projection = arrayOf(column)
val cursor = contentResolver.query(
databaseUri,
projection,
selection,
selectionArgs,
null
)
cursor?.let {
if (it.moveToFirst()) {
val columnIndex = cursor.getColumnIndexOrThrow(column)
realPath = cursor.getString(columnIndex)
}
cursor.close()
}
} catch (e: Exception) {
println(e)
}
}
addImage(realPath)
}


private fun startCamera() {
val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
cameraProviderFuture.addListener({
Expand All @@ -168,9 +242,11 @@ class CameraActivity : AppCompatActivity() {
if (isCompleteSelect){
binding.fabSendData.visibility = View.VISIBLE
binding.cameraCaptureButton.visibility = View.GONE
binding.galleryCaptureButton.visibility = View.GONE
}else{
binding.fabSendData.visibility = View.GONE
binding.cameraCaptureButton.visibility = View.VISIBLE
binding.galleryCaptureButton.visibility = View.VISIBLE
}
}

Expand Down Expand Up @@ -468,6 +544,13 @@ class CameraActivity : AppCompatActivity() {
return if (path.exists()) path else filesDir
}

fun getPickImageIntent() {
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = "image/*"
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false)
resultGallery.launch(intent)
}

private fun getListPath(list: ArrayList<String>) {
val result = Intent()
result.putExtra(IMAGE_RESULTS, list)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M21,19V5c0,-1.1 -0.9,-2 -2,-2H5c-1.1,0 -2,0.9 -2,2v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2zM8.5,13.5l2.5,3.01L14.5,12l4.5,6H5l3.5,-4.5z"/>
</vector>
21 changes: 17 additions & 4 deletions EasyCameraAndGallery/src/main/res/layout/activity_camera.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,13 @@
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginBottom="16dp"
android:elevation="9dp"
app:layout_constraintBottom_toTopOf="@+id/camera.capture.button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:listitem="@layout/image_picker_list"
tools:itemCount="8"
tools:orientation="horizontal"
android:elevation="9dp"
/>
tools:listitem="@layout/image_picker_list"
tools:orientation="horizontal" />

<ImageButton
android:id="@+id/camera.capture.button"
Expand All @@ -81,6 +80,20 @@
android:contentDescription="@string/tomar_foto"
android:elevation="9dp"/>


<ImageButton
android:id="@+id/gallery.capture.button"
android:layout_width="@dimen/round_button_medium"
android:layout_height="@dimen/round_button_medium"
android:layout_marginRight="24dp"
android:background="@drawable/ic_baseline_image_24"
android:contentDescription="@string/tomar_foto"
android:elevation="9dp"
android:scaleType="fitCenter"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/rv.gallery" />

<View
android:layout_width="match_parent"
android:layout_height="0dp"
Expand Down
1 change: 1 addition & 0 deletions EasyCameraAndGallery/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<resources>
<dimen name="round_button_large">60dp</dimen>
<dimen name="shutter_button_margin">25dp</dimen>
<dimen name="round_button_medium">40dp</dimen>
</resources>
3 changes: 2 additions & 1 deletion app/src/main/java/dev/ahrsoft/cameralibrary/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class MainActivity : AppCompatActivity() {
val optionsCamera = OptionsCamera(
ratio = Ratio.RATIO_16_9,
path = "Evidences",
flash = Flash.Off
flash = Flash.Off,
count = 2
)

binding.textInput.setOnClickListener {
Expand Down

0 comments on commit de317cd

Please sign in to comment.