I am trying to select images from android gallery. And here is my code. It works fine with single image. But when if select multiple image its giving me back null. Any idea whats going wrong
Button addNewCart = (Button) findViewById(R.id.imageSelect);
addNewCart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent( );
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(Intent.createChooser(intent,
"select multiple images"), 100);
}
});
And here is the code for activity
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == Activity.RESULT_OK){
String[] all_path = data.getStringArrayExtra("all_path");
if(data != null)
{
Uri selectedImageUri = data.getData();
System.out.println(selectedImageUri);
}
}
}
Any ideas ?
Thanks
try like this,
private final int PICK_IMAGE_MULTIPLE =1;
addNewCart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent( );
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(Intent.createChooser(intent,
"select multiple images"), PICK_IMAGE_MULTIPLE);
}
});
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == Activity.RESULT_OK){
if(requestCode == PICK_IMAGE_MULTIPLE){
String[] imagesPath = data.getStringExtra("data").split("\\|");
}
}
}
Related
Set image in com.mikhaellopez.circularimageview.CircularImageView from gallery image display after close android app image not showing in android studio
imgbtnSetProfile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 100);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100 && resultCode == RESULT_OK && null != data) {
try {
Uri uriImage = data.getData();
circuler_imgView.setImageURI(uriImage);
}
i was adding profile picture option in my application from camera and gallery All was working well till i tried to select a picture and display it in my image circulerView .when i select a picture to set in my imageCirculerView , it doesn't set in the imageCirculerView and no error is even showing ..So i literally don't know where i did the mistake
private void pickFromGallery() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"select picture"), IMAGE_PICK_GALLERY_CODE);
}
private void pickFromCamera() {
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Images.Media.TITLE, "Temp_image Title");
contentValues.put(MediaStore.Images.Media.DESCRIPTION, "Temp_desc Desctription");
image_uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, image_uri);
startActivityForResult(intent, IMAGE_PICK_CAMERA_CODE);
}
profilepicture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//pick a image
showImagePickDialog();
}
});
private void showImagePickDialog() {
//options to display in dialoge
String[] options = {"Camera", "Dialoge"};
//dialoge
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick Image").setItems(options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
//Camera Clicked
if (checkCameraPermission()) {
//Camera request allowed
pickFromCamera();
} else {
//not allowed Request
requestCameraPermission();
}
} else {
//gallery Clicked
if (checkStoragePermission()) {
//Storage request allowed
pickFromGallery();
} else {
//not allowed Request
requestStoragePermission();
}
}
}
}).show();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
if(requestCode == RESULT_OK){
if(requestCode== IMAGE_PICK_GALLERY_CODE){
//getPicked image
image_uri = data.getData();
//settoimageView
profilepicture.setImageURI(image_uri);
}else if(requestCode== IMAGE_PICK_CAMERA_CODE){
//settoimageView
profilepicture.setImageURI(image_uri);}
}
super.onActivityResult(requestCode, resultCode, data);
}
I try to send extra data of string by intent to a function, but I receive null.
here is the intent.put:
private void takePicFromGallery(String nameOfButton) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
**intent.putExtra(NAME_OF_BUTTON, nameOfButton.toString());**
startActivityForResult(intent.createChooser(intent, "choose picture"), PICK_FROM_GALLERY);
and here is the the getting:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
String nameOfButton = data.getStringExtra(NAME_OF_BUTTON);
switch (nameOfButton) {
case "ibMainPicture": {
ibMainPicture.setImageBitmap(bitmap);
break;
}
case "imageButton1": {
imageButton1.setImageBitmap(bitmap);
break;
}
The intent received during onActivityResult is not the same intent which you are creating in the takePicFromGallery method. The intent you are starting is consumed by the Activity opened and it sends a new intent back to your application.
Option1(Preferred Option):
private static final int IB_MAIN_PICTURE_REQUEST_CODE = 524;
private static final int IMAGE_BUTTON_1_REQUEST_CODE = 785;
private void takePicFromGallery(String nameOfButton) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
if(nameOfButton.equals("ibMainPicture")) {
startActivityForResult(intent.createChooser(intent, "choose picture"), IB_MAIN_PICTURE_REQUEST_CODE);
else if(nameOfButton.equals("imageButton1") {
startActivityForResult(intent.createChooser(intent, "choose picture"), IMAGE_BUTTON_1_REQUEST_CODE);
}
Then when getting the Result:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == Activity.RESULT_OK) {
if(requestCode == IB_MAIN_PICTURE_REQUEST_CODE) {
ibMainPicture.setImageBitmap(bitmap);
} else if(requestCode == IMAGE_BUTTON_1_REQUEST_CODE) {
imageButton1.setImageBitmap(bitmap);
}
}
}
Option 2:
private static String lastButtonClicked = null;
private void takePicFromGallery(String nameOfButton) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
lastButtonClicked = nameOfButton.toString();
startActivityForResult(intent.createChooser(intent, "choose picture"), PICK_FROM_GALLERY);
}
Then when getting the Result:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(lastButtonClicked == null || resultCode != Activity.RESULT_OK || requestCode != PICK_FROM_GALLERY) {
return;
}
switch (lastButtonClicked) {
case "ibMainPicture": {
ibMainPicture.setImageBitmap(bitmap);
break;
}
case "imageButton1": {
imageButton1.setImageBitmap(bitmap);
break;
}
...
}
Use a specific request code for each different button clicks instead of a generic PICK_FROM_GALLERY, then in the onActivityResult you can check the request code sent with the intent
In my android app, I attach a string value to my image picker event. I then want to get it back, but it keeps getting a null value.
Does anyone know whats wrong?
Thanks
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
Intent image_chooser = Intent.createChooser(intent, getString(R.string.select_picture));
image_chooser.putExtra("type", "6");
startActivityForResult(image_chooser, SELECT_PICTURE);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
String type = data.getExtras().getString("type"); // -- > null
if (type.equals("5")) {
Bitmap bitmap = MyImage.GetBitmapFromPath(this, data.getData(), 240, 180);
new Async_up_image(bitmap, NavigationScreen.CategoryWhosOptionsClicked);
} else {
analyze(data);
}
}
}
Try this way,hope this will help you to solve your problem.
HashMap<Integer,String> map = new HashMap<Integer, String>();
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
Intent image_chooser = Intent.createChooser(intent, getString(R.string.select_picture));
map.put(SELECT_PICTURE,"6");
startActivityForResult(image_chooser, SELECT_PICTURE);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if(requestCode==SELECT_PICTURE) {
String type = map.get(requestCode);
if (type.equals("5")) {
Bitmap bitmap = MyImage.GetBitmapFromPath(this, data.getData(), 240, 180);
new Async_up_image(bitmap, NavigationScreen.CategoryWhosOptionsClicked);
} else {
analyze(data);
}
}
}
}
In my simple Activity I launch the selection of an image.
On the result of the selection i want to show the image.
But debugging I see that resultCode == RESULT_OK is not true.
What am i doing wrong here???
public class PictureActivity extends Activity {
private static final int SELECT_PICTURE = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, SELECT_PICTURE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setType("image/*");
intent.setData(data.getData());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
}
}
It is because of:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
From the documentation: http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_NEW_TASK
"This flag can not be used when the caller is requesting a result from the activity being launched."
Use my Code :-
Uri selectedImageUri; // Global Variable
String selectedPath; // Global Variable
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView preview = findViewById(R.id.preview);
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select file to upload "), 10);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if(data.getData() != null){
selectedImageUri = data.getData();
}else{
Log.d("selectedPath1 : ","Came here its null !");
Toast.makeText(getApplicationContext(), "failed to get Image!", 500).show();
}
if (requestCode == 10)
{
selectedPath = getPath(selectedImageUri);
preview.setImageURI(selectedImageUri);
Log.d("selectedPath1 : " ,selectedPath);
}
}
}