How to select a video from the gallery and get it&

2019-02-04 16:45发布

I can open the gallery with this code,

    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType("video/*");

    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Video"), PICK_VIDEO_REQUEST );

It works well on a Samsung Galaxy S5. But in some phones, images are displayed along with videos.

What changes do I need to do to the code to open the gallery to display only videos?

How can I get the real path of the selected video when the results are returned?

8条回答
闹够了就滚
2楼-- · 2019-02-04 17:24

Use this

Intent intent = new 
Intent(Intent.ACTION_PICK,MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
Intent.setType("video/*");
startActivityForResult(Intent.createChooser(intent,"SelectVideo"),REQUEST_TAKE_GALLERY_VIDEO);
查看更多
干净又极端
3楼-- · 2019-02-04 17:27

Here is the full code to get the video path after selecting from gallery.

Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Video"),REQUEST_TAKE_GALLERY_VIDEO);

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

                // OI FILE Manager
                filemanagerstring = selectedImageUri.getPath();

                // MEDIA GALLERY
                selectedImagePath = getPath(selectedImageUri);
                if (selectedImagePath != null) {

                    Intent intent = new Intent(HomeActivity.this,
                            VideoplayAvtivity.class);
                    intent.putExtra("path", selectedImagePath);
                    startActivity(intent);
                }
            }
        }
    }

    // UPDATED!
    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Video.Media.DATA };
        Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
        if (cursor != null) {
            // HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
            // THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
            int column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } else
            return null;
    }
查看更多
聊天终结者
4楼-- · 2019-02-04 17:38
Intent intent = new Intent(Intent.ACTION_PICK,MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
                    fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
                    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

startActivityForResult(intent,
                            CAMERA_CAPTURE_VIDEO_REQUEST_CODE);


@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri selectedImageUri = data.getData();
imagePath = getPath(selectedImageUri);

}

  public String getPath(Uri uri) {
            String[] projection = { MediaStore.Video.Media.DATA };
            Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
            if (cursor != null) {

                int column_index = cursor
                        .getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
                cursor.moveToFirst();
                return cursor.getString(column_index);
            } else
                return null;
        }
查看更多
等我变得足够好
5楼-- · 2019-02-04 17:39
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Video.Media.EXTERNAL_CONTENT_URI);

startActivityForResult(intent , SELECT_VIDEO);
查看更多
爷、活的狠高调
6楼-- · 2019-02-04 17:40

Below code works for me:

Intent videoPickIntent = new Intent(Intent.ACTION_PICK);
videoPickIntent.setType("video/*");
TheActivity.startActivityForResult(Intent.createChooser(videoPickIntent, "Please pick a video"), PickIntroductionVideo);

and in onActivityResult:

if (requestCode != PickIntroductionVideo)
    return;
if (resultCode != Activity.RESULT_OK)
    return true;
String pickedVideoUrl = FarayanUtility.getRealPathFromUri(getContext(), data.getData());

Method FarayanUtility.getRealPathFromUri is:

public static String getRealPathFromUri(Context context, Uri contentUri) {
    Cursor cursor = null;
    try {
        String[] proj = {MediaStore.Images.Media.DATA};
        cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}
查看更多
Luminary・发光体
7楼-- · 2019-02-04 17:40
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                Uri selectedVideoUri = data.getData();

                // OI FILE Manager
                String filemanagerstring = selectedVideoUri.toString();
                vidPath = filemanagerstring;

                // MEDIA GALLERY
                String selectedVideoPath = getPath(selectedVideoUri); 
                if (selectedVideoPath != null) {
                    vidPath = selectedVideoPath;
                }
            }
        }
    }

   //this function returns null when using IO file manager
    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Video.Media.DATA };
        getContentResolver();
        Cursor cursor = getApplicationContext().getContentResolver().query(uri, projection, null, null, null);
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index); 
        } else
            return null;
    }

if the getpath() function is returning "null" then its prolly cuz you haven't set the permissions right. What you need to do is in your manifest paste this:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Paste this before the <application> tag, then, once you load your app, go to settings->applications-> YourApp ->permissions and then set all the permissions to true. Once you do that, this code should work perfectly

查看更多
登录 后发表回答