I would like to select multiple images and display these images into different separate ImageViews. I select a multiple images, but it shows the same image view. Please help I'm really stuck.
For Example. if the user selects 2 pic direct set into imageview 1 and imageview 2
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"), REQUEST_BROWSE_PICTURE);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_BROWSE_PICTURE){
if (data.getData() != null) {
Uri selectedImage = data.getData();
ivImage1.setImageURI(null);
ivImage1.setImageURI(selectedImage);
}
}
}
}
First try to get all images data.getClipData - check out this answer
store this to an arraylist for Recyclerview
Show selected image
create multiple image views instead of single one. ivImage1, ivImage2 etc or use Images in RecyclerView
Get all Uris:
ClipData clip = data.getClipData();
for(int i = 0; i < clip.getItemCount(); i++) {
ClipData.Item item = clip.getItemAt(i);
Uri uri = item.getUri();
urilist.add(uri);
}
Then pass the urilist to a listView or recyclerView adapter.
Related
I want to create an application that picks the image from gallery automatically according to its name. The user needs to sign in a signaturepad that he can save on the device and when he clicks on the button to save the image, the app automatically picks this image from gallery and display it on an imageview. I created one where I need to pick the image manually (with a little bit of help), this is my code so far:
private void SelectImage()
{
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*"); //Can I define here the name of the image that I want to be selected?
startActivityForResult(intent, PICK_IMAGE_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && null != data){
imageUri = data.getData();
imageView1.setImageURI(imageUri);
}
}
I would like to allow the user to pock only one image that can be referenced via a uri, I've successfully done this through the following code:
private static final int PICK_IMAGE_REQUEST = 1;
private Uri imageUri;
// Choose file extended from BottomTabView, opens all images on device
public static void openFileChooser(Context context) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
((Activity) context).startActivityForResult(intent, PICK_IMAGE_REQUEST);
// Slide Animation
((Activity) context).overridePendingTransition(R.anim.slide_in_up, R.anim.nothing);
}
// TODO: Is it better to use bitmap or URI
// Check if user has selected file and describe next step
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
// Retrieve image as a URI
imageUri = data.getData();
// Pass image URI to an intent and start activity
Intent intent = new Intent(this, UploadImageActivity.class);
intent.putExtra("imageUri", imageUri.toString());
startActivity(intent);
// Slide Animation
overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
this.finish();
}
}
The above code opens the following:
However, I would like to have something like the following:
Question: How can I achieve something more like the "Custom Gallery"?
if you want to custom layout pick image, you need:
create a screen and using recyclerView to make the layout as you want
get all picture gallery (https://stackoverflow.com/a/25957752/10153377)
I have implemented adding multiple image selection from the gallery in my project. However, I want to limit the user so he/she can select only 3 images from the gallery.
I have implemented selecting multiple images from the gallery like this:
`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"), 1);`
How can I achieve this?
Thanks.
You can get the count of ClipData when selecting multiple images from and gallery and if that count is greater than 3 you can notify the user about it.
You can do something like this after selecting images from gallery:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK || resultCode != RESULT_CANCELED){
ClipData clipData = data.getClipData();
if(clipData.getItemCount() > 3){
//notify user here...
}
}
}
This is not possible.
https://developer.android.com/reference/android/content/Intent.html#EXTRA_ALLOW_MULTIPLE
You'll have to manually check the returned data to see if it's more than 10 items, and if so, show a Toast
put this in your build.gradle(app)
compile 'com.github.esafirm.android-image-picker:imagepicker:1.5.0'
compile 'com.github.esafirm.android-image-picker:rximagepicker:1.5.0'
and this in your activity
private void pickImage() {
ImagePicker.create(UploadPhotosActivity.this)
.showCamera(false)
.limit(3)
.imageTitle(getString(R.string.select_image))
.folderTitle(getString(R.string.folder))
.theme(R.style.ImagePickerTheme)
.start(RC_CODE_PICKER);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_CODE_PICKER) {
Log.d("===uploadPhoto", "gallery : " + data);
imagesList = (ArrayList<Image>) ImagePicker.getImages(data);
Intent intent = new Intent(UploadPhotosActivity.this, ImageCropperActivity.class);
intent.putExtra(ImageCropperActivity.EXTRA_VIEW_PORT_RATIO, imagesList);
startActivity(intent);
}
} else {
Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent);
finish();
}
}
I have a MultiplePhotoSelectActivity.java which let user select multiple photo and store the path in an ArrayList.
public void btnChoosePhotosClick(View v){
ArrayList<String> selectedItems = imageAdapter.getCheckedItems();
if (selectedItems!= null && selectedItems.size() > 0) {
//Toast.makeText(MultiPhotoSelectActivity.this, "Total photos selected: " + selectedItems.size(), Toast.LENGTH_SHORT).show();
Log.d(MultiPhotoSelectActivity.class.getSimpleName(), "Selected Items: " + selectedItems.toString());
Intent intent = new Intent(MultiPhotoSelectActivity.this,PreuploadActivity.class);
intent.putStringArrayListExtra("selectedItems", selectedItems);
setResult(RESULT_OK, intent);
startActivity(intent);
}
}
This is ArrayList<String> selectedItems come from imageAdapter
ArrayList<String> getCheckedItems() {
ArrayList<String> mTempArry = new ArrayList<>();
for(int i=0;i<mImagesList.size();i++) {
if(mSparseBooleanArray.get(i)) {
mTempArry.add(mImagesList.get(i));
}
}
return mTempArry;
}
After user choose the photo,the following result will appear in logcat
D/MultiPhotoSelectActivity: Selected Items: [/storage/emulated/0/Pictures/Screenshot_1486795867.png, /storage/emulated/0/Pictures/15592639_1339693736081458_1539667284_n.jpg, /storage/emulated/0/15592639_1339693736081458_1539667284_n.jpg]
The problem now is,I want to display the image in my another activity using the file path in the array list,after the user choose the image
Here is PreuploadActivity.java that should be receive the intent data.
This is the button to let user choose photo in MultiplePhotoSelectActivity.java
//this button will open gallery,and select photo
addPhoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(PreuploadActivity.this,MultiPhotoSelectActivity.class);
startActivityForResult(intent,PICK_IMAGE_REQUEST);
}
});
This is the onActivityResult() which should receive the Intent data from MultiplePhotoSelectActivity.java
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data.getData() !=null){
ArrayList<String> selectedItems = data.getStringArrayListExtra("selectedItems");
for(String selectedItem : selectedItems){
Uri filePath = Uri.parse(selectedItem);
try{
// bimatp factory
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
bitmap = BitmapFactory.decodeFile(filePath.getPath(),
options);
//Setting image to ImageView
ImageView imageView = new ImageView(getApplicationContext());
LinearLayout.LayoutParams layoutParams =
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(layoutParams);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setPadding(0, 0, 0, 10);
imageView.setAdjustViewBounds(true);
imageView.setImageBitmap(bitmap);
linearMain.addView(imageView);
}catch (Exception e) {
e.printStackTrace();
}
}
So now in onActivityResult() of PreuploadActivity.java I cant display back the image in the ArrayList which sent from MultiplePhotoSelectActivity.java.I suspect is something wrong when putExtra in the intent,what I tried so far but still no different.
The answer of this Stackoverflow question
putParcelable or putSerializable like the answer
How to transfer a Uri image from one activity to another?
So what I need to know,
1) How should I putExtra and getExtra in the intent in both Activity in order send and receive the ArrayList of the image?
2) Is my handle to display the image correct?If no,please tell me what I doing wrong.
EDIT:Try for Aslam Hossin solution
After I tried this
ArrayList<String> myList = (ArrayList<String>) getIntent().getSerializableExtra("selectedItems ");
I got the following error
After looking at some documentation I figure out I make a few mistake
MultiPhotoSelectActivity.java
Intent intent = new Intent(MultiPhotoSelectActivity.this,PreuploadActivity.class);
intent.putStringArrayListExtra("selectedItems", selectedItems);
setResult(RESULT_OK, intent);
startActivity(intent);
I figure out,there are 3 mistake at above code,
1)In MultiPhotoSelectActivity.java should not a new intent,but it should be send the data back to PreuploadActivity.java
2) I should setResult like this
setResult(Activity.RESULT_OK, data);
3) According to the documentation as below ,so I add finish() after setResult()
Data is only returned once you call finish(). You need to call setResult() before calling finish(), otherwise, no result will be returned.
I solve it by setting result code in PreuploadActivity.java like below
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == PICK_IMAGE_REQUEST && resultCode == Activity.RESULT_OK ){
//setting Activity.RESULT_OK
ArrayList<String> selectedItems = data.getStringArrayListExtra("selectedItems");
This is MultiPhotoSelectActivity.java I do the following changes
ArrayList<String> selectedItems = imageAdapter.getCheckedItems();
if (selectedItems!= null && selectedItems.size() > 0) {
//Toast.makeText(MultiPhotoSelectActivity.this, "Total photos selected: " + selectedItems.size(), Toast.LENGTH_SHORT).show();
Log.d(MultiPhotoSelectActivity.class.getSimpleName(), "Selected Items: " + selectedItems.toString());
final Intent data = new Intent();
data.putStringArrayListExtra("selectedItems", selectedItems);
setResult(Activity.RESULT_OK, data);
finish();
}
}
In the app I am building, the user selects a picture from gallery and the path is saved in Shared Preferences. I then want to retrieve this picture from the stored path but it doesn't work. The image is an ImageButton, which the user clicks in order to select picture from Gallery.
The code I have to retrieve the picture and "put" on the ImageButton is:
This code does actually work now, put it here in case it helps others.
File imgFile = new File(sharedpreferences.getString(Path, LOCATION_SERVICE));
if(imgFile.exists())
{
Bitmap b = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageButton img=(ImageButton)findViewById(R.id.AddPic);
img.setImageBitmap(b);
}
I know the path is correct, but I am not able to retrieve the picture and put it on the ImageButton.
Below is the code where the user clicks on the ImageButton, and selects a picture from the gallery and the path of that picture is stored within the sharedPreferences:
imgButton = (ImageButton) findViewById(R.id.AddPic);
imgButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent GaleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(GaleryIntent, RESULT_LOAD_IMAGE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri SelectedImage = data.getData();
String[] FilePathColumn = {MediaStore.Images.Media.DATA };
Cursor SelectedCursor = getContentResolver().query(SelectedImage, FilePathColumn, null, null, null);
SelectedCursor.moveToFirst();
int columnIndex = SelectedCursor.getColumnIndex(FilePathColumn[0]);
String picturePath = SelectedCursor.getString(columnIndex);
SelectedCursor.close();
imgButton.setImageBitmap(BitmapFactory.decodeFile(picturePath));
Editor editor = sharedpreferences.edit();
editor.putString(Path, picturePath);
editor.commit();
}
}
What am I doing wrong?
Thanks very much!