Latest News

Pick An Image From The Gallery – Android Studio - Kotlin

In this video we will pick image from Gallery on Button click and set that image to an ImageView.
For this we will also handle Runtime Permission for READ_EXTERNAL_STORAGE if system OS is Marshmallow or above.
>>Check For Java

VIDEO:


Step 1: Create a new Project or open new project

Step 2: Code

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.blogspot.atifsoftwares.imagepick_kotlin"><!--Adding Read External Storage Permission--><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme"><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout 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"android:orientation="vertical"android:gravity="center_horizontal"tools:context=".MainActivity"><!--ImageView where image will be set--><ImageViewandroid:id="@+id/image_view"android:scaleType="centerCrop"android:src="@drawable/ic_image_black_24dp"android:layout_width="400dp"android:layout_height="400dp" /><!--Button to pick image--><Buttonandroid:id="@+id/img_pick_btn"android:text="Choose Image"style="@style/Base.Widget.AppCompat.Button.Colored"android:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout>

MainActivity.kt

package com.blogspot.atifsoftwares.imagepick_kotlinimport android.Manifestimport android.app.Activityimport android.content.Intentimport android.content.pm.PackageManagerimport android.os.Buildimport android.os.Build.*import android.support.v7.app.AppCompatActivityimport android.os.Bundleimport android.widget.Toastimport kotlinx.android.synthetic.main.activity_main.*class MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)//BUTTON CLICKimg_pick_btn.setOnClickListener {//check runtime permissionif (VERSION.SDK_INT >= VERSION_CODES.M){if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) ==PackageManager.PERMISSION_DENIED){//permission deniedval permissions = arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE);//show popup to request runtime permissionrequestPermissions(permissions , PERMISSION_CODE);}else{//permission already grantedpickImageFromGallery();}}else{//system OS is < MarshmallowpickImageFromGallery();}}}private fun pickImageFromGallery() {//Intent to pick imageval intent = Intent(Intent.ACTION_PICK)intent.type = "image/*"startActivityForResult(intent , IMAGE_PICK_CODE)}companion object {//image pick codeprivate val IMAGE_PICK_CODE = 1000;//Permission codeprivate val PERMISSION_CODE = 1001;}//handle requested permission resultoverride fun onRequestPermissionsResult(requestCode: Int , permissions: Array<out String> , grantResults: IntArray) {when(requestCode){PERMISSION_CODE -> {if (grantResults.size >0 && grantResults[0] ==PackageManager.PERMISSION_GRANTED){//permission from popup grantedpickImageFromGallery()}else{//permission from popup deniedToast.makeText(this , "Permission denied" , Toast.LENGTH_SHORT).show()}}}}//handle result of picked imageoverride fun onActivityResult(requestCode: Int , resultCode: Int , data: Intent?) {if (resultCode == Activity.RESULT_OK && requestCode == IMAGE_PICK_CODE){image_view.setImageURI(data?.data)}}}

Step 3: Run Project

Output
Pick an Image from the Gallery – Android Studio - Kotlin

0 Response to "Pick An Image From The Gallery – Android Studio - Kotlin"