Reputation: 2187
I'm trying to take a photo on android and sending it to server using POST method. I have searched alot on stackoverflow and the code I have right now is based on other answers but I still can't send to the server.The parameter name of the image should be "file" in my POST request. I debugged and response is empty and contentlegth is -1. here is my code:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_upload_data, container, false);
TextView barcodeText = (TextView) v.findViewById(R.id.label);
Button picButton = (Button) v.findViewById(R.id.take_picture);
picButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String file = "img.jpg";
File newfile = new File(Environment.getExternalStorageDirectory(), file);
file= newfile.getAbsolutePath();
Uri outputFileUri = Uri.fromFile(newfile);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntent.putExtra("return-data", true);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
}
});
barcodeText.setText(barcode);
return v;
}
and here is my onActivityResult:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TAKE_PHOTO_CODE) {
Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/img.jpg");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream); //compress to which format you want.
byte [] byte_arr = stream.toByteArray();
String image_str = Base64.encodeBytes(byte_arr);
nameValuePairs.add(new BasicNameValuePair("file",image_str));
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Constants.fakeURL);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
final String the_string_response = convertResponseToString(response);
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity().getApplicationContext(), "Response "+the_string_response , Toast.LENGTH_LONG).show();
}
});
}catch(final Exception e){
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getActivity().getApplicationContext(), "ERROR " + e.getMessage(), Toast.LENGTH_LONG).show();
}
});
System.out.println("Error in http connection "+e.toString());
}
}
});
t.start();
}
I also have the BASE64.java in my classes. Can anyone tell me what is wrong please?
Upvotes: 0
Views: 1902
Reputation: 2609
Try to set the bitmap to an imageview to make sure the image bitmap is there, After that use "BitmapFactory.Options" from here http://developer.android.com/training/displaying-bitmaps/load-bitmap.html to avoid out of memory error.
EDIT : Try this compression also.
String image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT);
Upvotes: 1