-1

我有一个问题,我想从 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);
4

3 回答 3

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

你会在这个问题上找到更多细节。

于 2013-06-25T11:18:39.003 回答
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;
}
于 2019-11-18T11:36:59.813 回答
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();
           }
        }
    }
}
于 2017-03-08T05:38:09.213 回答