In my application I am attempting to upload a photo to my server via a LAN connection. I started with this tutorial: Sending Image to Server, but found out that it used deprecated methods - HttpParams & HttpClient to be specific. Upon searching, I found HttpUrlConnection, and am trying to fix the app to upload a photo.
Here is my Android Code:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private static final int RESULT_LOAD_IMAGE = 1;
private static final String SERVER_ADDRESS = "http://my__LAN_IP_address/SavePicture.php";
HttpURLConnection urlConnection = null;
ImageView imageToUpload, downloadedImage;
Button bUploadImage, bDownloadImage;
EditText uploadImageName, downLoadImageName;
InputStream is;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageToUpload = (ImageView) findViewById(R.id.imageToUpload);
downloadedImage = (ImageView) findViewById(R.id.downloadedImage);
bUploadImage = (Button) findViewById(R.id.bUploadImage);
bDownloadImage = (Button) findViewById(R.id.bDownloadImage);
uploadImageName = (EditText) findViewById(R.id.etUploadImage);
downLoadImageName = (EditText) findViewById(R.id.etDownloadName);
imageToUpload.setOnClickListener(this);
bUploadImage.setOnClickListener(this);
bDownloadImage.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.imageToUpload:
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
break;
case R.id.bUploadImage:
Bitmap image = ((BitmapDrawable) imageToUpload.getDrawable()).getBitmap();
new UploadImage(image, uploadImageName.getText().toString()).execute();
break;
case R.id.bDownloadImage:
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null){
Uri selectedImage = data.getData();
imageToUpload.setImageURI(selectedImage);
}
}
private class UploadImage extends AsyncTask<Void, Void, Void>{
Bitmap image;
String name;
public UploadImage(Bitmap image, String name){
this.image = image;
this.name = name;
}
@Override
protected Void doInBackground(Void... params) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
String encodedImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT);
ContentValues dataToSend = new ContentValues();
dataToSend.put("image", encodedImage);
dataToSend.put("name", name);
try{
URL url = new URL(SERVER_ADDRESS);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setReadTimeout(3000);
connection.setConnectTimeout(3000);
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(String.valueOf(dataToSend));
writer.flush();
writer.close();
os.close();
connection.connect();
is = connection.getInputStream();
}catch (Exception e){
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
Toast.makeText(getApplicationContext(), "Image Uploaded", Toast.LENGTH_SHORT).show();
}
}
}
And here is my php code:
<?php
$name = isset($_POST["name"]) ? $_POST["name"] : '';
$image = isset($_POST["image"]) ? $_POST["image"] : '';
$decodedImage = base64_decode("$image");
file_put_contents("pictures/" + $name + ".JPG", $decodedImage);
?>
Upon running the app and selecting a photo, nothing errors out and it seems to work, but looking server side, there are no photos getting uploaded. Any help would be greatly appreciated.