2016-11-08 60 views
4

我試圖創建一個應用程序,我必須上傳任何不超過16-20 mb的文件。我使用的是OkHttpClient3,但我的主要問題,它會採取文件與簡單的名稱,如a.mp4,b.jpg等等,但它不能選擇文件,如hello world 12.mp4什麼代碼應放在 RequestBody file_body = RequestBody.create(MediaType.parse(content_type),f). 我的代碼是。無法通過使用OkHttp3選擇文件管理器中的文件

public void enable_button(){ 
     choosefile.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       new MaterialFilePicker() 
         .withActivity(ShowStudent.this) 
         .withRequestCode(10) 
         .start(); 
      } 
     }); 
    } 
    @Override 
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
     if(requestCode == 100 && (grantResults[0] == PackageManager.PERMISSION_GRANTED)){ 
      enable_button(); 
     }else { 
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
       requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},100); 
      } 
     } 
    } 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, final Intent data) { 
     if(requestCode == 10 && resultCode == RESULT_OK){ 
      progress = new ProgressDialog(ShowStudent.this); 
      progress.setTitle("Uploading"); 
      progress.setMessage("Please wait..."); 
      progress.show(); 

      Thread t = new Thread(new Runnable() { 
       @Override 
       public void run() { 
        File f = new File(data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH)); 
        String content_type = getMimeType(f.getPath()); 
        String file_path = f.getAbsolutePath(); 
        OkHttpClient client = new OkHttpClient(); 
        RequestBody file_body = RequestBody.create(MediaType.parse(content_type),f); 
        RequestBody request_body = new MultipartBody.Builder() 
          .setType(MultipartBody.FORM) 
          .addFormDataPart("type",content_type) 
          .addFormDataPart("uploaded_file",file_path.substring(file_path.lastIndexOf("/")+1), file_body) 
          .build(); 
        Request request = new Request.Builder() 
          .url("http://onsitesupport.info/diary/uploadproject.php") 
          .post(request_body) 
          .build(); 
        try { 
         Response response = client.newCall(request).execute(); 
         if(!response.isSuccessful()){ 
          throw new IOException("Error : "+response); 
         } 
         progress.dismiss(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } 
      }); 
      t.start(); 
     } 
    } 
    private String getMimeType(String path) { 
     String extension = MimeTypeMap.getFileExtensionFromUrl(path); 
     return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); 
    } 

和我得到的部份例外

E/AndroidRuntime: FATAL EXCEPTION: Thread-3122 
       Process: com.kliff.studentteacherportal, PID: 29436 
       java.lang.NullPointerException: Attempt to invoke interface method 'int java.lang.CharSequence.length()' on a null object reference 
        at java.util.regex.Matcher.reset(Matcher.java:177) 
        at java.util.regex.Matcher.<init>(Matcher.java:90) 
        at java.util.regex.Pattern.matcher(Pattern.java:297) 
        at okhttp3.MediaType.parse(MediaType.java:51) 
        at com.kliff.studentteacherportal.ShowStudent$2.run(ShowStudent.java:100) 
        at java.lang.Thread.run(Thread.java:818) 
E/WindowManager: android.view.WindowLeaked: Activity com.kliff.studentteacherportal.ShowStudent has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{26acab10 V.E..... R......D 0,0-684,322} that was originally added here 
       at android.view.ViewRootImpl.<init>(ViewRootImpl.java:458) 
       at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:278) 
       at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85) 
       at android.app.Dialog.show(Dialog.java:311) 
       at com.kliff.studentteacherportal.ShowStudent.onActivityResult(ShowStudent.java:91) 
       at android.app.Activity.dispatchActivityResult(Activity.java:6294) 
       at android.app.ActivityThread.deliverResults(ActivityThread.java:3873) 
       at android.app.ActivityThread.handleSendResult(ActivityThread.java:3920) 
       at android.app.ActivityThread.access$1300(ActivityThread.java:182) 
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1523) 
       at android.os.Handler.dispatchMessage(Handler.java:111) 
       at android.os.Looper.loop(Looper.java:194) 
       at android.app.ActivityThread.main(ActivityThread.java:5706) 
       at java.lang.reflect.Method.invoke(Native Method) 
       at java.lang.reflect.Method.invoke(Method.java:372) 
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1033) 
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828) 
W/MALI: glDrawArrays:714: [MALI] glDrawArrays takes more than 5ms here. Total elapse time(us): 5659 
+0

'java.lang中解決我的問題。 NullPointerException:嘗試在空對象引用上調用接口方法'int java.lang.CharSequence.length()'。所以你的變量是'null'。使用前檢查。並找出它爲什麼是'空'。它根據你的logcat在'(ShowStudent.java:91)'上。 – greenapps

+0

@greenapps一些文件上傳到服務器,但沒有其他的......另一個問題是,如果文件大小很大,即使上傳文件,進度條也不會消失。這個問題只發生在我對Android新手的大尺寸文件中。請幫助我 –

+0

您應該首先解決NullPointer問題。我想知道爲什麼你甚至沒有把它命名。不要求救。我已經給你提供了幫助,但你忽略了它。爲什麼? – greenapps

回答

1

好吧,我弄清楚,它不能選擇與空間的文件,但我通過更換

String mimeType= URLConnection.guessContentTypeFromName(f.getName()); 
RequestBody file_body = RequestBody.create(MediaType.parse(mimeType),f);` 
相關問題