Android Studioでアプリケーションを作っているのですが, OkHttpを使って非同期に画像をPOSTする方法がわかりません.
POST先はGyazoです. カメラで撮った画像をそのままuploadしたいのですが, byteで送る方法も分からなく今現在UriでPOSTしようかと考えております. 以下は書いているコードの途中です. よろしくお願いいたします. byteで指定する方法なども知りたいです.
android
1//Gyazoにuploadする関数. カメラで撮った写真をUriで取得してPOST 2public String gyazoUpload(final Uri uri) throws IOException { 3 OkHttpClient client = new OkHttpClient();// client準備 4 5 String id = loadGyazoId();// gyazo idを読み込み 6 String type = getContentResolver().getType(uri);// uriのタイプを取得 7 8 RequestBody body = new MultipartBuilder() 9 .type(MultipartBuilder.FORM) 10 .addPart( 11 Headers.of("Content-Disposition", "form-data; name=\"imagedata\";), 12 RequestBody.create(MediaType.parse(type), file) 13 .build(); 14 15 Request request = new Request.Builder() 16 .url(getEndpointUrl())// cgiのurlを取得 17 .post(body) 18 .build(); 19 20 Response response = client.newCall(request).execute(); 21 22 if (response.isSuccessful()) { 23 String url = response.body().string(); 24 showToast(url, Toast.LENGTH_SHORT);// url表示 25 id = response.header("GyazoId"); 26 if (!TextUtils.isEmpty(id)) 27 showToast(id, Toast.LENGTH_SHORT);//toast表示 28 return url; 29 } 30 return null; 31 32 }
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。