android中的视频上传问题
我正在尝试将一个mp4文件从sd卡上载到远程服务器。上传很成功,但当我尝试使用VideoView通过url播放该文件时,它显示“无法播放此视频”。这个问题只发生在使用手机拍摄的视频上,假设我从watsapp文件夹上传视频,一切正常,没有任何问题。上传前我需要做任何压缩吗? 这是我用来上传视频的代码android中的视频上传问题,android,video,upload,video-streaming,Android,Video,Upload,Video Streaming,我正在尝试将一个mp4文件从sd卡上载到远程服务器。上传很成功,但当我尝试使用VideoView通过url播放该文件时,它显示“无法播放此视频”。这个问题只发生在使用手机拍摄的视频上,假设我从watsapp文件夹上传视频,一切正常,没有任何问题。上传前我需要做任何压缩吗? 这是我用来上传视频的代码 try { FileInputStream fileInputStream = new FileInputStream(sourceFile);
try {
FileInputStream fileInputStream = new FileInputStream(sourceFile);
URL url = new URL(my url for upload);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestProperty("myFile", fileName);
video_name = fileName;
video_name = fileName.substring(fileName.lastIndexOf("/")+1);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"myFile\";filename=\"" + fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available();
Log.i("ava", "Initial .available : " + bytesAvailable);
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
while (bytesRead > 0) {
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
serverResponseCode = conn.getResponseCode();
Content = conn.getResponseMessage();
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
是的,这可能是因为您的URL包含空格,所以您只需删除空格并设置 %二十 代替空白,如: URL url1=新URL(您的_URL.trim().replace(“,“%20”)
希望它对您有用您是否验证了视频已成功上传到远程服务器?@HeshanSandeepa是的,它也可以在浏览器中播放。但我觉得有些问题存在,有时会让人感到结巴。那么,这里真正的问题是什么呢?“无法播放此视频”或“卡住”?@HeshanSandeepa在web浏览器中播放上载的视频时发生卡住。如果用户尝试在android应用程序的VideoView中加载该url,则会显示无法播放该视频。请发布该url好吗
try {
// Start the MediaController
MediaController mediacontroller = new MediaController(
PlayVideoActivity.this);
mediacontroller.setAnchorView(videoview);
// Get the URL from String VideoURL
Uri video = Uri.parse(VideoURL);
videoview.setMediaController(mediacontroller);
videoview.setVideoURI(video);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
videoview.requestFocus();
videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
pgDialog.dismiss();
videoview.start();
}
});