【发布时间】:2017-07-22 14:52:16
【问题描述】:
我通过将图像转换为字节数组将图像从 Android 上传到 PHP。之后,我只需使用 POST 方法提交它。
在服务器端,我的操作与在客户端(Android 应用)端的操作相反。
我想知道是否还有其他好的/高效/聪明的方法可以做到这一点。
注意:我只需要在客户端只使用 PHP/JS/HTML 和 Java。
【问题讨论】:
标签: javascript java php android
我通过将图像转换为字节数组将图像从 Android 上传到 PHP。之后,我只需使用 POST 方法提交它。
在服务器端,我的操作与在客户端(Android 应用)端的操作相反。
我想知道是否还有其他好的/高效/聪明的方法可以做到这一点。
注意:我只需要在客户端只使用 PHP/JS/HTML 和 Java。
【问题讨论】:
标签: javascript java php android
最有效的方法之一是使用 Volley,因此请确保将其包含在您的 gradle 中:
compile 'com.android.volley:volley:1.0.0'
我个人使用Universal Image Loader,当您包含 Volley 时,它会自动包含在内。由于您没有输入任何您尝试过的代码,我会给您一些示例。在您尝试上传图片的活动中,创建一个按钮。单击该按钮时添加此代码:
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);
}
确保将参数字符串替换为您在 php 中创建的后端。
示例网址:
http://yoursite.com/api.php?action=uploadimage&imagebase=adwtgiuewohnjsoiu&caption=somecaptiontext
那么android中的参数就是:
params.put("action", "uploadimage");
params.put("imagebase", image);
params.put("caption", "somecaptiontext");
【讨论】: