我想从图库中获取图像,获取其可绘制地址,然后将这些地址存储在一个int数组中。允许用户选择图像。我使用了ImageView
,并且允许用户从列表中选择图像。
public class StickerSelectActivity extends AppCompatActivity {
public static final String EXTRA_STICKER_ID = "extra_sticker_id";
private final int[] stickerIds = {
R.drawable.abra,
R.drawable.bellsprout,
R.drawable.bracelet,
R.drawable.bullbasaur,
R.drawable.camera,
R.drawable.candy
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.select_sticker_activity);
//noinspection ConstantConditions
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.stickers_recycler_view);
GridLayoutManager glm = new GridLayoutManager(this, 3);
recyclerView.setLayoutManager(glm);
List<Integer> stickers = new ArrayList<>(stickerIds.length);
for (Integer id : stickerIds) {
stickers.add(id);
}
recyclerView.setAdapter(new StickersAdapter(stickers, this));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
private void onStickerSelected(int stickerId) {
Intent intent = new Intent();
intent.putExtra(EXTRA_STICKER_ID, stickerId);
setResult(RESULT_OK, intent);
finish();
}
class StickersAdapter extends RecyclerView.Adapter<StickersAdapter.StickerViewHolder> {
private final List<Integer> stickerIds;
private final Context context;
private final LayoutInflater layoutInflater;
StickersAdapter(@NonNull List<Integer> stickerIds, @NonNull Context context) {
this.stickerIds = stickerIds;
this.context = context;
this.layoutInflater = LayoutInflater.from(context);
}
@Override
public StickerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new StickerViewHolder(layoutInflater.inflate(R.layout.sticker_item, parent, false));
}
@Override
public void onBindViewHolder(StickerViewHolder holder, int position) {
holder.image.setImageDrawable(ContextCompat.getDrawable(context, getItem(position)));
}
@Override
public int getItemCount() {
return stickerIds.size();
}
private int getItem(int position) {
return stickerIds.get(position);
}
class StickerViewHolder extends RecyclerView.ViewHolder {
ImageView image;
StickerViewHolder(View itemView) {
super(itemView);
image = (ImageView) itemView.findViewById(R.id.sticker_image);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int pos = getAdapterPosition();
if (pos >= 0) { // might be NO_POSITION
onStickerSelected(getItem(pos));
}
}
});
}
}
}
}
答案 0 :(得分:0)
选择图像
gal_img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Select
Picture"), 1000);
}
});
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
// When an Image is picked
if (requestCode == 1000 && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
String[] filePathColumn = {MediaStore.Images.Media.DATA};
imagesUriList = new ArrayList<Uri>();
if (data.getData() != null) {
Uri mImageUri = data.getData();
// Get the cursor
Cursor cursor = getActivity().getContentResolver().query(mImageUri,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imageURI = cursor.getString(columnIndex);
cursor.close();
selected_file.add(getPath(getActivity(), mImageUri)); //store into array
}