如何上传视频音频文件PDF文档文件图像从android到php服务器

时间:2017-08-03 07:39:43

标签: android pdf video file-upload android-volley

您正在开发聊天应用程序,现在我想从我的应用程序中集成共享音频图像PDF视频。我正在使用此代码,但它无法正常工作。有谁知道如何使用截击或改造发送这些文件。听到我试过的。

    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);
}

1 个答案:

答案 0 :(得分:0)

您可以使用Mutipart Data将文件上传到API。

如果您使用像 -

这样的Retrofit,请创建一个界面
@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);