Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
安卓&x2B;Java从服务器上的URL获取图像并作为字符串传递给客户端:Base64解码和编码不工作_Java_Android_Base64_Android Imageview - Fatal编程技术网

安卓&x2B;Java从服务器上的URL获取图像并作为字符串传递给客户端:Base64解码和编码不工作

安卓&x2B;Java从服务器上的URL获取图像并作为字符串传递给客户端:Base64解码和编码不工作,java,android,base64,android-imageview,Java,Android,Base64,Android Imageview,在Java server中,我从外部服务URL获取图像,如: InputStream in = new java.net.URL(imageWebServiceURL).openStream(); String resultToCleint = org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString(IOUtils.toByteArray(in)); 然后在Android上,我将其解析为: byte[] imageAsB

在Java server中,我从外部服务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));
结果:未显示图像,服务器和客户端均未出现错误/异常

这里有什么问题

编辑:在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;
   }
}

使用此选项转换为基数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
是web服务/服务器端应用程序响应的base64字符串,您可以参考以下示例代码:

String base64Content = jsonObject.getString("Base64Content");
byte[] bytes = Base64.decode(base64Content, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); 

此外,如果您的服务器通过gzip或deflate压缩响应数据,则您的客户端应用程序必须首先解压缩数据


希望这有帮助

如前所述,假设
base64Content
是web服务/服务器端应用程序响应的base64字符串,您可以参考以下示例代码:

String base64Content = jsonObject.getString("Base64Content");
byte[] bytes = Base64.decode(base64Content, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); 

此外,如果您的服务器通过gzip或deflate压缩响应数据,则您的客户端应用程序必须首先解压缩数据


希望这有帮助

使用毕加索库加载图像:

只需添加1行代码即可在ImageView上显示图像

//Loading image from below url into imageView

Picasso.with(this)
   .load("YOUR IMAGE URL HERE")
   .into(imageView);

您可以通过使用毕加索库加载图像了解更多信息:

只需添加1行代码即可在ImageView上显示图像

//Loading image from below url into imageView

Picasso.with(this)
   .load("YOUR IMAGE URL HERE")
   .into(imageView);


您可以从

中了解更多信息,我忘了提及:图像url是外部服务,我有自己的服务器(后端)调用外部服务,以便稍后将其传递给android…我忘了提及:图像url是外部服务,我有自己的服务器(后端)调用externals服务以便稍后将其传递给android…让我们假设
base64Content
在从web服务响应的base64字符串中,您可以使用
byte[]bytes=base64.decode(base64Content,base64.DEFAULT);位图位图=位图工厂.decodeByteArray(字节,0,字节.长度)此外,如果服务器通过gzip或deflate压缩响应数据,则客户端必须解压缩数据first@BNK这种情况下,响应不是常规的,例如,在我的项目中,
String base64Content=jsonObject.getString(“base64Content”)。我的web服务是Asp.NETWebAPI,我使用了
Convert.ToBase64String(…)
@BNK,如果你知道如何解决这个问题,为什么不呢?只有通过回答,您才能获得声誉等。让我们假设
base64Content
在从web服务响应的base64字符串中,您可以使用
byte[]bytes=base64.decode(base64Content,base64.DEFAULT);位图位图=位图工厂.decodeByteArray(字节,0,字节.长度)此外,如果服务器通过gzip或deflate压缩响应数据,则客户端必须解压缩数据first@BNK这种情况下,响应不是常规的,例如,在我的项目中,
String base64Content=jsonObject.getString(“base64Content”)。我的web服务是Asp.NETWebAPI,我使用了
Convert.ToBase64String(…)
@BNK,如果你知道如何解决这个问题,为什么不呢?只有通过回答,你才能获得声誉等。这正是我在Android中所做的,它不起作用。在你的问题中,你使用了resultToCleint.getBytes()。删除getBytes,然后再次检查。因此,如果您有响应图像的直接url,这里是二进制数据,而不是base64字符串,您可以尝试使用毕加索或使用volley/okhttp…requestIt加载它,具体取决于您的要求。正如我所说,如果你有一个直接链接到图像,你可以试试毕加索,很好:)。我现在很忙,我会很快回复你的新评论,再见!我以前从未尝试过谷歌的服务。如果明天有空,我会了解更多这正是我在Android中所做的,它不起作用。在你的问题中,你使用了resultToCleint.getBytes()。删除getBytes,然后再次检查。因此,如果您有响应图像的直接url,这里是二进制数据,而不是base64字符串,您可以尝试使用毕加索或使用volley/okhttp…requestIt加载它,具体取决于您的要求。正如我所说,如果你有一个直接链接到图像,你可以试试毕加索,很好:)。我现在很忙,我会很快回复你的新评论,再见!我以前从未尝试过谷歌的服务。如果明天有空的话,我会了解更多的问题,这里我没有url,而是图像数据作为解码base64。所以我想也许可以使用url链接,并问了这个问题:这里我没有url,而是图像数据作为解码的base64。所以我想也许可以使用url链接,并问: