【问题标题】:Save Image Byte Array to .net webservice and retrieve it将图像字节数组保存到 .net webservice 并检索它
【发布时间】:2012-07-01 03:52:36
【问题描述】:

我有一个使用 ksoap2 库使用的 .net ASMX Web 服务。在服务中,我先保存用户图像,然后再检索它。但是,一旦我检索到它,字节数组是完整的,但 BitmapFactory 无法对其进行解码并返回 null。

转换为字节数组:

Bitmap viewBitmap = Bitmap.createBitmap(imageView.getWidth(),
imageView.getHeight(), Bitmap.Config.ARGB_8888);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
viewBitmap.compress(CompressFormat.PNG, 0 /* ignored for PNG */, bos);
byte[] bitmapdata = bos.toByteArray();

webservice 接受 byte[] 格式的字节数组。

将数组转换为位图:

byte[] blob= info.get(Main.KEY_THUMB_BYTES).getBytes();
Bitmap bmp=BitmapFactory.decodeByteArray(blob,0,blob.length); // Return null :(
imageView.setImageBitmap(bmp);

从部分分析来看,字节数组似乎没有改变。那为什么解码返回null?有没有更好的方法来保存图像并通过网络服务传递它?我没有分析整个字节数组,所以我猜它可能已经改变了一点。

有什么想法吗?非常感谢!

更新: 我只是尝试使用以下方法将 byte[] 转换为字符串:

Base64.encodeToString( bos.toByteArray(), Base64.DEFAULT);

并使用解码:

byte[] blob= Base64.decode(info.get(Main.KEY_THUMB_BYTES));

现在我得到的只是一张白色图片。我不确定这里有什么问题。请帮忙。

更新: 我将此图像存储在数据库中,在 varchar(max) 类型的列中。我应该将此字节数组字符串存储在不同的 sql 数据类型中吗?我对 SQL 不太熟悉,所以我使用了 varchar,因为它不会将文本转换为 unicode,我认为这可能对字节数组有好处。

谢谢!

【问题讨论】:

    标签: java android asmx bytearray android-ksoap2


    【解决方案1】:

    将您的字节数组转换为 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 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();
    }
    

    【讨论】:

    • 我试过你告诉我的,我仍然得到一个白色的图像。我想知道这是否与网络服务或数据库有关。我将图像存储为 varchar(max),我应该使用其他类型吗?
    • 发送到Webservice的base64的值和从它接收的base64的值一样吗?
    • 嗯,看起来....看起来差不多,但我没有比较它,因为它很大......我可以在几分钟内确认...... ....
    • @Pathachiever11 我也遇到了同样的问题...看到这个问题stackoverflow.com/questions/14681643/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 1970-01-01
    • 1970-01-01
    • 2011-10-15
    • 1970-01-01
    相关资源
    最近更新 更多