在Android的视频上传 [英] video upload in Android
问题描述
我试图将视频上传到服务器,在这里我有存储在资产文件夹中的视频,并试图upload.i使用了下列code:
私人无效uploadVideo(){
字符串upLoadServerUri =HTTP://xxxxx/video_upload.php
//的String [] =串sourceFileUri;
字符串文件名=文件:/// android_asset / clip0003
HttpURLConnection的康恩= NULL;
DataOutputStream类DOS = NULL;
DataInputStream以inStream中= NULL;
字符串lineEnd =\\ r \\ n;
串twoHyphens = - ;
字符串边界=*****;
INT读取动作,方bytesAvailable,缓冲区大小;
INT serverResponse code = 1;
字节[]缓冲区;
INT MAXBUFFERSIZE = 1 * 1024 * 1024;
串responseFromServer =; 文件的资源文件=新的文件(文件名);
如果(!sourceFile.isFile()){
Log.e(Huzza,源文件不存在); } 尝试{//打开一个URL连接到这个Servlet 的FileInputStream的FileInputStream =新的FileInputStream(新文件(文件名));
网址URL =新的URL(upLoadServerUri);
康恩=(HttpURLConnection类)url.openConnection(); //打开HTTP连接的URL
conn.setDoInput(真); //允许输入
conn.setDoOutput(真); //允许输出
conn.setUseCaches(假); //不要使用缓存副本
conn.setRequestMethod(POST);
conn.setRequestProperty(连接,保持活动);
conn.setRequestProperty(ENCTYPE,多部分/表单数据);
conn.setRequestProperty(内容类型,的multipart / form-data的;边界=+边界);
conn.setRequestProperty(视频文件名);
DOS =新的DataOutputStream类(conn.getOutputStream()); dos.writeBytes(twoHyphens +边界+ lineEnd);
dos.writeBytes(内容处置:表格数据;名称= \\uploaded_file \\;文件名= \\+文件名+\\+ lineEnd);
dos.writeBytes(lineEnd); 参考bytesAvailable = fileInputStream.available(); //创建最大大小的缓冲区
Log.i(Huzza,初始.available:+信息bytesAvailable); BUFFERSIZE = Math.min(方bytesAvailable,MAXBUFFERSIZE);
缓冲区=新的字节[缓冲区大小] //读取文件,并将其写入形式...
读取动作= fileInputStream.read(缓冲液,0,缓冲区大小); 而(读取动作大于0){
dos.write(缓冲液,0,缓冲区大小);
参考bytesAvailable = fileInputStream.available();
BUFFERSIZE = Math.min(方bytesAvailable,MAXBUFFERSIZE);
读取动作= fileInputStream.read(缓冲液,0,缓冲区大小);
} //发送文件数据后necesssary多部分的表单数据...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens +边界+ twoHyphens + lineEnd); //服务器的响应(code和消息)
serverResponse code = conn.getResponse code();
串serverResponseMessage = conn.getResponseMessage(); Log.i(上传文件到服务器,HTTP响应是:+ serverResponseMessage +:+ serverResponse code);
//关闭流
Log.i(上传文件到服务器,文件名+文件写入);
fileInputStream.close();
dos.flush();
dos.close();
}赶上(MalformedURLException的前){
ex.printStackTrace();
Log.e(上传文件到服务器,错误:+ ex.getMessage(),除息);
}赶上(例外五){
e.printStackTrace();
}
//这个块会给上传链接的响应
尝试{
RD的BufferedReader =新的BufferedReader(新的InputStreamReader(conn.getInputStream()));
串线;
而((行= rd.readLine())!= NULL){
Log.i(Huzza,RES消息:+线);
}
rd.close();
}赶上(IOException异常ioex){
Log.e(Huzza,错误:+ ioex.getMessage(),ioex);
}
} //结束upLoad2Server
但我不能能够上传由于以下错误!可能有人帮助我.Thanks!
错误:
03-20 18:09:07.033:E / AndroidRuntime(1529):致命异常:主要
03-20 18:09:07.033:E / AndroidRuntime(1529):显示java.lang.NullPointerException
03-20 18:09:07.033:E / AndroidRuntime(1529):在com.example.galleryframe.videoupload.uploadVideo(videoupload.java:233)
03-20 18:09:07.033:E / AndroidRuntime(1529):在com.example.galleryframe.videoupload.access $ 0(videoupload.java:150)
03-20 18:09:07.033:E / AndroidRuntime(1529):在com.example.galleryframe.videoupload $ 3.onClick(videoupload.java:114)
03-20 18:09:07.033:E / AndroidRuntime(1529):在android.view.View.performClick(View.java:2485)
03-20 18:09:07.033:E / AndroidRuntime(1529):在android.view.View $ PerformClick.run(View.java:9080)
03-20 18:09:07.033:E / AndroidRuntime(1529):在android.os.Handler.handleCallback(Handler.java:587)
03-20 18:09:07.033:E / AndroidRuntime(1529):在android.os.Handler.dispatchMessage(Handler.java:92)
03-20 18:09:07.033:E / AndroidRuntime(1529):在android.os.Looper.loop(Looper.java:123)
03-20 18:09:07.033:E / AndroidRuntime(1529):在android.app.ActivityThread.main(ActivityThread.java:3683)
03-20 18:09:07.033:E / AndroidRuntime(1529):在java.lang.reflect.Method.invokeNative(本机方法)
03-20 18:09:07.033:E / AndroidRuntime(1529):在java.lang.reflect.Method.invoke(Method.java:507)
03-20 18:09:07.033:E / AndroidRuntime(1529):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
03-20 18:09:07.033:E / AndroidRuntime(1529):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-20 18:09:07.033:E / AndroidRuntime(1529):在dalvik.system.NativeStart.main(本机方法)
不知道这线是233,我将做一个猜测,说这个问题行
的FileInputStream的FileInputStream =新的FileInputStream(新文件(文件名));
您文件名
被定义为一个URI,但你想创建一个输入流,它作为一个文件
。我怀疑这个输入流是空
。通过这次与调试,以确认步骤。
编辑:如果您想阅读您的文件资产
文件夹,使用 getAssets()
方法:
字符串文件名=clip0003;
InputSteam fileInputSteam =新的InputStreamReader(getAssets()打开(文件名)。);
请注意,这code假设你是一个活动的内部扩展
或其它类上下文
。如果没有,你需要访问上下文
和呼叫 context.getAssets()
。
I am trying to upload video to server ,here i have store the video in assets folder,and trying to upload.i have used the following code:
private void uploadVideo() {
String upLoadServerUri = "http://xxxxx/video_upload.php";
// String [] string = sourceFileUri;
String fileName = "file:///android_asset/clip0003";
HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
int serverResponseCode=1;
byte[] buffer;
int maxBufferSize = 1 * 1024 * 1024;
String responseFromServer = "";
File sourceFile = new File(fileName);
if (!sourceFile.isFile()) {
Log.e("Huzza", "Source File Does not exist");
}
try { // open a URL connection to the Servlet
FileInputStream fileInputStream = new FileInputStream(new File(fileName));
URL url = new URL(upLoadServerUri);
conn = (HttpURLConnection) url.openConnection(); // Open a HTTP connection to the URL
conn.setDoInput(true); // Allow Inputs
conn.setDoOutput(true); // Allow Outputs
conn.setUseCaches(false); // Don't use a Cached Copy
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("video", fileName);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""+ fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
bytesAvailable = fileInputStream.available(); // create a buffer of maximum size
Log.i("Huzza", "Initial .available : " + bytesAvailable);
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// read file and write it into form...
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);
}
// send multipart form data necesssary after file data...
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
serverResponseCode = conn.getResponseCode();
String serverResponseMessage = conn.getResponseMessage();
Log.i("Upload file to server", "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode);
// close streams
Log.i("Upload file to server", fileName + " File is written");
fileInputStream.close();
dos.flush();
dos.close();
} catch (MalformedURLException ex) {
ex.printStackTrace();
Log.e("Upload file to server", "error: " + ex.getMessage(), ex);
} catch (Exception e) {
e.printStackTrace();
}
//this block will give the response of upload link
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
Log.i("Huzza", "RES Message: " + line);
}
rd.close();
} catch (IOException ioex) {
Log.e("Huzza", "error: " + ioex.getMessage(), ioex);
}
} // end upLoad2Server
But i cannot able to upload due to following error!! could anybody help me .Thanks !!!
Error:
03-20 18:09:07.033: E/AndroidRuntime(1529): FATAL EXCEPTION: main
03-20 18:09:07.033: E/AndroidRuntime(1529): java.lang.NullPointerException
03-20 18:09:07.033: E/AndroidRuntime(1529): at com.example.galleryframe.videoupload.uploadVideo(videoupload.java:233)
03-20 18:09:07.033: E/AndroidRuntime(1529): at com.example.galleryframe.videoupload.access$0(videoupload.java:150)
03-20 18:09:07.033: E/AndroidRuntime(1529): at com.example.galleryframe.videoupload$3.onClick(videoupload.java:114)
03-20 18:09:07.033: E/AndroidRuntime(1529): at android.view.View.performClick(View.java:2485)
03-20 18:09:07.033: E/AndroidRuntime(1529): at android.view.View$PerformClick.run(View.java:9080)
03-20 18:09:07.033: E/AndroidRuntime(1529): at android.os.Handler.handleCallback(Handler.java:587)
03-20 18:09:07.033: E/AndroidRuntime(1529): at android.os.Handler.dispatchMessage(Handler.java:92)
03-20 18:09:07.033: E/AndroidRuntime(1529): at android.os.Looper.loop(Looper.java:123)
03-20 18:09:07.033: E/AndroidRuntime(1529): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-20 18:09:07.033: E/AndroidRuntime(1529): at java.lang.reflect.Method.invokeNative(Native Method)
03-20 18:09:07.033: E/AndroidRuntime(1529): at java.lang.reflect.Method.invoke(Method.java:507)
03-20 18:09:07.033: E/AndroidRuntime(1529): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-20 18:09:07.033: E/AndroidRuntime(1529): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-20 18:09:07.033: E/AndroidRuntime(1529): at dalvik.system.NativeStart.main(Native Method)
Without knowing which line is 233, I'll make a guess and say that the problem line is
FileInputStream fileInputStream = new FileInputStream(new File(fileName));
Your filename
is defined as a URI, but you're trying to create an input stream to it as a File
. I suspect this input stream is null
. Step through this with a debugger to confirm.
EDIT: If you want to read a file from you assets
folder, use getAssets()
method:
String fileName = "clip0003";
InputSteam fileInputSteam = new InputStreamReader(getAssets().open(filename));
Note that this code assumes that you're inside an Activity
or other class that extends Context
. If not, you'll need access to a Context
and call context.getAssets()
.
这篇关于在Android的视频上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!