从Android设备库中获取视频react-native

时间:2016-03-10 12:16:28

标签: android video react-native video-capture

我需要从设备上获取视频。 我试过了:

 const fetchParams = {
        first: 25000000,
         assetType:'Videos', ->not working, gets only images 
    };
    CameraRoll.getPhotos(fetchParams, this.storeImages, this.logImageError);

但只有图片,我也尝试过:

var options = {
  title: 'Select Image',
  cancelButtonTitle: 'Cancel',
  takePhotoButtonTitle: 'Take Photo...', 
  chooseFromLibraryButtonTitle: 'Choose from Library...',
  mediaType: 'video', // 'photo' or 'video'
  videoQuality: 'high', // 'low', 'medium', or 'high'
 };
UIImagePickerManager.launchImageLibrary(options, (response) => {}

2 个答案:

答案 0 :(得分:1)

试试这个:

Intent openGal = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(openGal, REQUEST_CODE);

这将打开您视频中的图库,以便您可以从中选择一个,然后您可以在onActivityResult

中获取所选视频uri
if (requestCode == 101 && resultCode == RESULT_OK) {
        Uri VideoData = data.getData();
}

答案 1 :(得分:0)

我添加了一些行

@ReactMethod
  public void launchVideoLibrary(final ReadableMap options, final Callback callback){
      response = Arguments.createMap();
      Intent intent = new Intent();
      intent.setType("video/*");
      intent.setAction(Intent.ACTION_GET_CONTENT);
      mMainActivity.startActivityForResult(Intent.createChooser(intent, "Select Video"), REQUEST_LAUNCH_VIDEO_LIBRARY);
//      response.putString("error", "Cannot launch video library");
//      callback.invoke(response);
//
      mCallback = callback;
  }

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    //robustness code

      if (requestCode == REQUEST_LAUNCH_VIDEO_LIBRARY) {
          Uri VideoData = data.getData();

          String realPath = getRealPathFromURI(VideoData);
          boolean isUrl = false;

          if (realPath != null) {
              try {
                  URL url = new URL(realPath);
                  isUrl = true;
              } catch (MalformedURLException e) {
                  // not a url
              }
          }

          if (realPath ==  null || isUrl) {
              try {
                  File file = createFileFromURI(VideoData);
                  realPath = file.getAbsolutePath();
                  VideoData = Uri.fromFile(file);
              }
              catch(Exception e) {
                  response.putString("error", "Could not read video");
                  response.putString("uri", VideoData.toString());
                  mCallback.invoke(response);
                  return;
              }
          }
          response.putString("uri", VideoData.toString());
          response.putString("path", realPath);
          mCallback.invoke(response);



          return;
      }

在RN文件中:

 UIImagePickerManager.launchVideoLibrary(options, (response) => {
  console.log('Response = ', response);});