user1445877
user1445877

Reputation: 83

How to record video using Intents?

I need to unserstand how I can record video programatically. Now I use this construction:

public class AndroidLearningActivity extends Activity {
    /** Called when the activity is first created. */   
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.main);

        Intent captureVideoIntent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
        startActivityForResult(captureVideoIntent, 100);
    }  

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Uri uri=data.getData();
        Log.e("result", "result:"+resultCode);

    }
}

When the application is opened then the camera will be opened too. I have record some video, but if I press "back" button on the device then the application crushes. Please, explain me, how can I do it? Thank you.

Upvotes: 0

Views: 100

Answers (2)

Vipul
Vipul

Reputation: 28093

You have problem in this statement

        Uri uri=data.getData();
        Log.e("result", "result:"+resultCode);

When you will press back button recording will be cancelled and you will get data.getData as null since no recording is done.So change your code to following.

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (data != null) {
            Uri uri = data.getData();
            Log.e("result", "result:" + resultCode);
        }

        super.onActivityResult(requestCode, resultCode, data);
    }

Upvotes: 1

Dheeresh Singh
Dheeresh Singh

Reputation: 15701

looks as you are pressing back key and data (intent) not get set.so data may be null here

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK) { 
        Uri uri=data.getData();
        Log.e("result", "result:"+resultCode);
       }

    }

Upvotes: 1

Related Questions