Java 将图像字节数组保存到.net webservice并检索它
我有一个使用ksoap2库的.net ASMX Web服务。在服务中,我首先保存用户图像,然后检索它。但是,一旦我检索到它,字节数组就完好无损,但是BitmapFactory无法对其进行解码并返回空值 要转换为字节数组,请执行以下操作: webservice接受byte[]格式的bytearray 要将阵列转换为位图,请执行以下操作: 从部分分析来看,字节数组似乎没有改变。那么为什么解码返回空值呢?是否有更好的方法保存图像并通过Web服务传递?我没有分析整个字节数组,所以我猜它可能有点变化 有什么想法吗?非常感谢 更新: 我刚刚尝试使用以下命令将字节[]转换为字符串:Java 将图像字节数组保存到.net webservice并检索它,java,android,asmx,bytearray,android-ksoap2,Java,Android,Asmx,Bytearray,Android Ksoap2,我有一个使用ksoap2库的.net ASMX Web服务。在服务中,我首先保存用户图像,然后检索它。但是,一旦我检索到它,字节数组就完好无损,但是BitmapFactory无法对其进行解码并返回空值 要转换为字节数组,请执行以下操作: webservice接受byte[]格式的bytearray 要将阵列转换为位图,请执行以下操作: 从部分分析来看,字节数组似乎没有改变。那么为什么解码返回空值呢?是否有更好的方法保存图像并通过Web服务传递?我没有分析整个字节数组,所以我猜它可能有点变化 有什
Base64.encodeToString( bos.toByteArray(), Base64.DEFAULT);
并使用以下方式进行解码:
byte[] blob= Base64.decode(info.get(Main.KEY_THUMB_BYTES));
现在我得到的只是一张白色的照片。我不确定这里出了什么问题。请帮忙
更新:
我将此图像存储在一个数据库中,存储在varcharmax类型的列中。我应该将这个字节数组字符串存储在不同的sql数据类型中吗?我对SQL不是很有经验,所以我使用了varchar,因为它没有将文本转换为unicode,我认为这可能对字节数组有好处
谢谢 将字节数组转换为易于传输的字符串Base64:
public static String bitmapToBase64(Bitmap bitmap) {
byte[] bitmapdata = bitmapToByteArray(bitmap);
return Base64.encodeBytes(bitmapdata);
}
public static byte[] bitmapToByteArray(Bitmap bitmap) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /* ignored for PNG */, bos);
byte[] bitmapdata = bos.toByteArray();
return bitmapdata;
}
及
此外,您不仅可以对图像文件执行此操作,还可以对每种文件类型执行此操作:
public static String fileToBase64(String path) throws IOException {
byte[] bytes = fileToByteArray(path);
return Base64.encodeBytes(bytes);
}
public static byte[] fileToByteArray(String path) throws IOException {
File imagefile = new File(path);
byte[] data = new byte[(int) imagefile.length()];
FileInputStream fis = new FileInputStream(imagefile);
fis.read(data);
fis.close();
return data;
}
public static void base64ToFile(String path, String strBase64)
throws IOException {
byte[] bytes = Base64.decode(strBase64);
byteArrayTofile(path, bytes);
}
public static void byteArrayTofile(String path, byte[] bytes)
throws IOException {
File imagefile = new File(path);
File dir = new File(imagefile.getParent());
if (!dir.exists()) {
dir.mkdirs();
}
FileOutputStream fos = new FileOutputStream(imagefile);
fos.write(bytes);
fos.close();
}
我试过你告诉我的,但我还是得到了一个白色的图像。我想知道这是否与Web服务或数据库有关。我将图像存储为varcharmax,我应该使用不同的类型吗?发送到Webservice的base64的值是否与从中接收到的base64的值相同?嗯,似乎……看起来差不多,但我没有比较它,因为它非常大……我可以在几分钟内确认这一点……@Pathachiever11我也遇到了同样的问题……看到这个问题了吗
public static String bitmapToBase64(Bitmap bitmap) {
byte[] bitmapdata = bitmapToByteArray(bitmap);
return Base64.encodeBytes(bitmapdata);
}
public static byte[] bitmapToByteArray(Bitmap bitmap) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /* ignored for PNG */, bos);
byte[] bitmapdata = bos.toByteArray();
return bitmapdata;
}
public static Bitmap base64ToBitmap(String strBase64) throws IOException {
byte[] bitmapdata = Base64.decode(strBase64);
Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata, 0,
bitmapdata.length);
return bitmap;
}
public static String fileToBase64(String path) throws IOException {
byte[] bytes = fileToByteArray(path);
return Base64.encodeBytes(bytes);
}
public static byte[] fileToByteArray(String path) throws IOException {
File imagefile = new File(path);
byte[] data = new byte[(int) imagefile.length()];
FileInputStream fis = new FileInputStream(imagefile);
fis.read(data);
fis.close();
return data;
}
public static void base64ToFile(String path, String strBase64)
throws IOException {
byte[] bytes = Base64.decode(strBase64);
byteArrayTofile(path, bytes);
}
public static void byteArrayTofile(String path, byte[] bytes)
throws IOException {
File imagefile = new File(path);
File dir = new File(imagefile.getParent());
if (!dir.exists()) {
dir.mkdirs();
}
FileOutputStream fos = new FileOutputStream(imagefile);
fos.write(bytes);
fos.close();
}