【问题标题】:Android Volley: CalledFromWrongThreadException when uploading an image as StringAndroid Volley:将图像上传为字符串时出现 CalledFromWrongThreadException
【发布时间】:2018-10-24 07:15:58
【问题描述】:

我正在尝试使用 Android 中的 Volley 将图像作为字符串上传到我的服务器。

仅在旧设备上我会收到 CalledFromWrongThreadException。该代码在较新的设备(Android 7+)上完美运行。

错误消息:

E/Volley:[231] NetworkDispatcher.processRequest:未处理的异常 android.view.ViewRootImpl$CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能接触其视图。

我的 POST 请求:

StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
                new Response.Listener<String>() { ...

            @Override
            protected Map<String, String> getParams() {
                ...
                params.put("png", BitMapToString(cropImageView.getCroppedImage()));
                return params;
            }...



public String BitMapToString(Bitmap bitmap){
    ByteArrayOutputStream baos=new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG,100, baos);
    byte [] b=baos.toByteArray();
    String temp=Base64.encodeToString(b, Base64.DEFAULT);
    return temp;
}

【问题讨论】:

    标签: android android-volley


    【解决方案1】:

    在请求之外获取图像解决了问题!

    final Bitmap imageBitmap = cropImageView.getCroppedImage();
    
    //REQUEST using imageBitmap below
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-19
      • 2017-01-23
      • 1970-01-01
      • 2019-11-03
      • 2017-07-01
      • 2011-09-07
      • 2016-07-31
      • 1970-01-01
      相关资源
      最近更新 更多