Hi i have used SurfaceView and taking picture by below code
First i am starting activity by this code
startActivityForResult(new Intent(PictureEditor.this, CustomCamera.class), CAMERA_REQUEST3);
and then getting result from this code
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == CAMERA_REQUEST3) {
BitmapFactory.Options abc = new BitmapFactory.Options();
abc.inJustDecodeBounds = true;
BitmapFactory.decodeFile((Environment.getExternalStorageDirectory() + File.separator + "tester.png"), abc);
abc.inSampleSize = calculateInSampleSize(abc, w, h) + 1;
abc.inJustDecodeBounds = false;
view.setBackBitmap(BitmapFactory.decodeFile((Environment.getExternalStorageDirectory() + File.separator + "tester.png"), abc));
}
Now the CustomeCamera Class's code is below
// / Handles when mTakePicture is clicked
private OnClickListener mTakePictureAction = new OnClickListener() {
#Override
public void onClick(View v) {
if (mCamera != null)
mCamera.takePicture(CustomCamera.this);
}
};
Then
#Override
public void takePicture(Activity activity) {
if (mCamera != null)
mCamera.takePicture(shutterCallback, rawCallback, jpegCallback);
Intent returnIntent = new Intent();
activity.setResult(mActivity.RESULT_OK, returnIntent);
activity.finish();
}
the problem is image is captured but the activity is not getting finish! Can anybody suggest me what to do!
you need to write code for finish activity in onActivityResult() in previous activity from where this activity starts.
So your previous activity finish . . .
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode)
{
case YOUR_REQUEST_CODE:
finish();
}
}
Edit :
First change this code.
Intent returnIntent = new Intent();
activity.setResult(Activity.RESULT_OK, returnIntent);
activity.finish();
then in onActivityResult() first check the request code condition then after in request code condition check result code condition.
I have a strong feeling that main (UI) thread is stuck while
#Override
public void takePicture(Activity activity) {
if (mCamera != null)
mCamera.takePicture(shutterCallback, rawCallback, jpegCallback);
Intent returnIntent = new Intent();
activity.setResult(mActivity.RESULT_OK, returnIntent);
activity.finish();
}
I am not too sure where it is being stuck (from code example above),
possibilities
1) picture cannot be saved
2) picture cannot be encoded
and etc
You can test it by running debugger
Related
I'm trying to use onActivityResult to send a title, I've looked at the google implementation for the same task but it doesn't work me. Can someone help?
code for onActivityResult in mainActivity.
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
System.out.println("There is something coming to this function" + requestCode);
if(requestCode == NEW_TITLE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK){
Title title = new Title(data.getStringExtra(notesSection.EXTRA_REPLY));
mTitleViewModel.insert(title);
}else{
Toast.makeText(getApplicationContext(), R.string.empty_not_saved, Toast.LENGTH_LONG).show();
}
}
code in my notesSection activity.
finishFab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// System.out.println("Button Has Been Clicked From Notes Section");
Intent replyIntent = new Intent();
if (TextUtils.isEmpty(titleEdit.getText())){
// System.out.println("Empty?");
setResult(RESULT_CANCELED, replyIntent);
}else{
// System.out.println("Sending Something Supposedly");
String title = titleEdit.getText().toString();
// System.out.println("Sending " + title);
replyIntent.putExtra(EXTRA_REPLY, title);
setResult(RESULT_OK, replyIntent);
}
finish();
// startActivity(new Intent(notesSection.this, MainActivity.class));
}
});
FYI: When I print something in the onActivityResult function, nothing shows up on my run terminal, I don't know why this is, but I don't think the function is being reached for some reason. I will send more code if necessary.
Thanks.
In your first activity try starting second activity like this
static int NEW_TITLE_ACTIVITY_REQUEST_CODE = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
---
---
---
// i assume you are starting activity on some button click
// so add following line in you button on click event
startActivityForResult(new Intent(MainActivity.this,notesSection.class),NEW_TITLE_ACTIVITY_REQUEST_CODE);
}
then override following method in your FIRST ACTIVITY
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == NEW_TITLE_ACTIVITY_REQUEST_CODE)
{
// do whatever you want
}
}
Also update your finish fab on click listener
finishFab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent replyIntent = new Intent();
if (TextUtils.isEmpty(titleEdit.getText())){
setResult(RESULT_CANCELED, replyIntent);
}
else{
replyIntent.putExtra("TITLE", titleEdit.getText().toString());
setResult(RESULT_OK, replyIntent);
}
finish();
}
});
Actually, this procedure has changed recently. You might want to take a look at the official documentation so that you can implement it according to your needs.
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 writing a component that allows user to pick a location based on the place indicated by the location picker. One of the requirements is to send the LatLng object back from the map activity to the activity that called the former. The problem is that returned result code is always RESULT_CANCELLED, despite setting it explicitly to RESULT_OK. Here's the code:
Calling activity:
public void getLocationBtn(View view) {
Intent i = new Intent(this, PickLocationActivity.class);
startActivityForResult(i, 1);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == Activity.RESULT_OK) {
location = data.getParcelableExtra("location");
Log.d(TAG, "gotLocation: " + location);
}
if (resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), "Location not chosen", Toast.LENGTH_SHORT).show();
}
}
}
Called activity:
btnFind.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
centerLatLang = mMap.getProjection().getVisibleRegion().latLngBounds.getCenter();
Button doneBtn = findViewById(R.id.locationPickerDoneBtn);
doneBtn.setEnabled(true);
}
});
}
public void doneBtn(View view) {
Intent returnIntent = new Intent();
returnIntent.putExtra("location", centerLatLang);
setResult(Activity.RESULT_OK, returnIntent);
finish();
}
btnFind get the coordinates, doneBtn confirms user's choice and comes back to the previous activity.
I have already tried replacing Intent returnIntent = new Intent(); with getIntent(), but it didn't work; the returned bundle was null.
it happen when your activity is using singleTask launch mode. so i recommand if you have below line in your manifest activity tab please remove it.
android:launchMode="singleInstance"
I have problem with onActivityResult, whatever I'm doing I can't get resultCode right.
I know that there are similar questions but at the end they didn't help me and I couldn't fix it
MainActivity: method which will open new Activity Popup.class
public void openShopView(){
Intent intent = new Intent(this, Popup.class);
Bundle b = new Bundle();
b.putString("which", "ShopMain");
intent.putExtras(b);
startActivityForResult(intent, 1);
}
Second Activity: method which will open yet another Activity Popup.class just with different layout
shop_c1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getIntent());
Bundle b = new Bundle();
b.putString("which", "ShopBuildings");
intent.putExtras(b);
startActivity(intent);
finish();
}
});
Third Activity: and there is method which should setResult and close Activity
building2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.i("LOG_NEW: ", "" + getCurrentBuildingTable(1) + ", " + checkSlotTable(1));
if(getCurrentBuildingTable(1) && checkSlotTable(1) == -1) {
Intent returnIntent = getIntent();
returnIntent.putExtra("result", 1);
setResult(RESULT_OK, returnIntent);
finish();
}else if (checkSlotTable(1) == -1){
Log.i("LOG_NEW: ", "Building already exist");
}
else{
Log.i("LOG_NEW: ", "Not enough resources");
}
}
});
At the end there is onActivityResult() from MainActivity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i("LOG_RES: ", "Checking.. " + requestCode + ", " + resultCode);
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String result = data.getStringExtra("result");
Log.i("LOG_RES: ", result);
}
}
}
Whatever I'm doing I can't start if(resultCode == RESULT_OK) loop and resultCode is always 0..
Thanks for help
setResult must be called in Second Activity, since intent of second activity was passed in startActivityForResult.
However, you can delegate the result code of Third Activity to Second Activity, then to third.
Change your Second Activity to something like this:
shop_c1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getIntent());
Bundle b = new Bundle();
b.putString("which", "ShopBuildings");
intent.putExtras(b);
startActivityForResult(intent,1);
//Remove finish from here
}
});
then also add this in Second Activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1){
setResult(resultCode,data);
}
finish();
}
I'm having some trouble using the Camera intent in Android. I want to be able to take
a picture and to use the picture later (retreiving the URI). Here's the code:
newPicBut.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
PhotoChoiceActivity.this.launchCameraIntent();
}
});
public void launchCameraIntent()
{
//create camera intent
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//set fileUri
MediaManager manager = new MediaManager();
manager.setPictureName(currentItemToAdd.toString());
intent.putExtra(MediaStore.EXTRA_OUTPUT, manager.getOutputMediaFileUri());
startActivityForResult(intent, 100);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == 100){
if(resultCode == RESULT_OK){
Toast.makeText(getApplicationContext(), "Image saved to: " + data.getData(), Toast.LENGTH_LONG).show();
}
}
}
So basically the button calls the launchCameraIntent() method. The MediaManager creates a file, and I use the file URI in the intent.putExtra to indicate where the file should be saved. The Camera intent is called, but here is the problem: the onActivityResult() only gets triggered when I cancel my camera intent. So it doesn't get triggered at all when I accept the picture that I've taken.
Hope you can help!