본문 바로가기

안드로이드

안드로이드 이미지 업로드 PHP

반응형

android

Uri filePath = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), resultUri);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
selectedPicture = Base64.encodeToString(imageBytes, Base64.DEFAULT);


} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}


implementation 'com.android.volley:volley:1.1.1'


private void updatePhoto() {
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());

// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
String flag = "";
String message = "";

try {
JSONObject root = new JSONObject(response);
JSONObject fff = new JSONObject(root.getString("flag"));

flag = fff.getString("flag");
message = fff.getString("message");

} catch (JSONException e) {
Log.d("myLog", "Parse Error");
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Log.d("myLog", "VolleyError Error " + error.toString());
}
}) {
@Override
protected Map<String, String> getParams() {
Log.d("myLog", "VolleyError filename 11 " + filename);

Map<String, String> params = new HashMap<>();
params.put("contents", contents.getText().toString());
params.put("mem_id", pref.getValue(RbPreference.MEM_ID, ""));
params.put("board_idx", board_idx);
params.put("mode", "write");
params.put("reg_id", reg_id);
params.put("pid", pid);
params.put("picture", selectedPicture);
params.put("filename", filename);

return params;
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Content-Type", "application/x-www-form-urlencoded");
return params;
}
};

// To prevent timeout error
stringRequest.setRetryPolicy(new DefaultRetryPolicy(50000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

// Add the request to the RequestQueue.
stringRequest.setShouldCache(false);
queue.add(stringRequest);


}

 

 

php

if($filename){
$picture = $_POST["picture"];
$file_path = SAVE_FILE_DIR.'/board/'. $filename;
 
file_put_contents($file_path, base64_decode($picture));
 
}else{
$filename='';
}
반응형