How to attach string value to image picker intent in android? - android

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);
}
}
}
}

Related

pick image form gallery not display after close application

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);
}

getStringExtra() return null

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

multiple image selection from gallery

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("\\|");
}
}
}

startActivityForResult() for selecting image, why do I not get RESULT_OK?

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);
}
}
}

camera activity does not return to the previous/caller activity

I read an text input from the user, this input i use it as a name of a picture i'm taking by the camera.
i store this name and the path of the image name into Sqlite Database.
what I'm trying to do is, after clicking OK to accept the taken picture, i want the saved path to be displayed in a toast.
the problem is, when I click OK to accept the picture, nothing is being displayed and i cant switch from the camera activity to the activity that called the camera activity"the previous activity"
Note: I'm using the emulator not a real device.
OnClickListener btn_TakePictureListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String imgPath = retrievePath();
intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri(imgPath));
startActivityForResult(intent, RequestCode);
}
};
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RequestCode && resultCode == RESULT_OK) {
String s = data.getData().toString();
Toast.makeText(getBaseContext(), ""+s, Toast.LENGTH_SHORT).show();
}
}
if you are passing Uri for you image then you can retrieve image as taken by camera:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
Environment.getExternalStorageDirectory(), "temp.jpg")));
startActivityForResult(intent, 1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == NONE)
return;
if (requestCode == 1) {
// Set the file save path with directory
File picture = new File(Environment.getExternalStorageDirectory()
+ "/temp.jpg");
Uri imageuri= Uri.fromFile(picture);
//set to imageview here
}
}
EDIT:
For Getting data uri in onActivityResult start Camra Activiyt as:
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,IMAGE_UNSPECIFIED);
startActivityForResult(intent, 2);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == NONE)
return;
if (requestCode == 1) {
Uri uriimg = data.getData();
Toast.makeText(getBaseContext(), ""+uriimg.toString(), Toast.LENGTH_SHORT).show();
}
}

Categories

Resources