I'm trying to put extras (string) in an intent. I use startActivityForResult and onActivityResult to get my extras on the other side.
But I can't get why it doesn't works ! Here's my code :
buttonCamera.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra("abc", "test");
startActivityForResult(intent, PHOTO_RESULT);
}
});
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PHOTO_RESULT) {
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
if (extras != null) {
String abc = extras.getString("abc");
Toast.makeText(getApplicationContext(), abc, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "can't get", Toast.LENGTH_SHORT).show();
}
}
}
}
I always get an empty toast, so the extra is not null.. But I cant't get the String..
Thanks !
edit:
To tell the camera the file name you can use this:
File mFile = new File(path, filename);
final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mFile));
activity.startActivityForResult(intent, TAKE_PHOTO_CODE);
The camera activity will get your extra intent data, but it won't return that "abc" string. You can't tell the camera activity to return "abc" for results. What are you trying to do with that abc string?
Obviously you will get an empty toast .
You are putting extra for the intent new Intent(MediaStore.ACTION_IMAGE_CAPTURE) and the Intent data you are getting in onActivity result is different , (Returned from Camera Activity ) .
so store value in a class variable will work fine .
Related
I searched a lot and tried things but the onActivityResult function isn't launched when the intent from which I try to get a string information is closed.
I use Visual Studio to write this application, this is my code :
The click event that opens the activity where users can type strings :
private void Btn_Valid_Click(object sender, EventArgs e)
{
----
Intent intent = new Intent(this, typeof(activity_OF_TransfertChxChmb));
StartActivityForResult(intent, 0);
----
}
The function in the openned Intent that should return the string information :
private void Validate()
{
string stringToPassBack = tb_Store.Text;
// put the String to pass back into an Intent and close this activity
Intent intent = new Intent();
intent.PutExtra("result", stringToPassBack);
SetResult(Result.Ok, intent);
Finish();
}
And the onActivityResult function that should be launched in the first activity:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
base.onActivityResult(requestCode, 0, data);
if (requestCode == 0)
{
if (resultCode == -1) // Ok
{
string result = data.GetStringExtra("result");
}
if (resultCode == 0) // Canceled
{
//Write your code if there's no result
}
}
}
I am missing something, but can't figure out what.
Thank you for your help.
You are adding the extra via:
intent.PutExtra(Intent.ExtraText, stringToPassBack);
Your key is Intent.ExtraText.
You are retrieving the extra via:
string result = data.GetStringExtra("result");
Your key is "result".
So, perhaps Intent.ExtraText does not equal "result". You need to use the same key in both places.
I am trying to launch an activity after a user has selected a photo. I was trying to do this:
uploadImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent selectImageIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
selectImageIntent.setType("image/*");
startActivityForResult(selectImageIntent, 1);
Intent goToActivityIntent = new Intent(view.getContext(), SendPhotoChangeActivity.class);
goToActivityIntent.putExtra("email", email);
goToActivityIntent.putExtra("donorEmail", donorEmail);
goToActivityIntent.putExtra("orderId", orderId);
goToActivityIntent.putExtra("uriString", uriString);
view.getContext().startActivity(goToActivityIntent);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && data != null) {
uriString = data.getData().toString();
}
}
But I realised that with this code, the code for launching the activity (SendPhotoChangeActivity) executes before the user selects the image, crashing the app because the uriString variable is null.
I tried simply copy/pasting the code into onActivityResult(), but the view variable (in view.getContext()) was, of course, not recognized in onActivityResult().
I am thinking of simply replacing view.getContext() by getApplicationContext() in onActivityResult(). Is this the right thing to do? If not, please tell me how I can start an activity in onActivityResult().
If you are in Activity then you can just use this as Context
Intent goToActivityIntent = new Intent(this, SendPhotoChangeActivity.class);
If you are in a Fragment then you can obtain Context by calling getContext()
Intent goToActivityIntent = new Intent(getContext(), SendPhotoChangeActivity.class);
And use that code inside onActivityResult() as you were trying to.
set an integer code for the act of selecting a picture like REQUEST_CODE_TAKE_PICTURE so you know that has happened, this works ok in Kotlin, I assume that works as well with java:
if (requestCode == REQUEST_CODE_TAKE_PICTURE && resultCode == Activity.RESULT_OK) {
Intent goToActivityIntent = new Intent(view.getContext(),SendPhotoChangeActivity.class);
goToActivityIntent.putExtra("email", email);
goToActivityIntent.putExtra("donorEmail", donorEmail);
goToActivityIntent.putExtra("orderId", orderId);
goToActivityIntent.putExtra("uriString", uriString);
view.getContext().startActivity(goToActivityIntent);
if (data == null) {
//Display an error
println("error accuered at onActivityResult ")
return
}
Have you tried this simpler one:
uploadImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent selectImageIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
selectImageIntent.setType("image/*");
startActivityForResult(selectImageIntent, 1);
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && data != null) {
uriString = data.getData().toString();
Intent goToActivityIntent = new Intent(view.getContext(), SendPhotoChangeActivity.class);
goToActivityIntent.putExtra("email", email);
goToActivityIntent.putExtra("donorEmail", donorEmail);
goToActivityIntent.putExtra("orderId", orderId);
goToActivityIntent.putExtra("uriString", uriString);
startActivity(goToActivityIntent);
}
}
Just call
startActivity(goToActivityIntent);
to call the activity.
This assumes you are calling it from your activity or fragment. If this doesn't meet your requirements, let me know. There are other ways to implement this.
I am trying to add a code in my current app so that on one button click ,camera should open and one snap should be taken.
here is my code :
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
try {
PackageManager pm =this.getPackageManager();
final ResolveInfo mInfo = pm.resolveActivity(i, 0);
Intent intent = new Intent();
intent.setComponent(new ComponentName(mInfo.activityInfo.packageName, mInfo.activityInfo.name));
intent.setAction(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);
}
catch (Exception e){ Log.i(TAG, "Unable to launch camera: " + e); }
Error : it displays -> Complete action uing
Not able to figure it out what i am doin wrong, can any one help me out in this issue.
First
Add permission to your manifest
<uses-feature android:name="android.hardware.camera" android:required="fase" />
Second
static final int REQUEST_IMAGE_CAPTURE = 1;
#Override
protected void onCreate (Bundle savedInstanceState) {
//Your code
//dispatchTakePictureIntent() <-- call this on some button click etc.
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
Third
Retrieve your Snap
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(imageBitmap);
}
}
Retrieved Image is only a small size thumbnail. To get/save full size Image
I am trying to capture camera photo in may app...
this is what I have:
The photo is saved but on the on Activity Result, I get Null point Exception.
What could I be possible missing out?
private Uri getImgUri() {
File filePath= new File(Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES),APP_ALIAS);
if(!filePath.exists()){
if(!filePath.mkdirs())
return null;
}
String timeStamp= new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String path=filePath.getPath()+File.separator+"_IMG"+timeStamp+".jpg";
File file=new File(path);
return Uri.fromFile(file);
}
private void startGetPicFromCam() {
Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri uri= getImgUri();
intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
startActivityForResult(intent,MEDIA_CAPTURE_RESULT_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// super.onActivityResult(requestCode, resultCode, data);
if(requestCode==MEDIA_CAPTURE_RESULT_CODE){
if(resultCode==RESULT_OK){
try{
if(data.getData()!=null)
Toast.makeText(this,"saved to "+data.getData(),Toast.LENGTH_LONG).show();
else
Toast.makeText(this,"saved to path",Toast.LENGTH_LONG).show();
}
catch(Exception e){
Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG).show();
}
}
}
}
Edit, it appears I didn't read your question close enough. It appears that the issue is that when you use EXTRA_OUTPUT, a null intent is passed back. If you want to get access to your data, just query the file that you passed in as an extra. See this and this question for more detailed information.
HI i am getting null pointer exception while sending image path to another activity
here is below my code
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
System.out.println(requestCode + ", " + resultCode);
if (requestCode == SELECT_PICTURE) {
if (data.getData() != null) {
Uri selectedImageUri = data.getData();
String path = getPath(selectedImageUri);
System.out.println("PATH = "+path);
Intent _intent = new Intent(MainActivity.this,AndroidFaceDetector.class);
_intent.putExtra("mypath", path);
startActivityForResult(_intent, CROPPED_FACE_IMAGE);
}
else
{
Toast.makeText(MainActivity.this, "Please try again", Toast.LENGTH_SHORT).show();
}
}
else {
setImage(data);
}
}
}
Now in AndroidFaceDetector class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new myView(this));
Intent _intent = getIntent();
path = _intent.getStringExtra("mypath");
System.out.println("Path is gettting "+path); //here i am getting null value
}
because
here i am getting path value null.. can any body solve this problem....
try like this in Second Activity
Intent myintent = getIntent();
if(null != myintent.getExtras())
{
// do your work here
String path = myintent.getExtras().getString("mypath");
}
else
{
// not here you can't get values
Toast.makeText(getApplicationContext(),"No Recor Here..",12).show();
}
In your second activity try with this code instead of yours,
path = _intent.getString("mypath");
(or)
Bundle bundle = this.getIntent().getExtras();
String pic = bundle.getString("mypath");
If you wish to pass image instead of URI means try this code
In your First activity,
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
Intent intent = new Intent();
intent.setClass(AndroidPassingBitmap.this, AndroidReceiveBitmap.class);
intent.putExtra("Bitmap", bitmap);
startActivity(intent);
In your Second activity,
Bitmap bitmap = (Bitmap)this.getIntent().getParcelableExtra("Bitmap");
ImageView viewBitmap = (ImageView)findViewById(R.id.bitmapview);
viewBitmap.setImageBitmap(bitmap);
Write below Code to get data from intent, it will solve your problem.
Bundle bdl=getIntent().getExtras();
String path=bdl.getString("mypath");
And see below link for more information.
pass bitmap between activities in android