data:image / png; base64转换,但返回null(运行以下代码)

时间:2019-01-16 12:01:23

标签: android bitmap base64

这是我的代码。位图返回“”;
你知道为什么吗?以下是步骤:( base64字符串是正确的,因为我用它来转换http://imgbase64.duoshitong.com/上的位图,或者您可以使用其他位图来转换成base64,下面的代码在网络中是没有答案的工作。)

  String  content = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACYAAAAmCAYAAACoPemuAAAABGdBTUEAALGPC/xhBQAABURJREFUWAnNWFtMXFUU3XcKBHBmiMZERRmIAuqXtcbE1A/7EDE24iPGxGj88MtHP/TDRzR8gYmPRP1Sv4lKYpqgxBqFWtuAVNJE/VIrmMBAWySNAjMF5HHHte6cPTlzZi4MUxrZyWHvsx9rL849986515PypAJlUYwaDNo6YMqaNZZgp80c6vJIBLBXYiTa29v3DA0NvTAzM/PZwsLCyeXl5TPr6+spDtr0McYc5rLG1BJj28QDUhzjxsHBwWfQ9ITv+6uZEoW5rGEtMQwWMS9JKlHd2NPT82gqlTpVIpfQNGIQi5gYxC5LauLxeEsymfwotFOZAWISG6y4R7ck8ba2tt1zc3PHyuy9aRmx2QOsuE0KpNi1rkHBzf39/b3V1dW3FFQYx9g/Il//mZGB6YxMXszIFO+/jEhDrUjTFZ603eDJAzd50sLbJURwo/ze0dHxBPbfGaQQIScusUoscROXuq6u7mAuyzJ+/kvkjRFfvjnvZ72KAFLi2vDdf11E3twbkduvsUAsc35+/rtEIvEcbpAJuFc1pFCc006A1GsNDQ3PaoLqdTR59YQv7/+WwcKQReniAfqlWz15e19EdtkdDcTU1NTHIPcWpkmMANx+rsRwx9xRjNTcvyKHvvDlvV99webJlrI8bLChxmiihrXEIJYr7Mne8Mc0pvxJsAm386fRaPQuDVJzpQ71+TKgl84OlmHfh0t79JHClUun0z/GYrEnATmB4euK1WED7nNJse/L34PUOewnXYFL1MQipivsTQ7w1zGmK5bA5usB43voVPkJG/3OI+uS8ckGwmyatqbf9emcMYop1zov4snpx3bJHueGwBU7iZvvaVQkuWIV+D27GozvJoYtrw9jTykpBrSBrW3bzWFM41aMmMR2hRzIBf4KEot2dnbu9TyPJ4Sc/PG3yMBZU6wNVDOrmK0+W9u56oeP2HwW2kIO5AJflMRqWltbC1brKB6egRiVnZi/ts+285KK5Grc1Hw1Xljc3NxMLjXBpcQTvl5rVA8kUcS6yziCHtrQ6NraWnIJLmVFVVXVtU5cJtOGGAMkR7FJ6ly1nUMfxfU59ZMpTcim86/hUhGcPCsrKwtWbPqiSdZa1Yqhc9X0q606zGf804s08sVwCVaMERsqm1noyUfYhpmnD6t8rKAz99ja6urq+fyYyPU4JeRdOqZv86ivdrviVzzLZS0gtrKyMuOmNMasf0dXz9a2zWJ7rraC6tzWsPN6mFzDJUtscXHxrGKobkuAGIFsMAZ1btvqU60xztXnaoSCHsy1xHAJiC2Nj4+PWLHAfLDZIUavNrJ1mN/NKZLX0WJdFcYhY2NjP0At8VKmu7q6RnA04ftgTlqvEjnYwDBEm2Rn+X8Z20yK1BPbPd2SQ3d39ynApZVy+I/4J/gR5xlsGwU/PXL6qc1/xNkyNTo62uP25q//4dsMd5ebO9di2x9iE9M9WbDccEjR1hXjNQs9KLZ/7svxZOFpgABblQOJiHz7eOkHRXa90NfX967biGf0Iw9HZD8Ac3uNK6GDBboy6rPnGoePGMQqdu43vS8gPVgBXTGW097wZeTFY758+AteRba457innt/tyQf3FidV7GXEJkZym76+8VT7Co7GxydLu7QHGiPyzv5I0T3FhqW8vjGPUtILLw+SX45lZHAiI8mFjJzjxyZIPT5OJeJ44W3y5CE8p/jYCZONXnjDav73TwRhxOjfkR9VlDA/Fe24z1BKjjfIjvtwp+So+RDOfeocHh4+PDs724v30bBPnb3MKfdTp/u4sIlsZPNIflk/Dv8H0I4Y2Oy/OicAAAAASUVORK5CYII=";

    Bitmap bitmap = null;
    try {
        byte[] bitmapArray = Base64.decode(content.split(",")[1], Base64.DEFAULT);
        bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
    } catch (Exception e) {
        e.printStackTrace();
    } catch (OutOfMemoryError e) {
        if (QLog.isColorLevel()) {
            QLog.d(TAG, QLog.CLR, "Texture: cannot decode base64: " + QLog.getStackTraceString(e));
        }
    }
    return bitmap;

3 个答案:

答案 0 :(得分:0)

1。字符串到位图

 String  content = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACYAAAAmCAYAAACoPemuAAAABGdBTUEAALGPC/xhBQAABURJREFUWAnNWFtMXFUU3XcKBHBmiMZERRmIAuqXtcbE1A/7EDE24iPGxGj88MtHP/TDRzR8gYmPRP1Sv4lKYpqgxBqFWtuAVNJE/VIrmMBAWySNAjMF5HHHte6cPTlzZi4MUxrZyWHvsx9rL849986515PypAJlUYwaDNo6YMqaNZZgp80c6vJIBLBXYiTa29v3DA0NvTAzM/PZwsLCyeXl5TPr6+spDtr0McYc5rLG1BJj28QDUhzjxsHBwWfQ9ITv+6uZEoW5rGEtMQwWMS9JKlHd2NPT82gqlTpVIpfQNGIQi5gYxC5LauLxeEsymfwotFOZAWISG6y4R7ck8ba2tt1zc3PHyuy9aRmx2QOsuE0KpNi1rkHBzf39/b3V1dW3FFQYx9g/Il//mZGB6YxMXszIFO+/jEhDrUjTFZ603eDJAzd50sLbJURwo/ze0dHxBPbfGaQQIScusUoscROXuq6u7mAuyzJ+/kvkjRFfvjnvZ72KAFLi2vDdf11E3twbkduvsUAsc35+/rtEIvEcbpAJuFc1pFCc006A1GsNDQ3PaoLqdTR59YQv7/+WwcKQReniAfqlWz15e19EdtkdDcTU1NTHIPcWpkmMANx+rsRwx9xRjNTcvyKHvvDlvV99webJlrI8bLChxmiihrXEIJYr7Mne8Mc0pvxJsAm386fRaPQuDVJzpQ71+TKgl84OlmHfh0t79JHClUun0z/GYrEnATmB4euK1WED7nNJse/L34PUOewnXYFL1MQipivsTQ7w1zGmK5bA5usB43voVPkJG/3OI+uS8ckGwmyatqbf9emcMYop1zov4snpx3bJHueGwBU7iZvvaVQkuWIV+D27GozvJoYtrw9jTykpBrSBrW3bzWFM41aMmMR2hRzIBf4KEot2dnbu9TyPJ4Sc/PG3yMBZU6wNVDOrmK0+W9u56oeP2HwW2kIO5AJflMRqWltbC1brKB6egRiVnZi/ts+285KK5Grc1Hw1Xljc3NxMLjXBpcQTvl5rVA8kUcS6yziCHtrQ6NraWnIJLmVFVVXVtU5cJtOGGAMkR7FJ6ly1nUMfxfU59ZMpTcim86/hUhGcPCsrKwtWbPqiSdZa1Yqhc9X0q606zGf804s08sVwCVaMERsqm1noyUfYhpmnD6t8rKAz99ja6urq+fyYyPU4JeRdOqZv86ivdrviVzzLZS0gtrKyMuOmNMasf0dXz9a2zWJ7rraC6tzWsPN6mFzDJUtscXHxrGKobkuAGIFsMAZ1btvqU60xztXnaoSCHsy1xHAJiC2Nj4+PWLHAfLDZIUavNrJ1mN/NKZLX0WJdFcYhY2NjP0At8VKmu7q6RnA04ftgTlqvEjnYwDBEm2Rn+X8Z20yK1BPbPd2SQ3d39ynApZVy+I/4J/gR5xlsGwU/PXL6qc1/xNkyNTo62uP25q//4dsMd5ebO9di2x9iE9M9WbDccEjR1hXjNQs9KLZ/7svxZOFpgABblQOJiHz7eOkHRXa90NfX967biGf0Iw9HZD8Ac3uNK6GDBboy6rPnGoePGMQqdu43vS8gPVgBXTGW097wZeTFY758+AteRba457innt/tyQf3FidV7GXEJkZym76+8VT7Co7GxydLu7QHGiPyzv5I0T3FhqW8vjGPUtILLw+SX45lZHAiI8mFjJzjxyZIPT5OJeJ44W3y5CE8p/jYCZONXnjDav73TwRhxOjfkR9VlDA/Fe24z1BKjjfIjvtwp+So+RDOfeocHh4+PDs724v30bBPnb3MKfdTp/u4sIlsZPNIflk/Dv8H0I4Y2Oy/OicAAAAASUVORK5CYII=";
  try {
  byte [] encodeByte=Base64.decode(content ,Base64.DEFAULT);
  Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
  return bitmap;
 } catch(Exception e) {
  e.getMessage();
  return null;
}

2。位图转换为字符串

public String BitMapToString(Bitmap bitmap){
 ByteArrayOutputStream baos=new  ByteArrayOutputStream();
 bitmap.compress(Bitmap.CompressFormat.PNG,100, baos);
 byte [] b=baos.toByteArray();
 String temp=Base64.encodeToString(b, Base64.DEFAULT);
 return temp;
}

答案 1 :(得分:-1)

我尝试了网络答案,但是失败了。大多数答案都与此链接
        byte [] bitmapArray = Base64.decode(content.split(“,”)[1],Base64.DEFAULT);         位图= BitmapFactory.decodeByteArray(bitmapArray,0,bitmapArray.length); 它不起作用

答案 2 :(得分:-1)

从Base64中删除此部分:“ data:image / png; base64”