Javascript 从Android向PHP上传图像的有效方法

Javascript 从Android向PHP上传图像的有效方法,javascript,java,php,android,Javascript,Java,Php,Android,我正在将一个图像从Android上传到PHP,方法是将其转换为字节数组。之后,我只需使用POST方法提交它 在服务器端,我的做法与在客户端(Android应用程序)的做法相反 我想知道是否还有其他好的/有效的/聪明的方法来做到这一点 注意:我只需要在客户端使用PHP/JS/HTML和Java 最有效的方法之一是使用截击,因此确保将其包括在你的梯度中: compile 'com.android.volley:volley:1.0.0' 我个人使用的是自动收录的截击。既然您还没有输入任何您尝试过的

我正在将一个图像从Android上传到PHP,方法是将其转换为字节数组。之后,我只需使用POST方法提交它

在服务器端,我的做法与在客户端(Android应用程序)的做法相反

我想知道是否还有其他好的/有效的/聪明的方法来做到这一点

注意:我只需要在客户端使用PHP/JS/HTML和Java


最有效的方法之一是使用截击,因此确保将其包括在你的梯度中:

compile 'com.android.volley:volley:1.0.0'
我个人使用的是自动收录的截击。既然您还没有输入任何您尝试过的代码,我将给您一些示例。在您尝试上载图像的活动中,创建一个按钮。单击该按钮时添加此代码:

Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
i.setType("image/*");
startActivityForResult(i, Constants.VALUE_BROWSE_IMAGE_REQUEST);
这将打开手机中的多媒体资料以浏览图像。在活动顶部声明一个变量:

private Bitmap mBitmap;
选择要从多媒体资料上载的图像后,请编写以下代码:

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

    if (requestCode == Constants.VALUE_BROWSE_IMAGE_REQUEST &&
            resultCode == RESULT_OK &&
            data != null) {

        try {
            // Get the photo URI data
            Uri filePath = data.getData();

            // Get the Bitmap from Gallery
            mBitmap = decodeBitmap(filePath, this);
        } catch (IOException e) {
            Toast.makeText(this, "Could not open picture.", Toast.LENGTH_SHORT).show();
        }
    }
}
现在您已经有了所选图像的位图,您需要将该位图转换为base64字符串,以便Volley能够上载它:

// Before uploading the selected image to the server, we need to convert the Bitmap to String.
// This method will convert Bitmap to base64 String.
private String getStringImage(Bitmap bmp) {
    ByteArrayOutputStream b = new ByteArrayOutputStream();

    // This part handles the image compression. Keep the image quality
    // at 70-90 so you don't cause lag when loading it on android
    // (0-low quality but fast load, 100-best (original) quality but slow load)
    bmp.compress(Bitmap.CompressFormat.JPEG, 80, b);
    byte[] imageBytes = b.toByteArray();
    String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    return encodedImage;
}
最后,您可以开始上载图像:

private void uploadImage() {
    StringRequest stringRequest = new StringRequest(
            Request.Method.POST,
            "URL_TO_YOUR_WEB_API",
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(getApplicationContext(), "Failed to upload image.", Toast.LENGTH_SHORT).show();
                }
            }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {

            // Converting Bitmap to String
            String image = getStringImage(mBitmap);

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

            // Add parameters
            params.put("action", "YOUR_BACKEND_KEY1");
            params.put("...", image);

            // Returning parameters
            return params;
        }
    };

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

    // Adding request to the queue
    requestQueue.add(stringRequest);
}
那么android中的参数将是:

params.put("action", "uploadimage");
params.put("imagebase", image);
params.put("caption", "somecaptiontext");
params.put("action", "uploadimage");
params.put("imagebase", image);
params.put("caption", "somecaptiontext");