如何在ImageView

时间:2018-03-14 07:08:24

标签: android android-studio encoding uiimageview android-image

我从图库中选择一个图像并转换为Base64编码的字符串并发送到服务器

转换为Base64的代码如下:

encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);

但是当我从服务器获得编码的字符串时,它不会显示在ImageView

下面的Base64编码图像字符串:

  

/ 9J / 4AAQSkZJRgABAQAAAQABAAD / 2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB                                                             AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH / 2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEB                                                             AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH / wAARCAi8CLwDASIA                                                             AhEBAxEB / 8QAHwAAAAYDAQEBAAAAAAAAAAAAAgMEBQYHAQgJAAoL / 8QAUxAAAgICAQMEAQMDAwMB                                                             AQAjAQIDEQQSBQYhIgAHEzEIFDJBI0JRCRVhFlJxM2IKFyRygUORGCWCoTRTkqKxJsHRGWPwwvEa                                                             J7LhNTZEc5PS8v / EABwBAAIDAQEBAQAAAAAAAAAAAAIDAAEEBQYHCP / EAEsRAAIBAgUCBAMHAgUD                                                             AwIADwERAiExAAMSQVEEYQUicYEykaEGE0JSscHwYtEUcoLh8SOSsgcVotIzwhbyJEPiUyVzg7MX                                                             NKPT / 9oADAMBAAIRAxEAPwDv0GA82IXerC / vDjszjUK29oC5NMQNT + 8MARSLvQJAjZomLIHAbcFG                                                             on7YWGOp7 + LUF + T0YD3bS2CDehuRJ2cKCVLEKAmt12YLe2wsCI2ryA3JZZXUgIUS / Gyt7fICAe5I                                                             JI7fXEJegCTKs7IiqPbb9CK9LUBeOn / NXbYUfF9juSSujkCqzOxVSXLMPKh3ckKRKqsVPY1QFMdL                                                             ZCMJEKpRIjqVLRsEOvYjbYDtfdte5JFCizeiVayBez6hiqyBRS3YINlkYUlhh2vYbag + WXUljt9h                                                             VpSXUnc2opSAAAATe48hv + 70eL1RJv8Avz / 9Xt7DVlA + yAlSw7UNW3VrpQGNdi6kgUoKqB9rZr + L                                                             se0YZhpuQCKjUH9oo / TJ + gSCT + 4gHxZCqFGQlqY0ANe + pI7kmit60LoKvcx + sNqQXIBSlIUAs1oC                                                             UUlvFtduy / x3EhJBuYgIJUd + 70yAPr29L + hqyaNTqzhhsVtbVVa9GJ1D6fHs9k1VORS2MlWsx0Vd                                                             gS7FQh2CtalnqmBChVq2 / tAHkRGhZAGXz + PdGWlayfpaCqGQMXJJ + / vYlT6NjmVuzmPeSVlJJMhC                                                             kXGQCupcECQmyvcUzWR6mCkkdK1VXLde6u13waw7OpLfIVWwQwBUsQu3bQWfA6kkqRsBXpRdspJW                                                             Q0FNGlQIGvdCbKsewIDqex2YgMUsikWS91saolv7gp + X + 4MDRJruwqiColJmY7qGCkx / 3AArQFsg                                                             QEESlB3P7fuyWKiC7xG3mNV76v1wGjUCShKjGxlIBar3Efw + hvga7OY7ZY7Id1Pke1rUbC3COFFx                                                             9 + ylQBW3rEIHxlkILbgVQIUKbU / U + PxA3IUk7CxTFFYAi2YbEghWNr8njSr4kIyk2U7N96mgRepP                                                             hK6hI1CWtvsdTdKniwBoKYyCe7EjzBHl6tKqT99N6 + / 0eJGwCKQkikF6kUPPst8eLfK / xBS5qyyE                                                             qFcgqF3BBEhJ79jW1E / tX0ZuYyy6bkFrNkUFAOlEFtQHqjZclgKDOfQREDQoFUYgle7KtNJTJ90S                                                             AflP7iTdFvQiV / uAoBdr8TqCK2oMzaUSAD3F2e3qxMBHTFgUQI1L8TCpz / xgoyZsQ3ckr9uN98Hk                                                             FQojRgGUyDXc7PepUBjS1oxIAr9pUlifWHnC7na9CaNgsrfub7LLSrIq1GTTFTZJay0YMVUbKojc                                                             FSDp41Gg2I7qS5ANXQF33b0pjYDs2vgu3kFLFgihi5IJTUr8ax2dgdq / d6E6iAWQzq5IAvvxuOfT                                                             DMCRGXeR3fWWvjA3BDBmAOwZwU3Isa2ttZrViIFdlkQCxo7Gn1omNfkGzR6lmNgg6juwPfuIjVlZ                                                             hvopITxGwPcWSaoUB5G6P14sCElhalkbxsnxbY6gsQa1p0gBF0h7UBXeDUJNkupBFFs613K7KixM                                                             YL9y1KNSGkDVq5F + bIjB / AChsSbG1Cr9HA2GYoQynTTYnUqDsGAOpJuyi0xNBy2oBJaNg5U62wpy                                                             vkwtQAqsAoVV7luzCqogF29HogjNF3s9yzKxViSwALnQWKNVbEAEg7benxkJRKdRs1TYBWO53xMG                                                             S6s0SknW97VTey0QvfVlGpAYrsQQCxHcEUZgDkKrCNaCE2QaBUAMC / kf2tQJatFui3ovzBYAX9Oh                                                             UAttqwBpu5oqDQr78rs + ghSgZSwlJIdexFiyAvcsB + wCwQVJJqyX9XEgvSBQ6Qa780 / gKfMwqoaK                                                             xYESNslkt9i9T5SAWFJeMkEsof8AgehIArswYM1bBSTtIvdAqktHp2BcKXKlmc6kAEltJYUqrLH3                                                             DoLRVUlVJBBve1sqCQLJPax6yYkIvUNZVSbNgCnDWathtrYG1WLrx9ACBIknbd1I4J3vquPrhOuC                                                             Wr6ji177P2 + GmDEsUqrWySMQ7BdAr7bFWWrUEgqasU9jvZjuVIYFXjRSf6VsWYAHsRQoG + 47fzf2                                                             fScgAUpYBO4LErTBT2U2zMCVvc9gLDWFX0pQkoDRDMtovYMfqtCexpCQ1CyoYoPoGjpPmqX6 /杰威尔                                                             d1N6m + GRMTQBb / Y + PRMr0uqhCUYeJKbAsfBk3dHUgt9lTqQP7j6PV0ZdFI7BVIBIJs0ACdvqqIvv                                                             TBqI9E2UC0LYFgS1j6O1ajXvqaS7sj + 3z9DUvGQzMPlkcB9WoKLsALYJNkk / ZKkmz + 80YMAiJCty                                                             Wgd1Vo04dqlgeyqqCNwrE6BFSggDKSWUtGGNKbPehZW719Z7Up7s + xsyFhQHcHUSXTEBFC2T33A7                                                             qMJ5 + a3sBStbgLsCAe5vupDFv52Kr2BPrIDBQpe + xtyQQNe9K3YBiLK7mhRZv5DWIoDzAGIZEgiB                                                             R2NiTv7vEwavySMljwFEMviDS6gMCC72bD2SCKYkkFSM0q7U4dQzAKSGZCFH0xI7 / IV2Bo0BTDa /                                                             AMUUIdaFliFY6qf7SratIwAP + dQWBOqn175kH7W8q / phAWRx3AJI7qCBqDX1ZO1OTDMA0AoYl8g2                                                             3RKFVW3CxMC8bGw7gCRr2bdQAjFgpOzBaWqq6Ki9ZPWVEaqjUQspVdDZBoOFHlZok3oKCkbp / CPJ                                                             jdHO306oCdSzAA0RqVAV6sHxH8GuzFSpChltpC8pUI + gLDsjtsAo2FKG7 / zV9mo + hnJhaAVVMgoE                                                             nc13PyGAMkJH8pX8 / tgaWyGygoghe / cglRRtltbCMAaNju1sCBEeQkbsssg + w48T2Cqq6vqSxBVj                                                             2AGxYs3oTBNuxZJGIIPc + XalO2y7L2JFfflROvryhh8fy9xuAHYozWStpJVsq7Gl7WDVg7PYxJdY                                                             6AwZcdzTelaetpMdVLUOzD0hBbMBnd96VUpaqY7VYxYKoxPfuLJcWSWs2K7kqoAv0CNttQra + QCq                                                             ftqWrBYliPEqWbs5Ygmg1kuQf3rZsAKKDEFgVJ2NElEHYA0SAewJPiDG21RjWQUVO0hAJDEgfQHa                                                             xdAkFB + 5 / VgipBBIJIqN6oImr9O18X8fZe9 / + P46KA1qCCQQAV1bvXYWxsMy0R8gB796JHrLMX0U                                                             sA0ZBpAyh01VFDk / IZCGsDUuboABRt6wRZ2XYP2UBP3yqB3 + 0 + kbsVYkMANNjqxFuV / cFF93plpS                                                             WBAP7VsMaFfz9dj3npt7aL / 92 / y74gmDbV5RUpD0qb / XvgRb49gybKWCEAHuBa / ZAX9zqdTTErYY                                                             + djpACCpU332oLdhQCp3vY9lYggCipHdfWY11VQ9hlUhKBKhQbVAbIYhvMMfuxqSO58pA2DXtRAq                                                             2JTuYzRZAG2A7WCwNXSqTQjclRJlWVRwRR / xWdqhATkzJ2MRUgIAVFPU8r5jhRnjOxEYDswYEsAo                                                             Ozgq9hlvVu / aiGIHrMrByERgEa1ZlCsr / wAFLpwrkldaP23agGBDGSpWJd9WYaFygYKC + yagFiuh                                                             Ut5XY7lvsiZNSg12VhsdCaCsA

我在ImageView中像这样显示这个编码图像字符串

String imageDataBytes = path;
InputStream stream = new ByteArrayInputStream(Base64.decode(imageDataBytes.getBytes(), Base64.URL_SAFE));
Bitmap image = BitmapFactory.decodeStream(stream);
dashboard_img.setImageBitmap(image);

以上代码我使用 Base.64URL_SAFE 默认,但无效。所以请帮我解决这个问题

3 个答案:

答案 0 :(得分:0)

使用invalidate刷新imageview。使用此代码段

try {
            byte[] decodedString = Base64.decode(imageDataBytes, Base64.DEFAULT);
            Bitmap bitmap = BitmapFactory.decodeByteArray(decodedString, 0,decodedString.length);
            dashboard_img.setImageBitmap(bitmap);
            dashboard_img.invalidate();

    } catch (Exception e) {
        e.printStackTrace();
    }

修改 我检查了你的base64字符串,它不是有效的基础64。

答案 1 :(得分:0)

对于imageView中的set base64图像,您可以按照给定的代码进行操作:

    String filePath;
    Bitmap bm = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), Uri.parse(filePath)); // filePath is our BASE64 string
imgView.setImageBitmap(bm);

如果您的base64字符串可能有误,因为这也无法解码在线解码器:https://www.base64decode.org/

答案 2 :(得分:0)

试试这个

Bitmap bmp = BitmapFactory.decodeByteArray(yourBase64String, 0, yourBase64String.length);
imageView.setImageBitmap(Bitmap.createScaledBitmap(bmp, customWidth, customHeight, false));