为什么 android 工作室说我的 BufferedWriter 已关闭?

Why does android studio say that my BufferedWriter is closed?

我想通过将图像编码为 base64 字符串将其上传到我的数据库槽。

当我有图像并按下 "upload" 按钮时,我的代码会执行此操作。

private void upLoadPicture()
{
    class UploadImage extends AsyncTask<Bitmap,Void,String>
    {
        //ProgressDialog loading;
        @Override
        protected String doInBackground(Bitmap... args) {
            RequestHandler rh = new RequestHandler();
            Bitmap tempBitmap = args[0];
            String tempImageString = ConvertBitMapToString(tempBitmap);
            HashMap<String,String> map = new HashMap<String,String>();
            map.put(UPLOAD_KEY, tempImageString);
            rh.SendPostRequest(DATABASE_REQUEST_PATH, map);
            return null;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
          //  loading = ProgressDialog.show(getApplicationContext(), "Uploading Image", "Please wait", true,true);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
         //   loading.dismiss();
        }
    }

    UploadImage Upi = new UploadImage();
    Upi.execute(bitmap);
}

下面是方法和class

/*We store the picture in the database as a encoded 64-Base String so we need to encode it first*/
protected String ConvertBitMapToString(Bitmap bm)
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG,100,baos);
    byte[] b = baos.toByteArray();
    String tempString = Base64.encodeToString(b,Base64.DEFAULT);
    return tempString;
}

我的 class 应该处理上传

public class RequestHandler {

public void SendPostRequest(String url, HashMap<String,String> map)
{
    String tempMessage,a = "";
    HashMap<String,String> ImageHashmap = map;
    try {
        URL sendmeurl = new URL(url);
        HttpURLConnection con = (HttpURLConnection) sendmeurl.openConnection();

        con.setRequestMethod("POST");
        con.setDoOutput(true);
        con.setDoInput(true);

        OutputStream os = con.getOutputStream();
        InputStream is = con.getInputStream();

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));

        bw.write(GetPOSTURL(ImageHashmap));
        bw.flush();
        bw.close();

        BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        while ((tempMessage = br.readLine()) != null){
            a = tempMessage;
        }
        Log.d("recieve", a);
        con.disconnect();
    } catch (Exception e){
        e.printStackTrace();
    }
}

private String GetPOSTURL(HashMap<String,String> MapPOST)
{
    StringBuilder result = new StringBuilder();
    boolean first = true;
    for (Map.Entry<String, String> entry : MapPOST.entrySet()) {
        if (first)
            first = false;
        else
            result.append("&");

        try {
            result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
            result.append("=");
            result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    return result.toString();
}

在此 class 中,我收到一条错误消息,提示我的 BufferedWriter 在执行我的方法 GetPOSTUrl() 之前已关闭,我不知道为什么。我不确定如何更深入地研究这个问题。

编辑

这是我接收位图的方式

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null)  {
        filepath = data.getData();
        try {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},FETCH_PICTURE_REQUEST);
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode){
        case FETCH_PICTURE_REQUEST:
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
                try {
                    bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filepath);
                    _IV.setImageBitmap(bitmap);
                } catch(Exception e){
                    e.printStackTrace();
                }
            } else {
                Toast.makeText(getApplicationContext(), "No Permission", Toast.LENGTH_SHORT).show();
            }
        break;
        default:
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

位图就在我的 class

里面
private Uri filepath;
private Bitmap bitmap;

我今天遇到了同样的问题。像这样写入 OutputStream 后尝试调用 getInputStream();

...   
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);

OutputStream os = con.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));

bw.write(GetPOSTURL(ImageHashmap));
bw.flush();
bw.close();

InputStream is = con.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
while ((tempMessage = br.readLine()) != null){
    a = tempMessage;
}
...

我完全不知道为什么会这样。 getInputStream() 似乎以某种方式关闭了 OutputStream。也许这与新的 Android Studio 2.2 有关,因为我以前从未注意到这种行为。