gpt4 book ai didi

android - android中的视频上传问题

转载 作者:太空狗 更新时间:2023-10-29 14:46:11 24 4
gpt4 key购买 nike

我正在尝试从 SD 卡上传一个 mp4 文件到远程服务器。上传成功,但当我尝试使用 VideoView 通过 url 播放该文件时,它显示“无法播放此视频”。只有使用手机拍摄的视频才会出现此问题,假设我从 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();
}

最佳答案

是的,它可能会发生,因为您的 URL 包含空格,因此您只需要删除空格并设置

%20

insted of white-space like :

URL url1 = new URL(your_url.trim().replace(" ", "%20"));

希望对你有用

关于android - android中的视频上传问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40016790/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com