Я работаю над проектом, в котором я должен выбрать несколько фотографий из галереи и хочу сохранить их в массиве изображений. Я не могу выбрать ни одного изображения из окна просмотра изображений. Может кто-нибудь сказать мне, пожалуйста, как я могу импортировать несколько изображений и сохранить их в массиве?
private void chooseimage() {
final CharSequence[] items = {"Take Photo", "Choose from Library",
"Cancel"};
android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(ActionStatusActivity.this);
builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo")) {
userChoosenTask = "Take Photo";
dialog.dismiss();
cameraIntent();
} else if (items[item].equals("Choose from Library")) {
userChoosenTask = "Choose from Library";
dialog.dismiss();
picimage();
//galleryIntent();
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
private void picimage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), PICK_IMAGE_MULTIPLE);
}
**@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_FILE && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
if(data.getClipData() != null) {
int count = data.getClipData().getItemCount(); //evaluate the count before the for loop --- otherwise, the count is evaluated every loop.
for(int i = 0; i < count; i++)
imageUri = data.getClipData().getItemAt(i).getUri();
onSelectFromGalleryResult((List<String>) imageUri);
//do something with the image (save it to some directory or whatever you need to do with it here)
}
} else if(data.getData() != null) {
String imagePath = data.getData().getPath();
//do something with the image (save it to some directory or whatever you need to do with it here)
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
}**
private void onSelectFromGalleryResult(List<String> data) {
Bitmap bm = null;
int position = 0;
int width = 1;
int height = 1;
editArray = new EditText[data.size()];
mLayoutParams = new RelativeLayout.LayoutParams(mLayoutImage.getWidth(), ViewGroup.LayoutParams.WRAP_CONTENT);
imageArray = new ImageView[data.size()];
mLayoutImage.setLayoutParams(mLayoutParams);
for (int i = 0; i <data.size(); i++) {
LinearLayout mainLayout=new LinearLayout(getApplicationContext());
mainLayout.setOrientation(LinearLayout.VERTICAL);
RelativeLayout.LayoutParams maiParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
maiParams.setMarginStart(200*i);
editArray[i] = new EditText(ActionStatusActivity.this);
editArray[i].setTextSize(16);
imageArray[i] = new ImageView(ActionStatusActivity.this);
imageArray[i].setId(i);
editArray[i].setId(i);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
200,
200);
RelativeLayout.LayoutParams textParams = new RelativeLayout.LayoutParams(
200,
RelativeLayout.LayoutParams.WRAP_CONTENT);
imageArray[i].setLayoutParams(params);
editArray[i].setLayoutParams(textParams);
new UploadFile().execute(Uri.fromFile(new File(data.get(i))).toString());
String path = Uri.fromFile(new File(data.get(i))).toString();
Glide.with(getApplicationContext()).load(Uri.fromFile(new File(data.get(i)))).asBitmap().override(250, 250).centerCrop().into(imageArray[i]);
mLayoutParams.addRule(RelativeLayout.BELOW, editArray[i].getId());
mainLayout.addView(imageArray[i]);
mainLayout.addView( editArray[i]);
mainLayout.setLayoutParams(maiParams);
mLayoutImage.addView(mainLayout);
}
}