java将图像从flask服务器发送到安卓应用程序
嗨,我是安卓应用程序的新手,我想发送一张图片作为对HttpPost请求的响应。我正在尝试从flask python服务器向安卓客户端发送响应图像,并更新ImageView。有什么建议吗
Flask服务器代码:
from flask import Flask, request, jsonify, make_response
import werkzeug
import matplotlib.pyplot as plt
import numpy as np
#import single_image_inference as single_image
from io import BytesIO
from PIL import Image
from base64 import encodebytes
import time
app = Flask(__name__)
@app.route('/x', methods = ['GET', 'POST'])
def get_response_image(image_path):
return send_file(image_path, mimetype='image/jpg')
app.run(host="0.0.0.0", port=5000, debug=True)
Android客户端代码:
void postRequest(String postUrl, RequestBody postBody) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(postUrl)
.post(postBody)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
// Cancel the post on failure.
call.cancel();
// In order to access the TextView inside the UI thread, the code is executed inside runOnUiThread()
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(Profile.this, "Something went wrong:" + " " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
runOnUiThread(new Runnable() {
@Override
public void run() {
InputStream inputStream = response.body().byteStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
}
});
}
});
}
我收到以下消息
D/skia: --- Failed to create image decoder with message 'unimplemented'
I/e.myapplicatio: Background young concurrent copying GC freed 2321(176KB) AllocSpace objects, 16(2200KB) LOS objects, 31% free, 4199KB/6107KB, paused 69us total 658.626ms
共 (0) 个答案