Created
November 21, 2023 11:02
-
-
Save amoozeshbebin/7ba11ed21b4775fa99d01f5d87d9dcaf to your computer and use it in GitHub Desktop.
Gallery Permission
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Manifest:: | |
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> | |
MainActivity: | |
package com.iliyadev.gallerypermission | |
import android.app.Activity | |
import android.content.Intent | |
import android.graphics.Bitmap | |
import android.os.Bundle | |
import android.provider.MediaStore | |
import android.view.View | |
import android.widget.Button | |
import android.widget.ImageView | |
class MainActivity : Activity() { | |
private val PICK_IMAGE_REQUEST = 1 | |
private lateinit var imageView: ImageView | |
private lateinit var openGalleryButton: Button | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
imageView = findViewById(R.id.imageView) | |
openGalleryButton = findViewById(R.id.openGalleryButton) | |
openGalleryButton.setOnClickListener { | |
openGallery() | |
} | |
} | |
private fun openGallery() { | |
val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI) | |
startActivityForResult(intent, PICK_IMAGE_REQUEST) | |
} | |
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | |
super.onActivityResult(requestCode, resultCode, data) | |
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null) { | |
val imageUri = data.data | |
val bitmap: Bitmap = MediaStore.Images.Media.getBitmap(contentResolver, imageUri) | |
imageView.setImageBitmap(bitmap) | |
} | |
} | |
} | |
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/openGalleryButton" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerInParent="true" | |
android:text="Open Gallery" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" | |
app:layout_constraintVertical_bias="0.499" /> | |
<ImageView | |
android:id="@+id/imageView" | |
android:layout_width="0dp" | |
android:layout_height="0dp" | |
android:layout_above="@id/openGalleryButton" | |
android:scaleType="fitCenter" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment