Как я могу показать URL изображения с сервера (фляги) на Android studio от okhttp. Я пишу метод для возврата img (закодировать с помощью base64) и отправляю в мою андроид студию по этому коду
@app.route('/', methods = ['GET', 'POST'])
def handle_request():
files_ids = list(flask.request.files)
print("\nNumber of Received Images : ", len(files_ids))
image_num = 1
n = 0
for file_id in files_ids:
print("\nSaving Image ", str(image_num), "/", len(files_ids))
if n == 0 or file_id == 'image0':
imagefile = flask.request.files[file_id]
filename = werkzeug.utils.secure_filename(imagefile.filename)
print("Image Filename : " + imagefile.filename)
timestr = time.strftime("%Y%m%d-%H%M%S")
path = r'C:\Users\Malee\Desktop\AndroidFlask-master\Part 1\FlaskServer\uploads'
imagefile.save(os.path.join(path,filename))
#os.system('python test_grader.py')
n = n+1
if n > 0 and n!=0:
if file_id != 'image0':
imagefile = flask.request.files['image'+str((len(files_ids)-1))]
filename = werkzeug.utils.secure_filename(imagefile.filename)
print("Image Filename : " + imagefile.filename)
timestr = time.strftime("%Y%m%d-%H%M%S")
path = r'C:\Users\Malee\Desktop\AndroidFlask-master\Part 1\FlaskServer\Answer'
imagefile.save(os.path.join(path,filename))
#os.system('python test_grader2.py')
break
image_num = image_num + 1
print("\n")
path = r'C:\Users\Malee\Desktop\AndroidFlask-master\Part 1\FlaskServer\download'
#return send_file(os.path.join(path,newest()), mimetype='image/gif')
path = r'C:\Users\User\Desktop\AndroidFlask-master\Part 1\FlaskServer\download'
data_uri = base64.b64encode(open('DN.jpg', 'rb').read()).decode('utf-8')
img_tag = '<img src="data:image/png;base64,{0}">'.format(data_uri)
return data_uri
А затем на мою андроид студию с помощью растрового изображения и показываю в ImageView этот код
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
// Cancel the post on failure.
call.cancel();
Log.d("FAIL", e.getMessage());
// In order to access the TextView inside the UI thread, the code is executed inside runOnUiThread()
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView responseText = findViewById(R.id.responseText);
responseText.setText("Failed to Connect to Server. Please Try Again.");
}
});
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
// In order to access the TextView inside the UI thread, the code is executed inside runOnUiThread()
runOnUiThread(new Runnable() {
@Override
public void run() {
TextView responseText = findViewById(R.id.responseText);
ImageView responesImg = findViewById(R.id.imageView2);
/*InputStream inputStream = response.body().byteStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);*/
ResponseBody in = response.body();
InputStream inputStream = in.byteStream();
Log.i("inputStream","inputstream value = "+inputStream);
// convert inputstram to bufferinoutstream
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
Bitmap bitmap=BitmapFactory.decodeStream(bufferedInputStream);
Log.i("bitmap","bitmap value = "+bitmap);
showImage(bitmap);/*
MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "test.jpg" , "none");
responesImg.setImageBitmap(bitmap);*/
}
});
}
});
И когда я запускаю в моем телефоне, можно показать заголовок, но не могу показать img.