2016-11-29 152 views
2

我試圖從我的應用程序上傳圖像到我的服務器使用凌空然而它無法上傳圖像,並正在進入排出錯誤響應代碼。這不是給我一個錯誤信息或類似的東西。使用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

回答

0

修復了這個問題。當我只需要使用「文件夾名稱/文件名」時正在使用完整的URL。