给我一个android stdio的输入多张图片的代码
时间: 2023-06-25 20:03:48 浏览: 106
android 使用OkHttp上传多张图片的实现代码
以下是一个简单的 Android Studio 代码示例,用于从设备的相册中选择多个图像,并将其显示在屏幕上:
1. 添加以下依赖项到 build.gradle 文件中:
```
dependencies {
implementation 'com.github.bumptech.glide:glide:4.12.0'
implementation 'com.github.bumptech.glide:annotations:4.12.0'
implementation 'com.github.bumptech.glide:okhttp3-integration:4.12.0'
implementation 'com.github.bumptech.glide:recyclerview-integration:4.12.0'
implementation 'com.github.bumptech.glide:gifdecoder:4.12.0'
implementation 'com.github.bumptech.glide:webpdecoder:4.12.0'
implementation 'com.github.bumptech.glide:compiler:4.12.0'
}
```
2. 在布局文件中添加一个 RecyclerView:
```
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/imageRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
3. 创建一个 ImageAdapter 类来管理和显示图像:
```
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder> {
private List<Uri> mImages;
public ImageAdapter(List<Uri> images) {
mImages = images;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.image_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Glide.with(holder.itemView.getContext())
.load(mImages.get(position))
.into(holder.imageView);
}
@Override
public int getItemCount() {
return mImages.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ImageView imageView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
}
}
}
```
4. 在 Activity 中添加以下代码来处理选取多个图像的请求:
```
private static final int REQUEST_CODE_SELECT_IMAGES = 1;
private List<Uri> mSelectedImages = new ArrayList<>();
private ImageAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.imageRecyclerView);
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
mAdapter = new ImageAdapter(mSelectedImages);
recyclerView.setAdapter(mAdapter);
Button selectButton = findViewById(R.id.selectButton);
selectButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Select Images"), REQUEST_CODE_SELECT_IMAGES);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_SELECT_IMAGES && resultCode == RESULT_OK) {
if (data != null) {
ClipData clipData = data.getClipData();
if (clipData != null) {
for (int i = 0; i < clipData.getItemCount(); i++) {
Uri imageUri = clipData.getItemAt(i).getUri();
mSelectedImages.add(imageUri);
}
} else {
Uri imageUri = data.getData();
mSelectedImages.add(imageUri);
}
mAdapter.notifyDataSetChanged();
}
}
}
```
此代码使用 RecyclerView 和 Glide 库来显示选定的多个图像。通过 `Intent.ACTION_GET_CONTENT` 请求用户允许选择多个图像,并在 `onActivityResult()` 方法中将它们添加到 RecyclerView 中。
阅读全文