如何从android中的图库中选择视频图像

时间:2013-04-02 13:33:35

标签: android android-gallery

我有一个问题,我想在aNDROID中从图库中选择图片和视频 我使用了以下代码,但没有成功。

  1. setType("*/*);
  2. setType("video/*");
  3. setType("image/*");
  4. setType("image/* , video/*");
  5. setType("image/* video/*");
  6. Intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, 1);
  7. Intent i= new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); i.setType("*/*"); startActivityForResult(i, RESULT_LOAD_IMAGE);

3 个答案:

答案 0 :(得分:8)

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*,video/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);

您可以在this question.

找到更多详情

答案 1 :(得分:0)

以下代码可供我在片段中加载视频:

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

public void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (resultCode == RESULT_OK) {
       if (requestCode == LOAD_TESTING_VIDEO) {
           // Get the Video from data
           Uri selectedVideo = data.getData();
           String[] filePathColumn = {MediaStore.Video.Media.DATA};
           Cursor cursor = getActivity().getContentResolver().query(selectedVideo, filePathColumn, null, null, null);
           if (cursor != null) {
               cursor.moveToFirst();
               int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
               mediaPath1 = cursor.getString(columnIndex);
               //uptestVideoName is a EditText
               upTestVideoName.setText(mediaPath1);
               cursor.close();
           }
        }
    }
}

答案 2 :(得分:0)

    case SELECT_IMAGE:
            intent = createGetFileIntent(SELECT_IMAGE, "image/*");
            startForResult(intent, 0);
            break;
        case SELECT_VIDEO:
            intent = createGetFileIntent(SELECT_VIDEO, "video/*");
            startForResult(intent, 1);
            break;



    private Intent createGetFileIntent(int fileType,
                                   String mimeType) {
    Intent intent = new Intent(Intent.ACTION_PICK);

    if (android.os.Build.VERSION.SDK_INT >= 11) {
        intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
    }

    intent.setType(mimeType);

    switch (fileType) {
        case SELECT_IMAGE:
            intent = Intent.createChooser(intent, getString(R.string.title_select_image));
            break;
        case SELECT_VIDEO:
            intent = Intent.createChooser(intent, getString(R.string.title_select_video));
            break;
        default:
            break;
    }

    return intent;
}