How to upload a picture to android via JSON, my current method takes a lot of time

The question I'm trying to answer is what settings do I need to make for my code to speed up the image upload to the server?

My code currently takes an image and saves it in an array of bytes, and then changes it to a base64 string, which is then placed in a JSON object and sent along with some text. However, it takes at least 30 seconds to a minute ...

Here is my code:

@Override
    public void onClick(View v) {


        if (v == uploadImageButton) {
            // below allows you to open the phones gallery
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(
                    Intent.createChooser(intent, "Complete action using"), 1);
        }
        if (v == postWardrobe) {
            // validate input and that something was entered
            if (nameField.getText().toString().length() < 1
                    || colorField.getText().toString().length() < 1
                    || sizeField.getText().toString().length() < 1
                    || quantityField.getText().toString().length() < 1) {

                // missing required info (null was this but lets see)
                Toast.makeText(getApplicationContext(),
                        "Please complete all sections!", Toast.LENGTH_LONG)
                        .show();
            } else {
                JSONObject dataWardrobe = new JSONObject();

                try {
                    dataWardrobe.put("name", nameField.getText().toString());
                    dataWardrobe.put("brand", brandField.getText().toString());
                    dataWardrobe.put("category", typeField.getSelectedItem()
                            .toString());
                    dataWardrobe.put("color", colorField.getText().toString());
                    dataWardrobe.put("weather", seasonField.getSelectedItem()
                            .toString());
                    dataWardrobe.put("size", sizeField.getText().toString());
                    dataWardrobe.put("quantity", quantityField.getText()
                            .toString());
                    dataWardrobe.put("picture", encodedImage);

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                // make progress bar visible
                progressBarField.setVisibility(View.VISIBLE);

                Log.e("check uri", "selected " + buri);
                // execute the post request
                new dataSend().execute(dataWardrobe);

                // new ImageUploadTask().execute(buri);

            }
        }

    }


    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && requestCode == 1 && null != data) {

            decodeUri(data.getData());
        }
    }

    public void decodeUri(Uri uri) {
        ParcelFileDescriptor parcelFD = null;
        try {

            parcelFD = getContentResolver().openFileDescriptor(uri, "r");
            FileDescriptor imageSource = parcelFD.getFileDescriptor();

            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeFileDescriptor(imageSource, null, o);

            // the new size we want to scale to
            final int REQUIRED_SIZE = 1024;

            // Find the correct scale value. It should be the power of 2.
            int width_tmp = o.outWidth, height_tmp = o.outHeight;
            int scale = 1;
            while (true) {
                if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE) {
                    break;
                }
                width_tmp /= 2;
                height_tmp /= 2;
                scale *= 2;
            }

            // decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            bitmap = BitmapFactory.decodeFileDescriptor(imageSource, null, o2);

            imageview.setImageBitmap(bitmap);

            // encode image into a Base64 to send as a JSON string
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            byte[] b = baos.toByteArray();
            encodedImage = Base64.encodeToString(b, Base64.DEFAULT);

            // can take off below just shows path
            buri = "" + uri;
            imageTextSelect.setText("select : " + uri);
        } catch (FileNotFoundException e) {
            // handle errors
        } catch (IOException e) {
            // handle errors
        } finally {
            if (parcelFD != null)
                try {
                    parcelFD.close();
                } catch (IOException e) {
                    // ignored
                }
        }
    }

private class dataSend extends AsyncTask<JSONObject, Integer, Double> {

        protected Double doInBackground(JSONObject... params) {
            // TODO Auto-generated method stub
            postData(params[0]);
            return null;
        }

        protected void onPostExecute(Double result) {
            progressBarField.setVisibility(View.GONE);
            Toast.makeText(wardrobe.this, "info sent", Toast.LENGTH_LONG)
                    .show();
        }

        protected void onProgressUpdate(Integer... progress) {
            progressBarField.setProgress(progress[0]);
        }

        public void postData(JSONObject dataWardrobe) {

            HttpParams httpParams = new BasicHttpParams();
            HttpClient httpclient = new DefaultHttpClient(httpParams);

            HttpPost httppost = new HttpPost("http://10.0.2.2:3000/wardrobe");

            try {
                Log.v("trying data", "prep");
                // add data
                StringEntity se = new StringEntity(dataWardrobe.toString());
                se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
                        "application/json"));
                httppost.setEntity(se);

                // execute http post request
                HttpResponse response = httpclient.execute(httppost);


            } catch (ClientProtocolException e) {
            } catch (IOException e) {
        }

    }
}
+4
source share
1 answer

94 Unicode, JSON ( JSON UTF-8). , , , , - base85, . 7% base64, , , base64, , , .

U + 0000-U + 00FF, , JSON, ; , - , - 105% - ( ) 25% base85 33% base64.

: base64 , , , , ,

+1

All Articles