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();
}
}
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 am trying to select PDF file on button click but I'm getting following message on samsung device :
all apps associated with this action have been turned off blocked or
are not installed.
private void selectPDFFiles(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
Uri.parse("package:" + getPackageName()));
startActivity(intent);
return;
}
intent = new Intent();
intent.setType("application/pdf");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent,"select PDF "), 1);
}
Try below code
private void selectPDFFiles(){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("application/pdf");
startActivityForResult(intent, 555);
}
And you can get file in activity result
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 555:
if (resultCode == RESULT_OK) {
// Get the Uri of the selected file
Uri uri = data.getData();
String uriString = uri.toString();
File myFile = new File(uriString);
String path = myFile.getAbsolutePath();
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
Update
Also try below code
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("application/pdf");
startActivityForResult(intent, 555);
I hope this can help You!
Thank You.
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.
So whenever I select a picture from the gallery in my app, it crashes. Here is the code for the button to the gallery and selected picture to the imageview.
pickImageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
openGallery();
}
});
}
private void openGallery() { //opens the gallery
Intent gallery =
new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, PICK_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
Uri imageUri = data.getData();
imageView2.setImageURI(imageUri);
}
}
Please comment if you need more information, I desperately need help, as this is a very major roadblock for me.
Without the Logs, I can recommend on the following:
Make sure that pickImageButton isn't null.
Make sure that imageView2 isn't null.
Make sure that in manifest your Activity is in:
android:launchMode="singleTop"