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

问题描述 投票:0回答:4

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

这是我的代码:

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

这就是我得到的:Log.d

java android encode
4个回答
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");

0
投票

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

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


0
投票

而不是Base64.DEFAULT,使用Base64.NO_WRAP

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

-1
投票

对不起伙计们,我以为Base64.encodeToString()会给我返回imagePath,但我错了。无论如何我得到了解决方案,

这是我用过的代码,

ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    saveBitmap.compress(Bitmap.CompressFormat.JPEG, 75, baos);
                    String path = MediaStore.Images.Media.insertImage(getContentResolver(),saveBitmap,"Title",null);
© www.soinside.com 2019 - 2024. All rights reserved.