We will solve this problem step by step
Step 1.Create program
First of all, to create a project, first of all, after opening Android Studio, we will create a project.After creating the project we will see two files activity_main.xml and MainActivity.java
Step 2.
After the project is created, we will see the activity_main .xml, in which the image we will pick up will show the image in it.So first we will paste the below code in activity_main.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/pickUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pick"
android:background="@color/purple_200"
android:layout_gravity="center"
android:layout_marginTop="5dp"/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:textSize="18sp"
android:textStyle="bold"
android:textColor="@color/black"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:layout_marginTop="5dp"/>
</LinearLayout>
In activity_main we have a button a TextView and in the last we have used recyclerview to show an image according to the list.After this we will perform the operation on the button textView RecyclerView in MainActivity.javaMainActivity.java
package com.example.multiplefiles;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
TextView textView;
Button pick;
ArrayList<Uri>uri=new ArrayList<>();
PictureAdapter recyclerAdapter;
private static final int Read_permission=101;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView=findViewById(R.id.recyclerView);
textView=findViewById(R.id.text);
pick=findViewById(R.id.pickUp);
recyclerAdapter=new PictureAdapter(uri);
recyclerView.setLayoutManager(new GridLayoutManager(MainActivity.this,4));
recyclerView.setAdapter(recyclerAdapter);
if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity.this,new String [] {Manifest.permission.READ_EXTERNAL_STORAGE},Read_permission );
}
pick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent =new Intent();
intent.setType("image/*");
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN_MR2)
{
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true);
}
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"),1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==1 && resultCode== Activity.RESULT_OK)
{
if (data.getClipData()!=null)
{
int x=data.getClipData().getItemCount();
for (int i=0;i<x;i++)
{
uri.add(data.getClipData().getItemAt(i).getUri());
}
recyclerAdapter.notifyDataSetChanged();
textView.setText("Photos("+uri.size()+")");
}
else if (data.getData()!=null)
{
String imageUrl=data.getData().getPath();
uri.add(Uri.parse(imageUrl));
}
}
}
}
After pasting this code we will see many error To fix this error, after pressing alt+enter, the error will be fixed but here we will see an error named PictureAdapter.To fix this, we will create a class named PictureAdapter.whose code is given below
PictureAdapter.java
package com.example.multiplefiles;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class PictureAdapter extends RecyclerView.Adapter<PictureAdapter.ViewHolder> {
private ArrayList<Uri> array;
public PictureAdapter(ArrayList<Uri> array) {
this.array = array;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflter=LayoutInflater.from(parent.getContext());
View view=inflter.inflate(R.layout.item,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.imageView.setImageURI(array.get(position));
}
@Override
public int getItemCount() {
return array.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView imageView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
imageView=itemView.findViewById(R.id.imageView);
}
}
}
item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="1dp">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scaleType="centerCrop"
app:layout_constraintDimensionRatio="1"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
Manifest<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.multiplefiles">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MultipleFiles">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Thank you I hope you like this article.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.multiplefiles">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MultipleFiles">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Thank you I hope you like this article.
0 Comments
Please do not enter any spam link in the comment box
Emoji