public void onClickSend(View view) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
}
private class ConnectToServer extends AsyncTask<Void, Void, Integer> {
HttpURLConnection conn;
Integer res;
protected Integer doInBackground(Void... params) {
try {
File file = new File(mFilePath);
HttpRequest request = HttpRequest.post("http://potatosing.16mb.com");
request.part("image", "image.jpg", file);
URL url = new URL(request.body());
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(1000000);
conn.setRequestMethod("POST");
conn.setRequestProperty("User-Agent", "Mozilla/5.0");
conn.connect();
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
if (resultCode == RESULT_OK) {
Uri photoUri = imageReturnedIntent.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(photoUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
mFilePath = cursor.getString(columnIndex);
cursor.close();
new ConnectToServer().execute();
}
}
<?php
$uploaddir = 'files/';
$uploadfile = $uploaddir.basename($_FILES['image']['name']);
?>
$uploaddir = 'files/';
$uploadfile = $uploaddir.basename($_FILES['image']['name']);
move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile);
$uploaddir = './files/';
$uploaddir = 'files/';
$file = $uploaddir . basename($_FILES['image']['name']);
$ext = substr($_FILES['image']['name'],strpos($_FILES['image']['name'],'.'),strlen($_FILES['image']['name'])-1);
$filetypes = array('.jpg','.gif','.bmp','.png','.JPG','.BMP','.GIF','.PNG','.jpeg','.JPEG');
$name=$_FILES['image']['name'];
$getMime = explode('.', $name);
$mime = end($getMime);
$randomName = substr_replace(sha1(microtime(true)), '', 12);
$filename=$randomName.'.'.$mime;
move_uploaded_file($_FILES['image']['tmp_name'],$uploaddir .$filename.'.'.$mime);
public void onClickSendImage(View view) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
if (resultCode == RESULT_OK) {
Uri photoUri = imageReturnedIntent.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(photoUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
mFilePath = cursor.getString(columnIndex);
cursor.close();
file = new File(mFilePath);
image_name = getFileName(mFilePath);
new Encode_image().execute();
}
}
private String getFileName(String filePath) {
char[] chars = filePath.toCharArray();
int index = 0;
for (int i = chars.length-1; i > 0; i--) {
if (chars[i] == '\\' || chars[i] == '/') {
index = i;
break;
}
}
String result = "";
for (int i = index+1; i < chars.length; i++) {
result = result + String.valueOf(chars[i]);
}
return result;
}
private class Encode_image extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
bitmap = BitmapFactory.decodeFile(mFilePath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
bitmap.recycle();
byte[] array = stream.toByteArray();
encoded_string = Base64.encodeToString(array, 0);
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
makeRequest();
}
}
private void makeRequest() {
RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest request = new StringRequest(Request.Method.POST, "http://potatosing.16mb.com/index.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String,String> map = new HashMap<>();
map.put("encoded_string",encoded_string);
map.put("image_name",image_name);
return map;
}
};
requestQueue.add(request);
}
<?php
header('Content-type : bitmap; charset=utf-8');
if(isset($_POST["encoded_string"])){
$encoded_string = $_POST["encoded_string"];
$image_name = $_POST["image_name"];
$decoded_string = base64_decode($encoded_string);
$path = 'images/'.$image_name;
$file = fopen($path, 'wb');
$is_written = fwrite($file, $decoded_string);
fclose($file);
}
?>