在上一篇文章中,我们已经看到了如何在Android中从库中选择图像,但是在大多数情况下,当我们在whatsapp上发布状态或在Facebook或instagram上发布帖子时,我们选择了多个图像。因此,在本文中,已逐步讨论了如何从图库中选择一个或多个图像,然后我们将看到所选图像的总数。下面提供了一个示例视频,以使您对本文中的工作有个大概的了解。注意,我们将使用Java语言实现该项目。
分步实施
步骤1:创建一个新项目
要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。
步骤2:使用AndroidManifest.xml文件
要将数据添加到Firebase,我们必须授予访问Internet的权限。要添加这些权限,请导航至应用程序> AndroidManifest.xml,然后在该文件内向其添加以下权限。
步骤3:使用activity_main.xml文件
导航到应用程序> res>布局> activity_main.xml,然后将以下代码添加到该文件中。以下是activity_main.xml文件的代码。
XML
Java
import android.content.ClipData;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
Button select, previous, next;
ImageSwitcher imageView;
int PICK_IMAGE_MULTIPLE = 1;
String imageEncoded;
TextView total;
ArrayList mArrayUri;
int position = 0;
List imagesEncodedList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
select = findViewById(R.id.select);
total = findViewById(R.id.text);
imageView = findViewById(R.id.image);
previous = findViewById(R.id.previous);
mArrayUri = new ArrayList();
// showing all images in imageswitcher
imageView.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
ImageView imageView1 = new ImageView(getApplicationContext());
return imageView1;
}
});
next = findViewById(R.id.next);
// click here to select next image
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (position < mArrayUri.size() - 1) {
// increase the position by 1
position++;
imageView.setImageURI(mArrayUri.get(position));
} else {
Toast.makeText(MainActivity.this, "Last Image Already Shown", Toast.LENGTH_SHORT).show();
}
}
});
// click here to view previous image
previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (position > 0) {
// decrease the position by 1
position--;
imageView.setImageURI(mArrayUri.get(position));
}
}
});
imageView = findViewById(R.id.image);
// click here to select image
select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// initialising intent
Intent intent = new Intent();
// setting type to select to be image
intent.setType("image/*");
// allowing multiple image to be selected
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_MULTIPLE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// When an Image is picked
if (requestCode == PICK_IMAGE_MULTIPLE && resultCode == RESULT_OK && null != data) {
// Get the Image from data
if (data.getClipData() != null) {
ClipData mClipData = data.getClipData();
int cout = data.getClipData().getItemCount();
for (int i = 0; i < cout; i++) {
// adding imageuri in array
Uri imageurl = data.getClipData().getItemAt(i).getUri();
mArrayUri.add(imageurl);
}
// setting 1st selected image into image switcher
imageView.setImageURI(mArrayUri.get(0));
position = 0;
} else {
Uri imageurl = data.getData();
mArrayUri.add(imageurl);
imageView.setImageURI(mArrayUri.get(0));
position = 0;
}
} else {
// show this if no image is selected
Toast.makeText(this, "You haven't picked Image", Toast.LENGTH_LONG).show();
}
}
}
步骤4:使用MainActivity。 Java文件
转到MainActivity。 Java文件并参考以下代码。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。
Java
import android.content.ClipData;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
Button select, previous, next;
ImageSwitcher imageView;
int PICK_IMAGE_MULTIPLE = 1;
String imageEncoded;
TextView total;
ArrayList mArrayUri;
int position = 0;
List imagesEncodedList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
select = findViewById(R.id.select);
total = findViewById(R.id.text);
imageView = findViewById(R.id.image);
previous = findViewById(R.id.previous);
mArrayUri = new ArrayList();
// showing all images in imageswitcher
imageView.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
ImageView imageView1 = new ImageView(getApplicationContext());
return imageView1;
}
});
next = findViewById(R.id.next);
// click here to select next image
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (position < mArrayUri.size() - 1) {
// increase the position by 1
position++;
imageView.setImageURI(mArrayUri.get(position));
} else {
Toast.makeText(MainActivity.this, "Last Image Already Shown", Toast.LENGTH_SHORT).show();
}
}
});
// click here to view previous image
previous.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (position > 0) {
// decrease the position by 1
position--;
imageView.setImageURI(mArrayUri.get(position));
}
}
});
imageView = findViewById(R.id.image);
// click here to select image
select.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// initialising intent
Intent intent = new Intent();
// setting type to select to be image
intent.setType("image/*");
// allowing multiple image to be selected
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_MULTIPLE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// When an Image is picked
if (requestCode == PICK_IMAGE_MULTIPLE && resultCode == RESULT_OK && null != data) {
// Get the Image from data
if (data.getClipData() != null) {
ClipData mClipData = data.getClipData();
int cout = data.getClipData().getItemCount();
for (int i = 0; i < cout; i++) {
// adding imageuri in array
Uri imageurl = data.getClipData().getItemAt(i).getUri();
mArrayUri.add(imageurl);
}
// setting 1st selected image into image switcher
imageView.setImageURI(mArrayUri.get(0));
position = 0;
} else {
Uri imageurl = data.getData();
mArrayUri.add(imageurl);
imageView.setImageURI(mArrayUri.get(0));
position = 0;
}
} else {
// show this if no image is selected
Toast.makeText(this, "You haven't picked Image", Toast.LENGTH_LONG).show();
}
}
}