0

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)

1 Answers1

0

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().

Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • I have used 'AssetManager assetManager = getAssets();' and added also.. 'input = assetManager.open(fileName);' but same error is showing ! –  Mar 20 '13 at 13:25
  • Do you have a file named `clip003` in the `assets` folder of your project? Keep in mind that this **must** be the file in the `assets` folder when you build the project, you cannot add it at runtime. – Aleks G Mar 20 '13 at 16:01
  • @user2134412 If the file is `clip0003.3gp` then that's what you need to assign to your `fileName` variable. In your code you do not have the extension. Also, what's the point of uploading a predefined file to the server? This file will _always_ be the same on all devices - this is the file you include with your application and cannot be changed. – Aleks G Mar 21 '13 at 08:37
  • do you know how could i get the response message!!from server –  Mar 21 '13 at 09:32
  • @user2134412 This is a separate question, not related to the one asked. Post your code and ask the relevant question separately. – Aleks G Mar 21 '13 at 09:36