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 Kotlin
activity_main.xml
MainActivity.java
Check for Kotlin
Video:
Step 1: Create a new project OR Open your 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_java"><!--//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 on which image will be set--><ImageViewandroid:id="@+id/image_view"android:src="@drawable/ic_image_black_24dp"android:scaleType="centerCrop"android:layout_width="400dp"android:layout_height="400dp" /><!--button to choose image--><Buttonandroid:id="@+id/choose_image_btn"android:text="Choose Image"style="@style/Base.Widget.AppCompat.Button.Colored"android:layout_width="match_parent"android:layout_height="wrap_content" /></LinearLayout>
MainActivity.java
package com.blogspot.atifsoftwares.imagepick_java;import android.Manifest;import android.content.Intent;import android.content.pm.PackageManager;import android.os.Build;import android.support.annotation.NonNull;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.ImageView;import android.widget.Toast;public class MainActivity extends AppCompatActivity {ImageView mImageView;Button mChooseBtn;private static final int IMAGE_PICK_CODE = 1000;private static final int PERMISSION_CODE = 1001;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//VIEWSmImageView = findViewById(R.id.image_view);mChooseBtn = findViewById(R.id.choose_image_btn);//handle button clickmChooseBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//check runtime permissionif (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)== PackageManager.PERMISSION_DENIED){//permission not granted , request it.String[] permissions = {Manifest.permission.READ_EXTERNAL_STORAGE};//show popup for runtime permissionrequestPermissions(permissions , PERMISSION_CODE);}else {//permission already grantedpickImageFromGallery();}}else {//system os is less then marshmallowpickImageFromGallery();}}});}private void pickImageFromGallery() {//intent to pick imageIntent intent = new Intent(Intent.ACTION_PICK);intent.setType("image/*");startActivityForResult(intent , IMAGE_PICK_CODE);}//handle result of runtime permission@Overridepublic void onRequestPermissionsResult(int requestCode , @NonNull String[] permissions , @NonNull int[] grantResults) {switch (requestCode){case PERMISSION_CODE:{if (grantResults.length >0 && grantResults[0] ==PackageManager.PERMISSION_GRANTED){//permission was grantedpickImageFromGallery();}else {//permission was deniedToast.makeText(this , "Permission denied...!" , Toast.LENGTH_SHORT).show();}}}}//handle result of picked image@Overrideprotected void onActivityResult(int requestCode , int resultCode , Intent data) {if (resultCode == RESULT_OK && requestCode == IMAGE_PICK_CODE){//set image to image viewmImageView.setImageURI(data.getData());}}}
0 Response to "Pick An Image From The Gallery – Android Studio - Java"