0

Hello I Want to capture image by click a button and after the photo was taken it will show on the screen and then click another button which will send that image to server. i tried it in many ways but i didn't found any appropriate solution so please help me.

I am developing this in android 2.3.3

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
             Bitmap photo = (Bitmap) data.getExtras().get("data"); 
             imageView.setImageBitmap(photo);
             Intent intent= getIntent();
            System.out.println("image name=========================:: "+imageName);
            requestIntent.putExtra("imageName", imageName);
            startActivity(requestIntent);

        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the image capture
        } else {
            // Image capture failed, advise user
        }
    }
}

the image is not showing in that ImageView but stored in SdCard. And while am sending that image by calling another intent it shows me a error i.e 11-01 12:00:15.085: E/log_tag(9205): Error in http connection android.os.NetworkOnMainThreadException and that intent code is.

public class PreviewAndSendImage extends Activity{

@SuppressLint("SdCardPath")
public void onCreate(Bundle savedInstanceState) {       
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button send=(Button) findViewById(R.id.sendToServer);
    send.setEnabled(true);
    send.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Intent intent= getIntent();
            String name=intent.getStringExtra("imageName");
            System.out.println("image name :::::::::::::::: +" +name);
            Bitmap bitmapOrg = BitmapFactory.decodeFile("/sdcard/pictures/"+name);
            ByteArrayOutputStream bao = new ByteArrayOutputStream();
            bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
            byte [] ba = bao.toByteArray();

            String ba1=Base64.encodeBytes(ba);
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("reqId", "40"));
            nameValuePairs.add(new BasicNameValuePair("image",ba1));    

            try{

                     HttpClient httpclient = new DefaultHttpClient();
                     HttpPost httppost = new HttpPost("http://192.168.0.115:8085/Dplus_EB/bill");
                     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                     HttpResponse response = httpclient.execute(httppost);
                    //HttpEntity entity = response.getEntity();
                    //is = entity.getContent();
                     BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                      String line = "";
                      while ((line = rd.readLine()) != null) {
                          System.out.println("success----------------------------");
                          Toast.makeText(PreviewAndSendImage.this, "Status: "+line, Toast.LENGTH_LONG).show();
                      }
            }catch(Exception e){

                    Log.e("log_tag", "Error in http connection "+e.toString());

            }

        }
    });

}
  }
Akshay
  • 2,506
  • 4
  • 34
  • 55
sitanshu
  • 65
  • 1
  • 3
  • 9

2 Answers2

6

Please follow the simple steps below and you can easily achieve what you want:

Step 1 : Make a class in your app that capture photo from camera and save it to your sd-card directory. See this Link, see also this discussion discussion.

step 2 :Now make Android FTP client. See this link.

step 3 : Now you can make many types of interaction with FTP server like changeDir, Upload files, Download file. See this Tutorial.

It is simple but only condition is that you have to read some documents. Please accept and vote up my answer if you find it helpful. And also comment me if any link not worked. Thanks.

Community
  • 1
  • 1
1

you cannot make http calls on ui thread. at least not on newer OS.

consider using AsyncTask for such calls.

ckpatel
  • 1,926
  • 4
  • 18
  • 34