Android Java - How do i get real file (image and video) path and upload with AsyncHttp to the server

470 views Asked by At

i have a project which i need to allow users upload images and video to the server... currently users can select image and video.. but i don't know how to get the real file path then upload by using AsyncHttp Post...

this is my code to select image below :

 pphoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);

        }


    });

and this is my onactivityresult code

protected void onActivityResult(int requestCode, int resultCode,
                                Intent imageReturnedIntent){

    super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

    switch(requestCode) {
        case PICK_IMAGE_REQUEST: // Do your stuff here...
            if (resultCode == RESULT_OK) {

                final String path = imageReturnedIntent.getData().getPath();


                final File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath());




                Uri selectedData = imageReturnedIntent.getData();


                final Button up = (Button) findViewById(R.id.up);

                final ImageView etPhoto = (ImageView) findViewById(R.id.imgg);



                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                Cursor cursor = getContentResolver().query(
                        selectedData, filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                final String filePath = cursor.getString(columnIndex);
                cursor.close();


                final Bitmap dImage = BitmapFactory.decodeFile(filePath);


                etPhoto.setImageURI(selectedData);

then if i want to post to server... this is the code i am using but not working...

AsyncHttpClient client = new AsyncHttpClient();
                        RequestParams params = new RequestParams();
                        params.put("token", "token");
                        try {
                            params.put("photo", file);
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        }
                        client.post("http://10.205.165.237/Android/uploadimages.php", params, new AsyncHttpResponseHandler() {

                            @Override
                            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                                System.out.println("statusCode " + statusCode);//statusCode 200
                            }

                            @Override
                            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

                            }
                        });

Now my problem is i do not know how to get the real file path to be used for upload and i am not sure if the AsyncHttp post is correct cos i have tried the php code i am sending the file to with html form and it worked properly.... please does anyone know how to go about this...? your contribution will be highly appreciated.

0

There are 0 answers