如何将图片(带网址)转换为基础64:
我试过了,但是没有用
InputStream inputStream = null;
try {
inputStream = new BufferedInputStream(
new FileInputStream("https://blahblah/xyz.png"));
} catch (IOException e) {
e.printStackTrace();
}
byte[] bytes;
byte[] buffer = new byte[8192];
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
while ((bytesRead = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}
bytes = output.toByteArray();
String encodedImage = Base64.encodeToString(bytes, Base64.DEFAULT);
首先尝试捕捉错误。任何人都可以建议,可以做些什么?
使用new URL("https://blahblah/xyz.png").openStream()
,它崩溃并出现以下错误:
android.os.NetworkOnMainThreadException
答案 0 :(得分:0)
首先从给定的URL下载图像,然后使用Bitmap将其转换为字节数组,然后执行Base64编码。