2017-08-03 119 views
0

您好工作在聊天應用程序,現在我想集成共享音頻圖像PDF視頻從我的應用程序。我正在使用此代碼,但無法正常工作。有沒有人有任何想法如何發送這些文件使用凌空或改造。聽到是我試過的。如何上傳文件,如視頻音頻PDF格式文本文件圖像從Android到PHP服務器

Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
    intent.setType("*/*"); 
    intent.addCategory(Intent.CATEGORY_OPENABLE); 

    try { 
     startActivityForResult(
       Intent.createChooser(intent, "Select a File to Upload"), 
       FILE_SELECT_CODE); 
    } catch (android.content.ActivityNotFoundException ex) { 
     // Potentially direct the user to the Market with a Dialog 
     Toast.makeText(this, "Please install a File Manager.", 
       Toast.LENGTH_SHORT).show(); 
    } 

而這是我onActivityResult方法。

 @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent 
    data) { 
    switch (requestCode) { 
     case FILE_SELECT_CODE: 
      if (resultCode == RESULT_OK) { 
     Uri uri = data.getData(); 
       ContentResolver cr = this.getContentResolver(); 
       mime = cr.getType(uri); 
       Toast.makeText(ChatActivity.this,mime,Toast.LENGTH_LONG).show(); 
       Log.d(TAG, "File Uri: " + uri.toString()+" "+mime); 
       // Get the path 

       if(mime.equalsIgnoreCase("image/jpeg")){ 
        try { 
         bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri); 
         uploadImage(); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       }else{ 

        ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
        FileInputStream fis; 
        try { 
         fis = new FileInputStream(new File(uri.getPath())); 
         byte[] buf = new byte[1024]; 
         int n; 
         while (-1 != (n = fis.read(buf))) 
          baos.write(buf, 0, n); 
        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
        filebytes = baos.toByteArray(); 
        uploadImage(); 

       } 

      } 
      break; 
    } 
    super.onActivityResult(requestCode, resultCode, data); 
} 

這是我的上傳方法。

 private void uploadImage(){ 

    StringRequest stringRequest = new StringRequest(Request.Method.POST, 
    UPLOAD_URL, 
      new Response.Listener<String>() { 
       @Override 
       public void onResponse(String s) { 
        //Disimissing the progress dialog 
        loading.dismiss(); 
        //Showing toast message of the response 
        try { 
         JSONObject jsonObject = new JSONObject(s); 
         String message = jsonObject.getString("result"); 
         sendfileMessage(message); 

        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 


        Log.d("image path",s); 

       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError volleyError) { 
        //Dismissing the progress dialog 
        loading.dismiss(); 


       } 
      }){ 
     @Override 
     protected Map<String, String> getParams() throws AuthFailureError { 
      //Converting Bitmap to String 




      Map<String,String> params = new Hashtable<String, String>(); 

      //Adding parameters 
      params.put("mkey", "R09fQ2hhdC0z"); 


      if(mime.equalsIgnoreCase("image/jpeg")){ 
       String image = getStringImage(bitmap); 
       params.put("ch_img", image); 
      }else{ 
       String image = new String(filebytes); 
       params.put("ch_img", image); 
      } 
      //returning parameters 
      Log.d("imagedemo",params.toString()); 
      return params; 
     } 
    }; 

    //Creating a Request Queue 
    RequestQueue requestQueue = Volley.newRequestQueue(this); 

    //Adding request to the queue 
    requestQueue.add(stringRequest); 
} 
+0

[上傳的可能的複製doc,pdf,xls等,從android應用程序到php服務器](https://stackoverflow.com/questions/33820723/upload-doc-pdf-xls-etc -from-android-application-to-php-server) – SripadRaj

+0

@SripadRas是否適用於視頻和音頻 –

+0

我想它應該可以工作。 – SripadRaj

回答

0

您可以使用Mutipart數據將文件上傳到API。

如果您使用的改造就像創建界面 -

@Multipart 
@POST("updateProfile") 
Call<JsonObject> uploadFile(@Part MultipartBody.Part file); 

然後創建Mutipart數據:

private MultipartBody.Part getFilePart(String filePath) { 

    File fileToUpload=new File(filePath); 

    return MultipartBody.Part.createFormData("file", fileToUpload.getName(),fileToUpload); 
    } 

最後只需要調用API

retrofitObject.uploadFile(getFilePart("filePath").enqueue(this); 
+0

所以我們只需要文件路徑上傳視頻pdf等 –

+0

是的唯一路徑。但是你的API必須使用多部分數據。尋找洞天數後找到 –

+0

得到更好的方法來做到這一點。你可以請一個簡短的代碼。來自onactivity結果。對不起,但我堅持這一點。 –

相關問題