问题描述
- 网络图片 转base64问题 1C
-
public static String imgPathToBase64(String imgFile) { InputStream in = null; byte[] data = null; try { URL url = new URL(imgFile); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); in = conn.getInputStream(); ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buffer = new byte[2048]; int n = 0; while (-1 != (n = in.read(buffer))) { output.write(buffer 0 n); } data = output.toByteArray(); BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(data); } catch (IOException e) { e.printStackTrace(); return """"; } }
这段代码在以前的工程能把网络图片转出正确的base64编码
现在 无论我传的url图片是什么 转出来的结果都一样 而且不是那张图片的base64编码究竟什么原因 我实在想不通了
解决方案
图片转Base64
图片和base64 String互转
Android 图片压缩图片转Base64工具类
解决方案二:
图片url是正确的http地址吗?
如果是,最好等待http返回200的成功码再进行读写,
设置connect的读写:conn.setDoInput(true);
然后试试,读写设置在getInputStream()方法上。
时间: 2024-08-04 00:39:24