1

I'm trying to add video editing to my android existing app. So I'm looking at this sample project: https://github.com/a914-gowtham/Android-video-trimmer

I got the original code to compile and run on my android phone

So I created some sample files:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".java.MLTrimVideo">

    <VideoView
        android:id="@+id/ml_video_view"
        android:layout_width="411dp"
        android:layout_height="728dp"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

And basic code, which I copied from the sample project above:


public class MLTrimVideo extends AppCompatActivity {
    private VideoView videoView;
    private MediaController mediaController;
    private static final String TAG = "MLTrimVideo";


    ActivityResultLauncher<Intent> takeOrSelectVideoResultLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result -> {
                if (result.getResultCode() == Activity.RESULT_OK &&
                        result.getData() != null) {
                    //somecode
                }
            }
         );



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mltrim_video);
        videoView = findViewById(R.id.ml_video_view);
        mediaController = new MediaController(this);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
            try {
                Intent intent = new Intent("android.media.action.VIDEO_CAPTURE");
                intent.putExtra("android.intent.extra.durationLimit", 30);
                takeOrSelectVideoResultLauncher.launch(intent);
            } catch (Exception e) {
                e.printStackTrace();
            }
    }
}

Now inside takeOrSelectVideoResultLauncher, I get RESULT_CANCELED instead of RESULT_OK. I can see that the original code use on onClick() rather than onActivityResult(). The whole project compiles and runs. Only problem is that I cannot get the camera to come on.

I'm calling this Activity from MainActivity

  private void launchVideoTrimmer() {
    Intent intent = new Intent(getApplicationContext(), MLTrimVideo.class);
    if (intent != null) {
      startActivity(intent);
    }
  }

So what do I change to get RESULT_OK instead of RESULT_CANCELED ?

Arun
  • 1,599
  • 5
  • 19
  • 33
  • What android version are you targetting? Could be related to https://stackoverflow.com/questions/63246442/android-11-r-return-empty-list-when-querying-intent-for-action-image-capture? – sleepystar96 Sep 06 '22 at 02:56

1 Answers1

1

Try This

Change

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_mltrim_video);
            videoView = findViewById(R.id.ml_video_view);
            mediaController = new MediaController(this);
    
        }

ActivityResultLauncher<Intent> takeOrSelectVideoResultLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result -> {
                if (result.getResultCode() == Activity.RESULT_OK &&
                        result.getData() != null) {
                    //somecode
                }
            }
         );

To

    @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            videoView = findViewById(R.id.ml_video_view);
            mediaController = new MediaController(this);
    
            Intent intent = new Intent("android.media.action.VIDEO_CAPTURE");
            intent.putExtra("android.intent.extra.durationLimit", 30);
            takeOrSelectVideoResultLauncher.launch(intent);
        }

ActivityResultLauncher<Intent> takeOrSelectVideoResultLauncher = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result -> {
                Log.e("check", " result :: " + result.getResultCode());
                if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) {
                    //somecode
                    videoView.setVideoURI(result.getData().getData());
                    videoView.start();
                    Log.e("check", " done :: ");
                }
            }
    );

also, remove onActivityResult method no need it's deprecated

Mayur
  • 332
  • 1
  • 8