我正在尝试使用排球从Android上传多个图像。在android中我多次检查在脚本中发送编码字符串之间没有错误。但我注意到,当脚本处理字符串时,只有最后一个成功上传,其余的都失败了。错误转到android volley中的String Request,我收到一个凌空错误。
UploadImages()
static BaseUrl baseUrl;
AddPropertyImagesCommand command = new AddPropertyImagesCommand(this);
private void UploadImages(){
for (String imagePath: imageList){
try {
Bitmap bitmap = ImageLoader.init().from(imagePath).requestSize(256,256).getBitmap();
final String encodedString = ImageBase64.encode(bitmap);
String imageName = imagePath.substring(imagePath.lastIndexOf("/")+1);
intent = getIntent();
final int property_id = Integer.parseInt(intent.getStringExtra("property_id"));
String ADD_IMAGE_REQUEST_URL = baseUrl.getBaseUrl() + "addPropertyImages.php";
StringRequest stringRequest = new StringRequest(Request.Method.POST, ADD_IMAGE_REQUEST_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(getApplicationContext(), response, Toast.LENGTH_SHORT).show();
Log.d(TAG, "imagelist count: " + imageList.size());
progressDialog.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "Error while uploading image", Toast.LENGTH_SHORT).show();
Log.d(TAG, "Volley Error: " + error.toString());
progressDialog.dismiss();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("encoded_string", encodedString);
params.put("image_name", image_name);
params.put("property_id", property_id + "");
return params;
}
};
command.add(stringRequest);
} catch (FileNotFoundException e) {
Toast.makeText(this, "File not found", Toast.LENGTH_SHORT).show();
progressDialog.dismiss();
}
}
//execute the singleton class from volley which is addtorequestqueue
command.execute();
}
然后它转到我的php脚本:
addPropertyImages.php
<?php
include "connection.php";
$response = array();
$response["success"] = false;
if(isset($_POST['property_id'])){
$encoded_string = $_POST['encoded_string'];
$image_name = $_POST['image_name'];
$property_id = $_POST['property_id'];
//method to upload images
upload($encoded_string, $image_name, $property_id);
exit;
}else{
echo "image not in";
exit;
}
function upload($encoded_string, $img_name, $property_id){
//create unique image file name based on micro time and date
$date = date('Y:m:d H:i:s');
$now = DateTime::createFromFormat('U.u', microtime(true));
$id = $now->format('YmdHisu');
$upload_folder = "listing_images";
$image_path = "$upload_folder/$id_$img_name";
$decoded_string = base64_decode($encoded_string);
if(file_put_contents($image_path, $decoded_string) != false){
echo "upload success";
}else{
echo "upload failed";
}
}
?>
logcat的:
01-24 10:29:02.229 23401-23401/homeseek.app.android.capstonehomeseek D/AddPropertyImages: Image name: P_20170122_220217.jpg
01-24 10:29:02.229 23401-23401/homeseek.app.android.capstonehomeseek D/AddPropertyImages: /storage/emulated/0/DCIM/Camera/P_20170122_220217.jpg
01-24 10:29:04.382 23401-23401/homeseek.app.android.capstonehomeseek D/AddPropertyImages: Image name: P_20170124_090947.jpg
01-24 10:29:04.382 23401-23401/homeseek.app.android.capstonehomeseek D/AddPropertyImages: /storage/emulated/0/DCIM/Camera/P_20170124_090947.jpg
01-24 10:29:07.745 23401-23401/homeseek.app.android.capstonehomeseek D/AddPropertyImages: Image name: P_20170121_131658.jpg
01-24 10:29:07.745 23401-23401/homeseek.app.android.capstonehomeseek D/AddPropertyImages: /storage/emulated/0/DCIM/Camera/P_20170121_131658.jpg
01-24 10:29:18.075 23401-23401/homeseek.app.android.capstonehomeseek D/AddPropertyImages: imagelist count: 3
01-24 10:29:26.888 23401-23401/homeseek.app.android.capstonehomeseek D/AddPropertyImages: Volley Error: com.android.volley.TimeoutError
01-24 10:29:27.329 23401-23401/homeseek.app.android.capstonehomeseek D/AddPropertyImages: imagelist count: 3
我正在使用Oum Saokosal通过android volley将图像上传到服务器的方式。 Youtube页面:https://www.youtube.com/user/oumsaokosal/featured