使用Volley上传图像

时间:2016-11-29 15:56:44

标签: php android

我正在尝试使用volley将我的应用程序中的图像上传到我的服务器,但它无法上传图像并且正在进入Volley Error响应代码。它没有给我一个错误信息或类似的东西。

这是我的Android代码。

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
            Uri filePath = data.getData();
            try {
                //Getting the Bitmap from Gallery
                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
                //Setting the Bitmap to ImageView
                btnCamera.setImageBitmap(bitmap);

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

    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }

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

    public String getStringImage(Bitmap bmp){
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] imageBytes = baos.toByteArray();
        String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
        return encodedImage;
    }

    private void uploadImage(){

        JSONObject js = new JSONObject();
        String name = txtDesc.getText().toString();
        try{
            js.put("image", getStringImage(bitmap));
            js.put("name", name );

            Log.e("js", js.toString());
            JsonObjectRequest jsonObjReq = new JsonObjectRequest(
                    Request.Method.POST,UPLOAD_URL, js,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            Log.d(TAG, response.toString());
                            Toast.makeText(ctx, "Success", Toast.LENGTH_LONG).show();

                            try{
                                String  strSuccess = response.getString("code");
                                Log.d(TAG, strSuccess);
                            } catch (JSONException e)
                            {
                                Log.d(TAG, e.toString());
                            }



                        }
                    }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());

                    Toast.makeText(ctx, "Failed", Toast.LENGTH_LONG).show();

                }
            });

            VolleyRequestQueue.getInstance(ctx).addToRequestQueue(jsonObjReq);
        } catch (JSONException e)
        {
            Log.e(TAG, e.toString());
        }
    }

PHP代码。

<?php
header('Content-type: application/json');

require_once 'db_config.php';
$json = file_get_contents('php://input');
$data = json_decode($json);

$image= $data->image;
$name = $data->name;


 $actualpath = "http://46.101.2.231/FootballGroundGuide/stadium_images/$name" ;


 file_put_contents($actualpath,base64_decode($image));
 echo "Successfully Uploaded";




?>

1 个答案:

答案 0 :(得分:0)

修正了此问题。当我只需要使用“foldername / filename”时使用完整的URL。