Project 6: Gallery Open Activity in android studio

 This example demonstrates how do I pick an image from image gallery in android app

Steps − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

step 1:-

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:orientation="vertical"
android:gravity="center_horizontal"
android:layout_marginEnd="30dp"
android:layout_marginStart="30dp"
android:layout_height="match_parent"
tools:context=".MainActivity">

<androidx.cardview.widget.CardView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="20dp"
app:cardCornerRadius="50dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imgGallery"
android:src="@drawable/edtbg"
android:scaleType="fitXY"/>
<ImageView
android:layout_width="20dp"
android:src="@drawable/ic_baseline_add_a_photo_24"
android:layout_height="20dp"
android:layout_margin="15dp"
android:id="@+id/imgOpenGallery"
android:layout_gravity="end|bottom"/>
</androidx.cardview.widget.CardView>
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="20dp"
android:background="@drawable/edtbg"
android:id="@+id/edtName"
android:paddingStart="10dp"
android:hint="Enter Name"/>
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@drawable/edtbg"
android:layout_marginTop="5dp"
android:id="@+id/edtEmail"
android:paddingStart="10dp"
android:hint="Enter Email"/>
<androidx.appcompat.widget.AppCompatButton
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Save"
android:textColor="@color/white"
android:backgroundTint="#FF5722"
android:id="@+id/btnSave"/>


</LinearLayout>

step 2:-

MainActivity.java

package com.sandhya.galleryopen;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

ImageView imgGallery,imgOpenGallery;
private static final int GALLERY_REQ_CODE = 200;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

imgGallery = findViewById(R.id.imgGallery);
imgOpenGallery = findViewById(R.id.imgOpenGallery);
imgOpenGallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

//for gallery
Intent iGallery = new Intent(Intent.ACTION_PICK);
iGallery.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(iGallery,GALLERY_REQ_CODE);
}
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode,Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode==RESULT_OK){
if (requestCode==GALLERY_REQ_CODE){
imgGallery.setImageURI(data.getData());
}
}
}
}

output:-




Post a Comment

Post a Comment (0)

Previous Post Next Post