getStringExtra() return null - android

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

Related

How to implement scoped storage to pick video from Gallery and send it to another Activity on Android 10

In my app i have 2 buttons one to open camera and record video and second one to select video from Gallery , then i send the video to another activity using intent . It is working fine on all android versions except android 10
private void openVideoCapture() {
String[] perms = {Manifest.permission.CAMERA};
if (EasyPermissions.hasPermissions(this, perms)) {
final int durationLimit = 600;
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, durationLimit);
startActivityForResult(intent, REQUEST_VIDEO_TRIMMER);
} else {
// Do not have permissions, request them now
EasyPermissions.requestPermissions(this, getString(R.string.permission_camera), 123, perms);
}
}
#AfterPermissionGranted(124)
private void pickFromGallery() {
String[] perms = {Manifest.permission.READ_EXTERNAL_STORAGE};
if (EasyPermissions.hasPermissions(this, perms)) {
Intent intent = new Intent();
intent.setTypeAndNormalize("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent, getString(R.string.label_select_video)), REQUEST_VIDEO_TRIMMER);
} else {
EasyPermissions.requestPermissions(this, getString(R.string.permission_read_storage_rationale), 124, perms);
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_VIDEO_TRIMMER) {
final Uri selectedUri = data.getData();
if (selectedUri != null) {
startTrimActivity(selectedUri);
} else {
Toast.makeText(MainActivity.this, R.string.toast_cannot_retrieve_selected_video, Toast.LENGTH_SHORT).show();
}
}
}
}
private void startTrimActivity(#NonNull Uri uri) {
Intent intent = new Intent(this, TrimmerActivity.class);
intent.putExtra(EXTRA_VIDEO_PATH, FileUtils.getPath(this, uri));
startActivity(intent);
}

onActivityResult has data null for startActivityForResult in fragment

I have a fragment where I am calling getActivity().startActivityForResult for camera activity and I have onActivityResult in my MainActivity to handle the Result.
Fragment
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 250);
intent.putExtra("outputY", 250);
try {
intent.putExtra("return-data", true);
getActivity().startActivityForResult(Intent.createChooser(intent,"Complete action using"), PICK_FROM_GALLERY);
} catch (ActivityNotFoundException e) {
// Do nothing for now
}
MainActivity
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) //CANCELED
{
Toast.makeText(this, "canceled", Toast.LENGTH_SHORT).show();
}
switch (requestCode) {
case PICK_FROM_GALLERY:
Toast.makeText(this, "Pick from Gallery", Toast.LENGTH_SHORT).show();
if (resultCode == RESULT_OK) {
Toast.makeText(this, "Result Okay", Toast.LENGTH_SHORT).show();
Bundle extras2 = data.getExtras();
if (extras2 != null) {
//Doesn't enter here
} else {
Toast.makeText(this, "extra is null", Toast.LENGTH_SHORT).show();
}
}
break;
}
}
Activity #Oncreate open camera intent
// Camera Option Clicked
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, 1);
Handle onActivityResult
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
if(resultCode == RESULT_OK){
if (data != null) {
takePhoto(data);
}
}
break;
}
}
Display image on ImageView
private void takePhoto(Intent imageData){
Bundle extras = imageData.getExtras();
if(extras != null){
imageView.setImageBitmap((Bitmap) extras.get("data"));
}
}
change this line
getActivity().startActivityForResult(Intent.createChooser(intent,"Complete action using"), PICK_FROM_GALLERY);
to
startActivityForResult(Intent.createChooser(intent,"Complete action using"), PICK_FROM_GALLERY);
refer this for further information onActivityResult is not being called in Fragment
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),
112);
if (requestCode == 112) {
try {
InputStream inputStream = getContentResolver()
.openInputStream(data.getData());
FileOutputStream fileOutputStream = new FileOutputStream(
mFileTemp);
copyStream(inputStream, fileOutputStream);// do other stuff
fileOutputStream.close();
inputStream.close();
//do other stuff
} catch (Exception e) {
e.printStackTrace();
}
}
You should call
startActivityForResult(Intent.createChooser(intent,"Complete action using"), PICK_FROM_GALLERY); from your fragment and then implement the onActivityResult() in your Fragment itself, and in the onActivityResult() just check for the result code as
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == PICK_FROM_GALLERY) {

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

How to attach string value to image picker intent in 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);
}
}
}
}

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

Categories

Resources