2017-08-03 2 views
0

私はチャットアプリケーションで作業していますが、今私のアプリケーションから共有オーディオ画像PDFビデオを統合したいと思います。私はこのコードを使用していますが、正しく動作していません。誰もがどのようにこれらのファイルをボレーや改造を使用して送信するか考えていますか?私が試したのが聞こえます。ビデオオーディオのようにファイルをアップロードするにはどうすればいいですか?PDF docテキストファイルイメージをアンドロイドから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

可能性のある重複したAPIを呼び出しますdoc、pdf、xlsなど、アンドロイドアプリケーションからPHPサーバへ](https://stackoverflow.com/questions/33820723/upload-doc-pdf-xls-etc -from-android-application-to-php-server) – SripadRaj

+0

@SripadRasは、ビデオとオーディオの仕事でもあります。 –

+0

私はそれがうまくいくはずです。 – SripadRaj

答えて

0

Mutipart Dataを使用してファイルを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); 
    } 

ちょうど[アップロードの

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

が推奨されていますので、ビデオファイルなどをアップロードするためのファイルパスのみが必要です。 –

+0

はい専用のパスです。しかし、あなたのapiはMutipartのデータを消費しなければなりません。穴の日数を検索した後に –

+0

がこれを行うためのより良い方法を得る。簡単なコードを入力してください。 onactivityresultから。申し訳ありませんが、私はこれに固執しました。 –

関連する問題