private class SendImage_ms extends AsyncTask {
@Override
protected String doInBackground(String... params) {
String id_ms = ID_Item.toString();
String key_api = api_key;
Bitmap image = photo_img_ms;
OutputStream fl_image = null;
String filename = "image.png";
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File file = new File(path, "/image.png");
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(file);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, baos);
try {
baos.writeTo(fOut);
fOut.close();
} catch (IOException e) {
String errorMessage = "Ошибка!";
Toast toast = Toast.makeText(MainActivity.this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
throw new RuntimeException(e);
}
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("id", id_ms)
.addFormDataPart("api_key", api_key)
.addFormDataPart("file", "image.png",
RequestBody.create(MEDIA_TYPE_PNG, new File(path+"/image.png")))
.build();
OkHttpClient client1 = client.newBuilder()
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.retryOnConnectionFailure(false)
.build();
Request request = new Request.Builder()
// .header("User-Agent", "Your-App-Name")
.url(params[0])
.post(requestBody)
.build();
try (Response response = client1.newCall(request).execute()) {
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
System.out.println(response.body().string());
} catch (IOException e) {
return e.toString();
//throw new RuntimeException(e);
}
return "null";
}
@Override
protected void onPostExecute(String data) {
name_ms.setText(data.toString());
//item.setActionView(null);
}