{"id":367,"date":"2016-05-17T07:52:14","date_gmt":"2016-05-17T06:52:14","guid":{"rendered":"https:\/\/mokrzycki.eu\/blog\/?p=367"},"modified":"2018-12-04T15:40:39","modified_gmt":"2018-12-04T14:40:39","slug":"example-how-to-take-photo-and-upload-its-thumbnail-to-your-server-using-okhttp3-first-i-convert-it-to-jpeg","status":"publish","type":"post","link":"https:\/\/mokrzycki.eu\/blog\/2016\/05\/17\/example-how-to-take-photo-and-upload-its-thumbnail-to-your-server-using-okhttp3-first-i-convert-it-to-jpeg\/","title":{"rendered":"Example how to take photo and upload it&#8217;s thumbnail to your server using okhttp3 (first I convert it to jpeg)"},"content":{"rendered":"<pre>\/\/part when you click on item and start camera\r\nimageView.setOnClickListener(new View.OnClickListener() {\r\n    @Override\r\n    public void onClick(View v) {\r\n        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);\r\n        Context context = v.getContext();\r\n\r\n        if (takePictureIntent.resolveActivity(context.getPackageManager()) != null) {\r\n            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);\r\n        }\r\n    }\r\n});\r\n\r\n\/\/part when you get the thumbnail from camera\r\n@Override\r\npublic void onActivityResult(int requestCode, int resultCode, Intent data) {\r\n    if (requestCode == REQUEST_IMAGE_CAPTURE &amp;&amp; resultCode == Activity.RESULT_OK) {\r\n        Bundle extras = data.getExtras();\r\n        Bitmap imageBitmap = (Bitmap) extras.get(\"data\");\r\n\r\n        new AsyncTask() {\r\n            @Override\r\n            protected Void doInBackground(Bitmap... bitmaps) {\r\n                OkHttpClient client = new OkHttpClient();\r\n\r\n                ByteArrayOutputStream stream = new ByteArrayOutputStream();\r\n                bitmaps[0].compress(Bitmap.CompressFormat.JPEG, 100, stream);\r\n                byte[] byteArray = stream.toByteArray();\r\n\r\n                \/\/there are some my custom fields form\r\n                RequestBody requestBody = new MultipartBody.Builder()\r\n                        .setType(MultipartBody.FORM)\r\n                        .addFormDataPart(\"submit\", \"\")\r\n                        .addFormDataPart(\"name\", \"myPhotoName\")\r\n                        .addFormDataPart(\"photo\", \"tmp_photo_\" + System.currentTimeMillis(), RequestBody.create(MediaType.parse(\"image\/jpeg\"), byteArray))\r\n                        .build();\r\n\r\n                Request request = new Request.Builder()\r\n                        .url(\"www.example.com\/upload_image.php\")\r\n                        .post(requestBody)\r\n                        .build();\r\n\r\n                Response response = null;\r\n                try {\r\n                    response = client.newCall(request).execute();\r\n                    if (response.isSuccessful()) {\r\n                        Log.d(TAG, \"doInBackground: upload success\");\r\n                    } else {\r\n                        Log.d(TAG, \"doInBackground: upload failed\");\r\n                    }\r\n                } catch (IOException e) {\r\n                    e.printStackTrace();\r\n                }\r\n                return null;\r\n            }\r\n        }.execute(imageBitmap);\r\n    } else {\r\n        \/\/another results\r\n        super.onActivityResult(requestCode, resultCode, data);\r\n    }\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\/\/part when you click on item and start camera imageView.setOnClickListener(new View.OnClickListener() { @Override public void&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[500],"tags":[5,83,238,261,260],"_links":{"self":[{"href":"https:\/\/mokrzycki.eu\/blog\/wp-json\/wp\/v2\/posts\/367"}],"collection":[{"href":"https:\/\/mokrzycki.eu\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mokrzycki.eu\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mokrzycki.eu\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mokrzycki.eu\/blog\/wp-json\/wp\/v2\/comments?post=367"}],"version-history":[{"count":2,"href":"https:\/\/mokrzycki.eu\/blog\/wp-json\/wp\/v2\/posts\/367\/revisions"}],"predecessor-version":[{"id":718,"href":"https:\/\/mokrzycki.eu\/blog\/wp-json\/wp\/v2\/posts\/367\/revisions\/718"}],"wp:attachment":[{"href":"https:\/\/mokrzycki.eu\/blog\/wp-json\/wp\/v2\/media?parent=367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mokrzycki.eu\/blog\/wp-json\/wp\/v2\/categories?post=367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mokrzycki.eu\/blog\/wp-json\/wp\/v2\/tags?post=367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}