2
내 애플리케이션에서 내 서버로 이미지를 업로드하려고하는데 발리볼을 사용하지만 이미지를 업로드하지 못하고 Volley Error 응답 코드로 이동합니다. 그것은 나에게 오류 메시지 나 그와 비슷한 것을주지 않습니다.Volley를 사용하여 이미지 업로드
내 안드로이드 코드입니다.
@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";
?>
''file_put_contents'의 [반환 값] (http://php.net/manual/en/function.file-put-contents.php#refsect1-function.file-put-contents-returnvalues)을 확인해보십시오. *이 함수는 파일에 기록 된 바이트 수를 반환하고, 실패하면 FALSE를 반환합니다. * – zwcloud
SUCCESS가 PHP의 문제라고 믿기 시작했습니다. – xiimoss
file_put_contents 메서드가 응용 프로그램에 false를 반환합니다. @zwcloud – xiimoss