VideoView won't play on a different activity - java

I have this button which should play the video on another activity or to be specific in Main2Activity. The problem is that the video wouldn't play when I click the button. Everything is working fine, the only problem I have is when I click that said button.
MainActivity:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
final Uri videoUri = data.getData();
if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
resultvideo.setVideoURI(videoUri);
mediacontroller.setAnchorView(resultvideo);
resultvideo.pause();
}
buttonPlay = (Button) findViewById(R.id.buttonPlay);
{
buttonPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediacontroller.show();
mediacontroller.setAnchorView(resultvideo);
resultvideo.start();
}
});
}
buttonFullScreen = (Button) findViewById(R.id.buttonFullScreen);
{
buttonFullScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
intent.putExtra("VIDEO_URI", videoUri.toString());
startActivity(intent);
}
});
}
}
Main2Activity:
public class Main2Activity extends Activity {
Button buttonPlay;
Button buttonFullScreen;
static final int REQUEST_VIDEO_CAPTURE = 1;
VideoView resultvideo;
MediaController mediacontroller;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String uri = getIntent().getStringExtra("VIDEO_URI");
Uri videoUri = Uri.parse(uri);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main2);
resultvideo = (VideoView)findViewById(R.id.videoView2);
resultvideo.start();
}
}

you will need to add
resultvideo.setVideoURI(videoUri);
after you initialed the resultvideo.

Related

Start activity from fragment, then get callback

I am trying to get a callback from an activity that I am starting with an intent in my fragment.
I thought I could do this with onActivityResult but it doesn't seem to get called when I finish(); on the activity. Is this possible in a fragment?
Fragment.java
#Nullable
#Override
public View onCreateView(final LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
getActivity().setTitle("Waiver");
myView = inflater.inflate(R.layout.waiver_layout, container, false);
signBtn = myView.findViewById(R.id.signBtn);
signBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), SignatureActivity.class);
intent.putExtra("signatureAbleId", device.id);
intent.putExtra("signatureAbleType", "App\\Models\\Device");
startActivity(intent);
}
});
return myView;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
Log.v("Activity", "The activity has finished");
if(resultCode == 200){
saveWaiver();
}
}
private void saveWaiver(){
Log.v("Save Waiver", "Saving waiver for you.");
}
SignatureActivity.java
public class SignatureActivity extends AppCompatActivity {
private Button btnClear;
private Button btnSave;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.activity_signature);
Bundle bundle = getIntent().getExtras();
if(bundle != null){
signatureAbleId = bundle.getInt("signatureAbleId");
signatureAbleType = bundle.getString("signatureAbleType");
}
btnClear = findViewById(R.id.btnClear);
btnSave = findViewById(R.id.btnSave);
btnClear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mSignaturePad.clear();
}
});
btnSave.setOnClickListener(new View.OnClickListener() {
setResult(200);
finish();
});
}
}
Use this. It will help you
Intent intent = new Intent(getContext(), SignatureActivity.class);
Instead of
Intent intent = new Intent(getActivity(), SignatureActivity.class);
And Yes need to change :
startActivity to startActivityForResult
Use startActivityForResult instead of startActivity
startActivityForResult(intent, 11);
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == 11){
if(resultCode == 200){
saveWaiver();
}
}else{
super.onActivityResult(requestCode, resultCode, data)
}
}

Play video on the next Activity with fullscreen button

Everything works fine without the Main2Activity but what I want to do is to play the video on Main2Activity when I click the fullscreen button. Everything works well on MainActivity but when I click the fullscreen button it crashes. Not sure why. I'm new to Android development, any help would be appreciated.
MainActivity:
public class MainActivity extends AppCompatActivity {
Button buttonPlay;
Button buttonFullScreen;
static final int REQUEST_VIDEO_CAPTURE = 1;
VideoView resultvideo;
MediaController mediacontroller;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.setContentView(R.layout.activity_main);
setContentView(R.layout.activity_main);
resultvideo = (VideoView)findViewById(R.id.videoView);
mediacontroller = new MediaController(MainActivity.this);
mediacontroller.setAnchorView(resultvideo);
resultvideo.setMediaController(mediacontroller);
Button click = (Button)findViewById(R.id.buttonRecord);
resultvideo = (VideoView)findViewById(R.id.videoView);
}
public void dispatchTakeVideoIntent(View v) {
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
Uri videoUri = data.getData();
Log.i("test","111111111111111" + videoUri.toString());
resultvideo.setVideoURI(videoUri);
mediacontroller.setAnchorView(resultvideo);
resultvideo.pause();
}
buttonPlay = (Button) findViewById(R.id.buttonPlay);
{
buttonPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediacontroller.show();
mediacontroller.setAnchorView(resultvideo);
resultvideo.start();
Log.i("test","111111111111111");
}
});
}
buttonFullScreen = (Button) findViewById(R.id.buttonFullScreen);
{
buttonFullScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Main2Activity.class));
}
});
}
}
}
Main2Activity:
public class Main2Activity extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main2);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
Uri videoUri = data.getData();
Log.i("test", "111111111111111" + videoUri.toString());
resultvideo.setVideoURI(videoUri);
mediacontroller.setAnchorView(resultvideo);
resultvideo.pause();
}
buttonFullScreen = (Button) findViewById(R.id.buttonFullScreen);
{
buttonFullScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediacontroller.show();
mediacontroller.setAnchorView(resultvideo);
resultvideo.start();
Log.i("test","111111111111111");
}
});
}
}
}
resultvideo is declared in MainActivity, it is not available in Main2Activity
mediacontroller and resultvideo are declared in MainActivity where they are in Main2Activity? You must have them in layout for Main2Activity also, as you have them in MainActivity layout and finding them by ids in Main2Activity is also necessary.

finish() does not close current activity at first time

I'm trying to develop an app with two activities which they send data to each other. the problem is that I want tofinish(); second activity. when I click on goBackButton_ which I defined in XML_ SecondActivity comes up, when I click on the button again, it comes back to MainActivity as I wanted. why? and How to solve it?why it doesn't work at first time?
public class MainActivity extends AppCompatActivity {
Button button;
int REQUEST_CODE = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("FirstActivity", "Hello from first activity");
startActivity(intent);
startActivityForResult(intent, REQUEST_CODE);
Log.d("TAG", "onClickListener: done!");
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//super.onActivityResult(requestCode, resultCode, data);
Log.d("TAG", "onActivityResult: Entered");
if(requestCode == REQUEST_CODE) {
Log.d("TAG", "onActivityResult: requestCode is OK");
if (resultCode == RESULT_OK) {
Log.d("TAG", "onActivityResult: result is returned OK");
String result = data.getStringExtra("SecondActivity");
Toast.makeText(this, result, Toast.LENGTH_LONG).show();
Log.d("TAG", "onActivityResult: done!");
}
}
}
}
public class SecondActivity extends AppCompatActivity {
TextView textView;
Button goBackButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
goBackButton = (Button) findViewById(R.id.goBackButton);
goBackButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("TAG", "onClick: Entered");
Intent returnIntent = getIntent();
returnIntent.putExtra("SecondActivity", "from second Activity");
setResult(RESULT_OK, returnIntent);
Log.d("TAG", "onClick: Message sent");
finish();
Log.d("TAG", "onClick: Activity finished");
//Intent myIntent = new Intent(SecondActivity.this,MainActivity.class);
//startActivity(myIntent);
}
});
textView = (TextView)findViewById(R.id.textView);
Bundle extras = getIntent().getExtras();
if(extras != null){
String string = extras.getString("FirstActivity");
textView.setText(string);
}
}
}
solved
The problem is in your code -
startActivity(intent);
startActivityForResult(intent, REQUEST_CODE);
When you want to do startActivityForResult you don't have to do startActivity as this will first open your second activity without looking for result.
Remove startActivity(intent);
you have added startActivity(intent) as well as startActivityforResult(intent, REQUEST_CODE) which creates double instance of an activity use only startActivityforResult(intent, REQUEST_CODE);
Try remove startActivity(intent);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("FirstActivity", "Hello from first activity");
startActivityForResult(intent, REQUEST_CODE);
Log.d("TAG", "onClickListener: done!");
}
});

Sending Zxing scanResult to another activity

I have tried everything and whatever I could to solve this problem. At last I am posting it here to get a solution (New to Android).
I have made an android scanner app and I am using ZXing open source code. The problem is after scan I am trying to send the scan result to another activity but unable to do.
Here is my code:
public class MainActivity extends AppCompatActivity
implements ZXingScannerView.ResultHandler, NavigationView.OnNavigationItemSelectedListener {
private ZXingScannerView mScannerView;
private int CALL_SCANNER_APP;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Scan Button code
public void onClick(View v) {
ZXingScannerView mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
mScannerView.setResultHandler(this);
mScannerView.startCamera();
//startActivityForResult(mScannerView1, CALL_SCANNER_APP);
}
#Override
protected void onPause (){
super.onPause();
mScannerView.stopCamera();
}
#Override
public void handleResult(Result result) {
ResultActivity.tvresult.setText(result.getText());
/*Log.w("handleReuslt", result.getText());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Scan Result");
builder.setMessage(result.getText());
AlertDialog alertDialog = builder.create();
//alertDialog.show();
builder.setPositiveButton("Result", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
}
});
builder.setNegativeButton("OK", null).show();*/
//Resume Scanning
//mScannerView.resumeCameraPreview(this);
}
There is one method which send results from One activity to other activity is scanActivityForResult() but in my case I am not using intent on public void onClick(View v)
So how do I achieve this.
Thanks!
Use Below code into the button click.
Intent intent = new Intent(SelectOptionActivity.this, CaptureActivity.class);
intent.putExtra("SCAN_MODE", "ONE_D_MODE");
intent.putExtra("SCAN_FORMATS", "CODE_39,CODE_93,CODE_128,DATA_MATRIX,ITF,CODABAR,EAN_13,EAN_8,UPC_A,QR_CODE");
intent.setAction(Intents.Scan.ACTION);
startActivityForResult(intent, 1);
And override this method to get result of scanning.
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 1 && resultCode == RESULT_OK) {
final String contents = intent.getStringExtra(Intents.Scan.RESULT);
final String formatName = intent.getStringExtra(Intents.Scan.RESULT_FORMAT);
}
}
in handleDecodeInternally you directly intent the Capture Activity to desired Activity
private void handleDecodeInternally(Result rawResult, ResultHandler resultHandler, Bitmap barcode) {
maybeSetClipboard(resultHandler);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
if (resultHandler.getDefaultButtonID() != null && prefs.getBoolean(PreferencesActivity.KEY_AUTO_OPEN_WEB, false)) {
resultHandler.handleButtonPress(resultHandler.getDefaultButtonID());
return;
}
statusView.setVisibility(View.GONE);
viewfinderView.setVisibility(View.GONE);
resultView.setVisibility(View.GONE);
Intent intent = new Intent(CaptureActivity.this, AfterCaptureActivity.class);
startActivity(intent);
finish();

Choose file from directory

How i can choose image from gallery and them put it on parse.com ?
There i open GALLERY:
#Override
public void onClick(View arg0) {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 1);
i.setType("image/*");
i.setAction(Intent.ACTION_VIEW);
///// - here must be code tat can choose file that i must upload to parse
////
There is code that can upload image to parse.com :
I think it will be something like this
ParseObject image = new ParseObject("guestList");
image.put("photo", image);
image.saveInBackground();
UPD
public class SendGuestPhoto extends Activity {
ImageView imageView;
private static final int PICK_IMAGE = 100;
String path;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send_guest_photo);
imageView = (ImageView) findViewById(R.id.image_view);
Button button_choose_guest_photo = (Button)findViewById(R.id.button_choose_guest_photo);
button_choose_guest_photo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openGallery();
}
});
}
private void openGallery() {
Intent gallery =
new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, PICK_IMAGE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
Uri imageUri = data.getData();
imageView.setImageURI(imageUri);
}
}
public void SendPhoto(){
Button button_send_guest_photo = (Button)findViewById(R.id.button_send_guest_photo);
button_send_guest_photo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ParseObject photos = new ParseObject("guestsImages");
photos.put("photos", ????????????????);
photos.saveInBackground();
}
});
}
}

Categories

Resources