onActivityResult not being called in activity - java

I am recording/selecting a video in my RecordVideo.java activity. Once the video is recorded/selected the I press the upload button and go to the PostActivity.java file. But once I'm in there for some reason onActivityResult isn't being called. I tried to toast and Log.d for some type of error or info but nothing came up.
I checked the answer on here to the same problem I'm having but none of them worked for me.
Can someone please help ?
RecordVideo.java:
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
if (requestCode == REQUEST_CODE_VIDEO_CAPTURE && resultCode == RESULT_OK && data.getData() != null) {
videoUri = data.getData();
videoView.setVideoURI(videoUri);
videoView.start();
} else if (requestCode == 5 && resultCode == RESULT_OK && data != null && data.getData() != null) {
videoUri = data.getData();
videoView.setVideoURI(videoUri);
videoView.start();
}
super.onActivityResult(requestCode, resultCode, data);
}
private void uploadUserVideo(Uri videoUri) {
if (videoUri != null) {
Intent intent = new Intent(RecordVideo.this, PostActivity.class);
intent.putExtra("videoUri", videoUri.toString());
startActivity(intent);
finish();
} else {
Toast.makeText(this, "It's null", Toast.LENGTH_SHORT).show();
}
}
PostActivity.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post);
Bundle extras = getIntent().getExtras();
if (extras != null) {
//videoUri = extras.getInt(videoUri);
videoUri = Uri.parse(extras.getString("videoUri"));
//Toast.makeText(this, "It's not null", Toast.LENGTH_SHORT).show();
} else {
// handle case
Toast.makeText(this, "Post null", Toast.LENGTH_SHORT).show();
}
videoView = findViewById(R.id.video_added);
MediaController mediaController = new MediaController(this);
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
close = findViewById(R.id.close);
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(PostActivity.this, MainActivity.class));
finish();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
Log.d("TAG3", "I'm here." + requestCode);
if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {
videoUri = data.getData();
videoView.setVideoURI(videoUri);
videoView.start();
Toast.makeText(this, "No problem.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "A problem.", Toast.LENGTH_SHORT).show();
}
super.onActivityResult(requestCode, resultCode, data);
}

Related

pick two images separately in Android

I'm making and android app where I need two upload images and save them to server.
When I select it from the first button, it is appeared in the second but I need two different images.
photoUpload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chooseImage();
}
});
photoUpload2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chooseImage();
}
});
}
private void chooseImage() {
Intent openGalleryIntent = new Intent();
openGalleryIntent.setType("image/*");
openGalleryIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(openGalleryIntent, "Select Picture"), GALLERY_REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_REQUEST_CODE && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
imageView.setImageBitmap(bitmap);
imageView2.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
You are getting this because you are setting selected image to imageview 1 and 2. to solve this you can use different request codes or use Boolean variable to differentiate the uploading of image 1 and 2
Define two Boolean variable to differentiate the uploading of image 1 and 2
Boolean first = false, second = false;
#Override
public void onClick(View v) {
first = true;
chooseImage();
}
});
photoUpload2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
second = true;
chooseImage();
}
});
}
private void chooseImage() {
Intent openGalleryIntent = new Intent();
openGalleryIntent.setType("image/*");
openGalleryIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(openGalleryIntent, "Select Picture"), GALLERY_REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_REQUEST_CODE && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
if(first)
{
imageView.setImageBitmap(bitmap);
first = false;
}else if(second)
{
imageView2.setImageBitmap(bitmap);
second = false;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
You can use library to select multiple images from Gallery. There are many libraries that are available, one such is https://github.com/ParkSangGwon/TedPicker
Hope this will help you!
Happy Coding
You are setting same image to both ImageViews.
Change Your Code to
photoUpload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chooseImage(GALLERY_REQUEST_CODE_1);
}
});
photoUpload2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
chooseImage(GALLERY_REQUEST_CODE_2);
}
});
}
private void chooseImage(int requestCode) {
Intent openGalleryIntent = new Intent();
openGalleryIntent.setType("image/*");
openGalleryIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(openGalleryIntent, "Select Picture"), requestCode);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_REQUEST_CODE_1 && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
if (requestCode == GALLERY_REQUEST_CODE_2 && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
imageView2.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
I think you have to set same bitmap in both imageView
You should try bellow code
boolean isFromFirstBtn;
photoUpload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
isFromFirstBtn = true;
chooseImage();
}
});
photoUpload2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
isFromFirstBtn = false;
chooseImage();
}
});
}
private void chooseImage() {
Intent openGalleryIntent = new Intent();
openGalleryIntent.setType("image/*");
openGalleryIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(openGalleryIntent, "Select Picture"), GALLERY_REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_REQUEST_CODE && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
if(isFromFirstBtn){
imageView.setImageBitmap(bitmap);
}else{
imageView2.setImageBitmap(bitmap);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

Multiple selected file in webview shows no file chosen

I want to upload multiple images in webview and i can select many file but when i chose file and tap open Webpage Input show no file.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
super.onActivityResult(requestCode, resultCode, intent);
if(Build.VERSION.SDK_INT >= 21){
Uri[] results = null;
//Check if response is positive
if(resultCode== Activity.RESULT_OK){
if(requestCode == FCR){
if(null == mUMA){
return;
}
if(intent == null){
//Capture Photo if no image available
if(mCM != null){
results = new Uri[]{Uri.parse(mCM)};
}
}else{
String dataString = intent.getDataString();
if(dataString != null){
results = new Uri[]{Uri.parse(dataString)};
}
}
}
}
mUMA.onReceiveValue(results);
mUMA = null;
}else{
if(requestCode == FCR){
if(null == mUM) return;
Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
mUM.onReceiveValue(result);
mUM = null;
}
}
}
and my onShowFileChooser code is
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public boolean onShowFileChooser(
WebView webView, ValueCallback<Uri[]> filePathCallback,
WebChromeClient.FileChooserParams fileChooserParams){
if(mUMA != null){
mUMA.onReceiveValue(null);
}
mUMA = filePathCallback;
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType("*/*");
contentSelectionIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
startActivityForResult(chooserIntent, FCR);
return true;
}
i searched on internet but can't find any suitable answer.

When my Speech recognition was done, it always changes into another activity

It always changes into another activity when speech recognition is done
This is my code. What must be the problem?
I can get the result from speech recog.when it is done but it automatically changes to CropActivity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQ_CODE_SPEECH_INPUT) {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
m_edtInputText.setText(result.get(0));
m_mic.setVisibility(View.VISIBLE);
sttProgress.setVisibility(View.INVISIBLE);
}
}
if (resultCode == Activity.RESULT_OK) {
Uri imageUri = getPickImageResultUri(data);
CropImage.activity(imageUri)
.setGuidelines(CropImageView.Guidelines.ON)
.start(this);
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_in_right);
}
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == Activity.RESULT_OK) {
Uri resultUri = result.getUri();
Intent intent = new Intent(this, CropActivity.class);
intent.putExtra("imageUri", resultUri);
startActivity(intent);
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
if(requestCode == MY_CHECK_DATA){
if(resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS){
m_TTS = new TextToSpeech(this,this);
}
else
{
Intent m_installTTSIntent = new Intent();
m_installTTSIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(m_installTTSIntent);
}
}
}

How to separate activities

I have several onActivityResult activities in my Android app and I am having an issue with two activities running at one time. When I open up my ZXing barcode scanner it will close the connection to my Bluetooth printer. Here is my code:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_ENABLE_BT:
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(this, "Bluetooth open successful", Toast.LENGTH_LONG).show();
} else {
finish();
}
break;
case REQUEST_CONNECT_DEVICE:
if (resultCode == Activity.RESULT_OK) {
String address = data.getExtras()
.getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
con_dev = mService.getDevByMac(address);
mService.connect(con_dev);
}
break;
}
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (scanningResult != null) {
String scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();
//formatTxt.setText("FORMAT: " + scanFormat);
edtContext.setText(scanContent);
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
What would be the best way to split up these activities so that they do not overlap each other?
UPDATE:
As #Mahfa stated I need to add a requestCode to the scanning activity. This is my code for the button that starts the scanning activity:
btnBarcode = (Button) findViewById(R.id.btnBarcode);
OnClickListener BarcodeOnClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
if (v.getId() == R.id.btnBarcode) {
IntentIntegrator integrator = new IntentIntegrator(PrintDemo.this);
integrator.initiateScan();
}
}
};
btnBarcode.setOnClickListener(BarcodeOnClickListener);
What do I need to do to add a requestCode to this?
#Mahfa put me on the right track with the links that he posted and eventually I found this code example that cleared up my issue http://www.programcreek.com/java-api-examples/index.php?source_dir=jdkernel-updater-master/src/jdkernel/ui/ThemeListNewActivity.java
Here is my completed code:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_ENABLE_BT:
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(this, "Bluetooth open successful", Toast.LENGTH_LONG).show();
} else {
finish();
}
break;
case REQUEST_CONNECT_DEVICE:
if (resultCode == Activity.RESULT_OK) {
String address = data.getExtras()
.getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
con_dev = mService.getDevByMac(address);
mService.connect(con_dev);
}
break;
case IntentIntegrator.REQUEST_CODE:
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (scanningResult != null) {
String scanContent = scanningResult.getContents();
String scanFormat = scanningResult.getFormatName();
//formatTxt.setText("FORMAT: " + scanFormat);
edtContext.setText(scanContent);
} else {
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
break;
}
I did not have to change anything on my OnClickListener. Thanks for pointing me in the right direction #Mahfa.

How To Detect User Cancel AccountPicker dialog Android Eclipse

i want user Email id via AccountPicker.newChooseAccountIntent. i want Detect User Cancel AccountPicker dialog
here is a code
private static final int REQUEST_CODE_EMAIL = 1;
private TextView email = (TextView) findViewById(R.id.email);
try {
Intent intent = AccountPicker.newChooseAccountIntent(null, null,
new String[] { GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE }, false, null, null, null, null);
startActivityForResult(intent, REQUEST_CODE_EMAIL);
} catch (ActivityNotFoundException e) {
// TODO
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_EMAIL && resultCode == RESULT_OK) {
String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
email.setText(accountName);
}
}
There is also a RESULT_CANCELED or RESULT_CANCEL constant in an Activity.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_EMAIL && resultCode == RESULT_OK) {
String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
email.setText(accountName);
} else if(requestCode == REQUEST_CODE_EMAIL && resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT).show();
}
}

Categories

Resources