【问题标题】:Uploading a video to parse server , using the Camera Intent使用 Camera Intent 上传视频到解析服务器
【发布时间】:2020-08-08 08:40:15
【问题描述】:

我正在尝试使用相机意图上传视频以解析服务器以捕获视频。 代码如下。现在我的问题是视频正在使用 videoUri 在 vi​​deoView 中播放,但它没有上传到服务器。我收到一个 FileNotFoundException 说“不存在这样的文件或目录”, 例子:- I/info: content://media/external/video/media/57463 //这是videoUri的日志输出// W/System.err:java.io.FileNotFoundException:/external/video/media/57463(没有这样的文件或目录) 有人可以帮忙解决这个问题吗?

public class VideoActivity extends AppCompatActivity {
static final int REQUEST_VIDEO_CAPTURE = 1;
Uri videoUri,vUri;
ParseUser currentUser;
VideoView videoView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_video);
    currentUser=ParseUser.getCurrentUser();
    Button captureVideo = (Button) findViewById(R.id.captureVideo);
    videoView=(VideoView) findViewById(R.id.videoView);
    Button saveButton=(Button) findViewById(R.id.saveButton);
    //save Button clicks handled here.
    saveButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(videoUri!=null){
               Log.i("info", ""+videoUri);
                byte[] bytes = convertVideoToBytes(videoUri);
                //now lets try and add this uri file to the parse server in a parsefile.
                ParseFile parseVideoFile = new ParseFile("video.mp4", bytes);
                parseVideoFile.saveInBackground();
                currentUser.put("video", parseVideoFile);
                currentUser.saveInBackground();
            }else{
                Toast.makeText(VideoActivity.this,"No Video to save",Toast.LENGTH_LONG).show();
            }
        }
    });
    //capture video button click is handled here.
    captureVideo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
            if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
                startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
            }
        }
    });
}
    //onActivityResult is shown here.
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
        if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {

            videoUri = intent.getData();
            videoView.setVideoURI(videoUri);
            videoView.start();

        }
    }

//this is to convert the videoUri to byte[] arrays.

public static byte[] convertVideoToBytes( Uri videoUri) {
        byte[] videoBytes = null;
        File inputFile=new File(videoUri.getPath());
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            FileInputStream fis = new FileInputStream(inputFile);
            byte[] buf = new byte[(int)inputFile.length()];
            int n;
            while (-1 != (n = fis.read(buf)))
                baos.write(buf, 0, n);

            videoBytes = baos.toByteArray();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return videoBytes;
}

}

【问题讨论】:

  • content://media/external/video/media/57463 这是一个不错的内容方案。一个 uri。
  • /external/video/media/57463 这是内容方案的最后一部分。它不是文件系统路径,因此不能在其上使用 File 和 FileOutputSteam 类。
  • 不是在 uri 的一部分上打开 FileInputStream,而是为 uri 本身打开 InputStream。然后以与 FileInputStream 相同的方式从 InputStream 中读取。
  • InputStream is = getContentResolver().openInputStream(uri);
  • 非常感谢,代码就像一个魅力。我能够将视频上传到解析服务器,但现在我必须尝试从服务器下载它并在我的应用程序中播放视频..如果你也有解决方案吗?可以分享一下吗?

标签: android android-studio parse-platform video-capture android-camera-intent


【解决方案1】:

这是我的代码..很抱歉将其发布为答案,但我不知道如何发布代码。

      public class ViewActivity extends AppCompatActivity {
      VideoView finalView;
      Button button_play;
      ParseUser currentUser;
      String videoString;
      Uri videoUri,contentUri, fileProvider;
      File videoFile;
      public final String APP_TAG = "MyCustomApp";
      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_view);
      finalView=(VideoView) findViewById(R.id.finalview);
      button_play=(Button) findViewById(R.id.button_play);

   
      // Create a File reference for future access
      videoFile = getVideoFileUri();
      fileProvider = FileProvider.getUriForFile(ViewActivity.this,
            "com.codepath.fileprovider", videoFile);
    
      currentUser= ParseUser.getCurrentUser();
      ParseFile parseFile = (ParseFile)currentUser.get("videos");
      parseFile.getDataInBackground(new GetDataCallback() {
        @Override
        public void done(byte[] data, ParseException e) {
            try {
                 videoUri=convertBytesToUri(data,fileProvider);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    });

    button_play.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            finalView.setVideoURI(videoUri);
            finalView.start();
        }
    });
}
    public Uri convertBytesToUri(byte[] data,Uri fileProvider) throws IOException {
    OutputStream os=getContentResolver().openOutputStream(fileProvider);

    os.write(data);
    os.flush();
    return   fileProvider;
    }
    // Returns the File for a photo stored on disk given the fileName
    public File getVideoFileUri() {
    // Get safe storage directory for photos
    // Use `getExternalFilesDir` on Context to access package-specific directories.
    // This way, we don't need to request external read/write runtime permissions.
    File mediaStorageDir = new File(getExternalFilesDir(Environment.DIRECTORY_MOVIES), 
    APP_TAG);
    //Let's create a unique fileName.
    String timeStamp=new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String fileName=timeStamp+".mp4";
    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists() && !mediaStorageDir.mkdirs()){
        Log.d(APP_TAG, "failed to create directory");
    }

    // Return the file target for the photo based on filename
    File file = new File(mediaStorageDir.getPath() + File.separator + fileName);

    return file;
}

【讨论】:

  • if (!mediaStorageDir.exists() && !mediaStorageDir.mkdirs()){ Log.d(APP_TAG, "failed to create directory"); } 那应该是:if (!mediaStorageDir.exists() && !mediaStorageDir.mkdirs()){ Log.d(APP_TAG, "failed to create directory"); return null; }。调用者应该检查 null。
猜你喜欢
  • 2021-07-07
  • 2018-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-30
  • 2015-11-26
相关资源
最近更新 更多