Android+Java 从服务器上的 URL 获取图像并作为字符串传递给客户端:Base64 解码和编码不起作用
Android+Java get image from URL on server and pass as string to client: Base64 decode and encode not working
在 Java 服务器中,我从外部服务 URL 获取图像,例如:
InputStream in = new java.net.URL(imageWebServiceURL).openStream();
String resultToCleint = org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString(IOUtils.toByteArray(in));
然后在 Android 上,我将其解析为:
byte[] imageAsBytes = Base64.decode(resultToCleint.getBytes(), Base64.DEFAULT);
imageView.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));
结果:图像未显示,errors/exceptions既不在服务器也不在客户端。
这里有什么问题?
编辑:在 android 我使用 class android.util.Base64
谢谢,
使用它来转换为 base 64
public static String uploadPic(Bitmap bm) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
String encoded = ""+ Base64.encodeToString(byteArray, Base64.DEFAULT);
return encoded;
}
check if image is uploaded then using volley String request object download the string response using this code convert it back.
public Bitmap StringToBitMap(String encodedString){
try {
byte [] encodeByte=Base64.decode(encodedString,Base64.DEFAULT);
Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
return bitmap;
} catch(Exception e) {
e.getMessage();
return null;
}
}
如评论所述,假设 base64Content
是从您的网络 service/server-side 应用程序响应的 base64 字符串,您可以参考以下示例代码:
String base64Content = jsonObject.getString("Base64Content");
byte[] bytes = Base64.decode(base64Content, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
此外,如果您的服务器通过 gzip 或 deflate 压缩响应数据,您的客户端应用程序必须先解压缩数据。
希望对您有所帮助!
使用 Picasso 库加载图像:
只需添加 1 行代码即可在 ImageView 上显示图像
//Loading image from below url into imageView
Picasso.with(this)
.load("YOUR IMAGE URL HERE")
.into(imageView);
您可以从 here
了解更多
在 Java 服务器中,我从外部服务 URL 获取图像,例如:
InputStream in = new java.net.URL(imageWebServiceURL).openStream();
String resultToCleint = org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString(IOUtils.toByteArray(in));
然后在 Android 上,我将其解析为:
byte[] imageAsBytes = Base64.decode(resultToCleint.getBytes(), Base64.DEFAULT);
imageView.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));
结果:图像未显示,errors/exceptions既不在服务器也不在客户端。
这里有什么问题?
编辑:在 android 我使用 class android.util.Base64
谢谢,
使用它来转换为 base 64
public static String uploadPic(Bitmap bm) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
String encoded = ""+ Base64.encodeToString(byteArray, Base64.DEFAULT);
return encoded;
}
check if image is uploaded then using volley String request object download the string response using this code convert it back.
public Bitmap StringToBitMap(String encodedString){
try {
byte [] encodeByte=Base64.decode(encodedString,Base64.DEFAULT);
Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
return bitmap;
} catch(Exception e) {
e.getMessage();
return null;
}
}
如评论所述,假设 base64Content
是从您的网络 service/server-side 应用程序响应的 base64 字符串,您可以参考以下示例代码:
String base64Content = jsonObject.getString("Base64Content");
byte[] bytes = Base64.decode(base64Content, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
此外,如果您的服务器通过 gzip 或 deflate 压缩响应数据,您的客户端应用程序必须先解压缩数据。
希望对您有所帮助!
使用 Picasso 库加载图像:
只需添加 1 行代码即可在 ImageView 上显示图像
//Loading image from below url into imageView
Picasso.with(this)
.load("YOUR IMAGE URL HERE")
.into(imageView);
您可以从 here
了解更多