Wie kann ich ein Bild in die ImageBB-API in Android hochladen?Java

Java-Forum
Anonymous
 Wie kann ich ein Bild in die ImageBB-API in Android hochladen?

Post by Anonymous »

Ich möchte https://api.imgbb.com/1/upload verwenden, um Bitmaps mithilfe von HttpURLConnection wie folgt auf ImageBB hochzuladen:
Laden Sie ein Bild hoch :
public JsonObject uploadImage(Bitmap bitmap) throws IOException, ExecutionException, InterruptedException {
JsonObject JSONInput = new JsonObject();
JSONInput.addProperty(
"image", BASE_64_IMAGE_STRING
);
JsonObject response = new Requests().execute(JSONInput).get();
return response;
}

Requests-Klasse:
public class Requests extends AsyncTask {
@Override
protected JsonObject doInBackground(JsonObject... jsonObjects) {
URL url = null;
url = new URL("https://api.imgbb.com/1/upload?key=API_ ... ration=300");
HttpURLConnection connection = null;

connection = (HttpURLConnection) url.openConnection();

connection.setRequestMethod("POST")
connection.setRequestProperty("Content-Type", "application/json");
connection.setDoOutput(true);

OutputStream os = connection.getOutputStream()
OutputStreamWriter osw = new OutputStreamWriter(os, StandardCharsets.UTF_8);
osw.write(jsonObjects[0].toString());
osw.flush();
osw.close();

connection.connect()
String result = new BufferedReader(new InputStreamReader(connection.getErrorStream()))
.lines().collect(Collectors.joining("\n"));
StringBuilder response = new StringBuilder();
BufferedReader br = new BufferedReader(
new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))
String responseLine = null;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());

Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonElement jelem = gson.fromJson(response.toString(), JsonElement.class);
return jelem.getAsJsonObject();
}

@Override
protected void onPostExecute(JsonObject jsonObject) {
super.onPostExecute(jsonObject);
}
}

Die jsonObjects bestehen aus einem einzelnen Element, das die Form {"image": base64_representation_of_image"
hat. Ich behalte es jedoch Erhalten Sie {"status_code":400,"error":{"message": "Leere Upload-Quelle.", "code":130},"status_txt": "Bad Request" von der API zurück kippen Ich scheine herauszufinden, was ich falsch mache.
Ich kann keine Ressourcen finden, die ähnliche APIs in Android verwenden.
Ich versuche, die HttpURLConnection-Klasse zu verwenden, um API-Anfragen an ImageBB zu stellen. Es scheint jedoch, dass sie die Base64-Bildzeichenfolge, die ich übergebe, nicht erkennt.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post