0

ビデオをサーバーにアップロードしようとしています。ここでは、ビデオをアセットフォルダーに保存し、アップロードしようとしています。次のコードを使用しました:

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

しかし、以下のエラーでアップロードできません!! 誰でも私を助けることができます.Thanks !!!

エラー:

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)
4

1 に答える 1

0

どの行が 233 なのかわからないので、推測して、問題の行は

FileInputStream fileInputStream = new FileInputStream(new File(fileName));

あなたfilenameは URI として定義されていますが、入力ストリームを として作成しようとしていますFile。この入力ストリームはnull. デバッガーでこれを確認してください。

編集:assetsフォルダーからファイルを読み取りたい場合は、次のgetAssets()方法を使用します。

String fileName = "clip0003";
InputSteam fileInputSteam = new InputStreamReader(getAssets().open(filename));

このコードは、Activityを拡張する または他のクラスの内部にいることを前提としていることに注意してくださいContext。そうでない場合は、にアクセスしてContextを呼び出す必要がありますcontext.getAssets()

于 2013-03-20T12:47:11.233 に答える