E.Bolandian
E.Bolandian

Reputation: 573

Android - how to capture selected video by intent

I'm writting a code that the user can upload video from his phone.

My only problem is that i dont know how to get/save the file.

With the intent the user access to his libary and choose his video. But then i dont know how to get what he choose

Here is my code

    @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    btnVideo.setOnClickListener(this);
    btnAudio.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    int id = view.getId();
    switch (id){
        case R.id.btnUploadVideo:
            if(!checkStoragePermission())return;
            uploadVideo();
            break;
        case R.id.btnUploadAudio:
            break;
    }

}

private void uploadVideo() {
    Intent intent = new Intent();
    intent.setType("video/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent,"Select Video"),OPEN_MEDIA_PICKER);
}

private boolean checkStoragePermission(){
    int resultCode = ActivityCompat.checkSelfPermission(getContext(),
            android.Manifest.permission.WRITE_EXTERNAL_STORAGE);

    boolean granted = resultCode == PackageManager.PERMISSION_GRANTED;

    if (!granted){
        ActivityCompat.requestPermissions(
                getActivity(),
                new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE},
                PICK_VIDEO_REQUEST /*Constant Field int*/
        );
    }
    return granted;
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == PICK_VIDEO_REQUEST && grantResults[0] == PackageManager.PERMISSION_GRANTED){
        uploadVideo();
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == PICK_VIDEO_REQUEST) {
            Uri selectedImageUri = data.getData();

        }
    }
}

What should i do on the "onActivityResult"? How should i save/get the user choose.

Upvotes: 0

Views: 69

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007554

My only problem is that i dont know how to get/save the file.

Your code has nothing to do with a file.

If you want the use to pick a file, use a third-party file-chooser library.

Your code is having the user choose a piece of content, and that content does not have to be a file, let alone a file that you have direct filesystem access to.

You are welcome to use data.getData() to get the Uri to the content, then use ContentResolver and openInputStream() to get an InputStream on that content, if that meets your needs.

Upvotes: 1

Related Questions