0

i am writing a small app which uploads a picture when a taken a picture from camera. the following is the complete code. When i install this on my phone and run the app i don't see any thing in the apache tomcat logs(i.e post request is not not able to execute).

I tested the back end with web browser on my phone and it works. But when i am trying with app i don't see any output on phone as well as in the server logs.

Can anyone tell me if there is any problem in the code? If not how to proceed with these type of errors.

Edited code

public class MainActivity extends Activity {

    int REQUEST_CODE = 1;
    ImageView IMG;
    Bitmap bitmap;
    TextView link;
    Bitmap bm[] = new Bitmap [4];

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (i.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(i, REQUEST_CODE);
        }
        setContentView(R.layout.activity_main);
        IMG = (ImageView) findViewById(R.id.img);
        link = (TextView) findViewById(R.id.response);
    }

    public void onActivityResult(int requestcode, int resultcode, Intent data) {
        if (requestcode == REQUEST_CODE) {
            if (resultcode == RESULT_OK) {
                Bundle bundel = new Bundle();
                bundel = data.getExtras();
                bitmap = (Bitmap) bundel.get("data");
                bm[0] = bitmap;
                UploadPic picUpload = new UploadPic();
                picUpload.execute(bm);
                IMG.setImageBitmap(bitmap);

            }

        }

    }

    private class UploadPic extends AsyncTask<Bitmap, Integer, String> {

        @Override
        protected String doInBackground(Bitmap... bitmap) {
            // TODO Auto-generated method stub
            BufferedReader in = null;
            String data = null;

            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://192.168.1.185:8080/ver0_2/query");
                MultipartEntityBuilder builder = MultipartEntityBuilder.create();

                /* example for setting a HttpMultipartMode */
                builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                bitmap[0].compress(CompressFormat.JPEG, 75, byteArrayOutputStream);
                // bitmap.compress
                byte [] byteData = byteArrayOutputStream.toByteArray();
                // String strData = Base64.encodeToString(data, Base64.DEFAULT); // I have no idea why Im doing this
                ByteArrayBody byteArrayBody = new ByteArrayBody(byteData, "image"); // second parameter is the name of the image

                /* example for adding an image part */
                builder.addPart("fileUpload", byteArrayBody);
                HttpEntity entity = builder.build();
                httppost.setEntity(entity);
                HttpResponse response = httpclient.execute(httppost);
                in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                StringBuffer sb = new StringBuffer("");
                String l = "";
                String nl = System.getProperty("line.seperator");
                while ((l = in.readLine()) != null) {
                    sb.append(l + nl);
                }

                in.close();
                data = sb.toString();

            }
            catch (Exception e) {
                e.printStackTrace();
            }
            return data;
        }

        @Override
        protected void onPostExecute(String result) {
            // execution of result of Long time consuming operation
            link.setText(result);
        }
    }
}
hpopiolkiewicz
  • 3,281
  • 4
  • 24
  • 36
rlvamsi
  • 179
  • 1
  • 2
  • 11
  • Why did you removed then asynctask call? By then way, the best approach to do it is by creating a class that extends AsyncTask, have you tried to search on Stackoverflow or in Google before post this question? I believe is covered in many portals and blogs accross the web. – ararog Oct 30 '14 at 14:19
  • You are trying to perform a network request on a UI thread, which is not allowed. It seems that you commented out very important parts of your code, the asyncTask ones.. –  Oct 30 '14 at 14:27
  • @jvrodrigues Very Very Thanks for your reply. I am new to this so searched several blog to write this code. Now I have updated my code as you suggested with AsyncTask but still i am facing the same issue. – rlvamsi Oct 30 '14 at 15:27
  • @ararog sorry couldn't add more than one user in the above comment. Thank you and if possible can you look at the updated code once ? – rlvamsi Oct 30 '14 at 15:28
  • Post the new code, and if possible the stackTrace of the exception it catches. I will take a look at it and help in anyway i can. –  Oct 30 '14 at 15:29
  • @jvrodrigues yeah i have already updated the code. I will have to see how i can get the stackTrace(i am new have to search a little) – rlvamsi Oct 30 '14 at 15:31
  • To add the stack trace all you have to do is run this code in your android device and check the logcat in verbose mode for the error, it will be displayed there. –  Oct 30 '14 at 15:56

0 Answers0