繁体   English   中英

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

[英]get Videos from Android device gallery react-native

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

 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) => {}

尝试这个 :

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();
}

我添加了一些行

@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);});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM