1

Currently, I'm developing an android app.My app allows users to send an Image and some additional details to server(a servlet). Here is codes to take picture from gallery

public void loadImagefromGallery(View view) {
    Intent galleryIntent = new Intent();
    galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
    galleryIntent.setType("image/*");
    startActivityForResult(Intent.createChooser(galleryIntent, "Select Picture"), CAMERA_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
        Uri uri = data.getData();

        try {
            ImageView imageView = (ImageView) findViewById(R.id.imgView);
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
            ByteArrayOutputStream b = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);

            /*arr is a static byte array to store image*/
            arr = b.toByteArray(); 


            imageView.setImageBitmap(bitmap);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

after that,i send a byte array of the image and some additional details (name,phone...blah blah,off course taken from the EditText) using DataOuputStream(and DataInputStream on server) here is the codes

HttpURLConnection connection = m.getConnection();
OutputStream ops = connection.getOutputStream();
DataOutputStream dops = new DataOutputStream(ops);
dops.writeInt(arr.length);

/*arr is the byte array off the image*/
dops.write(arr); 
dops.writeUTF(Name);
dops.writeUTF(Adress);
dops.writeUTF(Phone);
dops.writeUTF(Email);
dops.writeUTF(Comment);
ops.close();

and the server side :

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
    try {
        DataInputStream dIn = new DataInputStream(request.getInputStream());

        int length = dIn.readInt();                   
        if(length>0) {
            byte[] input = new byte[length];
            dIn.readFully(input, 0, input.length); 
            String Name = dIn.readUTF();
            System.out.println("Name " + Name);
            String Address = dIn.readUTF();
            System.out.println("Address " + Address);
            String Phone = dIn.readUTF();
            System.out.println("Phone " + Phone);
            String Email = dIn.readUTF();
            System.out.println("Email " + Email);
            String Comment = dIn.readUTF();
            System.out.println("Comment " +  Comment);
            dIn.close();   ......

So,the problem is : it's take 8 to 10 secconds for the data to be sent successfully to the server when running on a real mobile device,but just less than 1s when running on virtual device(the virtual device is running on my laptop using wifi,meanwhile the servlet is deployed on PC,which connects directly to the internet).

I supposed there are 4 differrent reasons :

1) The low quality wifi(when sending a same picture,sometime it's take 15s,but sometimes just...3s)

2) The size of data(low resolution picture is sent faster)

3) Low quality mobile device(i borrow from my friend and it look quite...old,with a crack on the screen)

4) I'm using the wrong technology,my code is bad....(I have done some research and it's seem that DataOutputStream is a good choice for this situation)

So...what is the main reason for this problem,and can I do anything to improve my app performance(am I use the right technology and are there any better technology)

**** Thanks all and sorry for my bad english

Thilina Sampath
  • 3,615
  • 6
  • 39
  • 65
lanpm
  • 11
  • 3
  • ok,may be I have fought the answer .My picture's size was really big(3000x1600)...so when took the Bitmap,i resized it to 1/4 the original size so it reduced from about 450 to 60 kb.This help my app to finish process in 1 -5s.Sorry for my silly question. – lanpm Oct 31 '15 at 14:50

0 Answers0