Base64.encodeToString()在Android中不起作用

时间:2018-12-08 03:13:31

标签: java android encode

所以我有一个位图,现在我想将其转换为imageUri(或字符串), 我在这里使用此代码,但它不能正常工作,而不是返回imageUri并返回一个长随机文本。

这是我的代码:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    saveBitmap.compress(Bitmap.CompressFormat.JPEG, 75, baos);
                    String path = Base64.encodeToString(baos.toByteArray(),Base64.DEFAULT);

这就是我得到的:Log.d

4 个答案:

答案 0 :(得分:1)

尝试以下方式,应该可以解决

byte[] data = convert image in byte.
String base64 = Base64.encodeToString(data, Base64.DEFAULT);


byte[] data = Base64.decode(base64, Base64.DEFAULT);
String text = new String(data, "UTF-8");

答案 1 :(得分:0)

Base64.encodeToString()将字节数组编码为字符串。这不是你的uri。而是这是您在Base64中的图像/位图。您可以使用合适的Base64.decode来获取字节数组。

要获取uri,您可以使用其他一些选项,包括      Uri.fromFile(new File(“ your_file_path));

答案 2 :(得分:0)

使用Base64.DEFAULT代替Base64.NO_WRAP

String path = Base64.encodeToString(baos.toByteArray(),Base64.NO_WRAP);

答案 3 :(得分:-1)

对不起,我认为Base64.encodeToString()将返回imagePath,但我错了。无论如何,我得到了解决方案,

这是我使用的代码,

from bs4 import BeautifulSoup
import requests
import lxml

url = 'https://www.wunderground.com/history/daily/in/chennai/VOMM/date/2017-1-1';
content = requests.get(url).content
soup = BeautifulSoup(content, 'lxml')
table = soup.find('table', {'id' : 'history-observation-table'})
print(table)