有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何通过okhttp在Android studio上显示来自服务器(flask)的图像url

如何通过okhttp在Android studio上显示来自服务器(flask)的图像URL。我编写了一个返回img(用base64编码)的方法,并通过该代码发送到我的安卓 studio

@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

然后在我的安卓 studio上通过位图进行响应,并通过以下代码在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


共 (0) 个答案