//part when you click on item and start camera
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Context context = v.getContext();
if (takePictureIntent.resolveActivity(context.getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
});
//part when you get the thumbnail from camera
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
new AsyncTask() {
@Override
protected Void doInBackground(Bitmap... bitmaps) {
OkHttpClient client = new OkHttpClient();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmaps[0].compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
//there are some my custom fields form
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("submit", "")
.addFormDataPart("name", "myPhotoName")
.addFormDataPart("photo", "tmp_photo_" + System.currentTimeMillis(), RequestBody.create(MediaType.parse("image/jpeg"), byteArray))
.build();
Request request = new Request.Builder()
.url("www.example.com/upload_image.php")
.post(requestBody)
.build();
Response response = null;
try {
response = client.newCall(request).execute();
if (response.isSuccessful()) {
Log.d(TAG, "doInBackground: upload success");
} else {
Log.d(TAG, "doInBackground: upload failed");
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}.execute(imageBitmap);
} else {
//another results
super.onActivityResult(requestCode, resultCode, data);
}
}