使用Volley上传图像

问题描述:

我试图从我的应用程序上传图像到我的服务器使用凌空然而它无法上传图像,并正在进入排出错误响应代码。这不是给我一个错误信息或类似的东西。使用Volley上传图像

这是我的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"; 




?> 
+0

尝试检查'file_put_contents'的[返回值](http://php.net/manual/en/function.file-put-contents.php#refsect1-function.file-put-contents-returnvalues)。 *这个函数返回写入文件的字节数,或者失败时返回FALSE。* – zwcloud

+0

开始说SUCCESS导致我相信这是我的PHP的问题。 – xiimoss

+0

file_put_contents方法将false返回给应用程序@zwcloud – xiimoss

修复了这个问题。当我只需要使用“文件夹名称/文件名”时正在使用完整的URL。