我正在尝试将sd卡上的mp4文件上传到远程服务器。上传成功,但是当我尝试使用VideoView通过网址播放该文件时,它显示"无法播放此视频"。这个问题只发生在使用手机拍摄的视频中,假设我是从watsapp文件夹上传视频,一切正常,没有任何问题。上传前我需要进行任何压缩吗? 以下是我用于上传视频的代码
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();
}
答案 0 :(得分:0)
是的,可能是因为您的网址包含空格,因此您需要删除空格并设置
20%
像白色空间一样:
网址url1 =新网址(your_url.trim()。replace(“”,“%20”));
希望它适合你
答案 1 :(得分:0)
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();
}
});