PHP png base64编码 - > Android base64解码

时间:2015-11-28 22:27:27

标签: php android google-maps-markers

我有一台服务器将png base64编码的图像发送到Android应用程序,并将其显示为GoogleMap标记。

我确认如果我使用drawable一切正常,那么这里的问题来自于从服务器接收的数据创建的位图。

以下是PHP代码,它读取png文件对图像数据进行编码:

if (($imgdata = file_get_contents($fname)) !== false) {
    $icon = base64_encode($imgdata);
}

在android上收到的字符串就是那个:

iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAYAAADimHc4AAAEKUlEQVR4nO3bPWgcRxQH8GusgBEqDgyp5CKq3JkYggsHQQKbeaMj1UGqQEgRCKiw3ViF4XBxkeeNrhCIJBAMgUDApJAakcIgNG+0HFgEF6fKLhKMERhMEMZgubkUtxaJoqDd2zeavc37weuO2zf/ud3bj9lGQwghhBBCCCGEEEIIIYQQQgghhBBCCCHE/8uK++QjJPgBCQ6RYGic2jBeX4vdV+11dlvn0UEXCV4hwfBYPUcPS53d1vnYfdYWelg6Ifh/lCV1M3aftXTXw5X/+OX/a0+wXqnY/dbKal/NGNIPcoSf7QV6sNpXM7H7rg3j9GLe8I/KQTd237Ww7FuzSPC08AQQHKxsq8ux+594Y/36jw5F8E3s/iceEjwcdwKQ1LO1rfnp2GOYWMbra0j65fgTAENL+uvj37vaVzPoYQk9rFvSA/Swbp26FGOMlYZO3ykTPhIMDekHb7+vlyZN9Oo2EuzL6espVjfVO0jQLzsBSHCw7FuzZgveNU5tnPLZJ8u+NRt77JVgnbrEEP7RnzES/Jjr807fiT32SjCk21wTUKg8/BZ77JVgCe5HmQCCN0jwRezxR2dJDyJNwBAJNr/fff9c7AzOBBJ8YB1ct16pzl57qtFoNNa25qdxvKtftqr984Vemsxlh5k//zbwR0hwr8zVL2PdiJ1RMGtb89ORDzGnliU9eLtH1g7HBdbZlP44dlbscGfhKmbPciegvoudFzvj9E8VCDZv/d5Lk7nYmbHppUkTCZ5XINgi1YmdG5tsRUPsQIvW69rcKcW892OqVh5+iZ1daV2nLuDJt4EnoPRLQ7odO8NS0MGn8YMsVft3PVyJnePY0OsvKxBi2dqc2Isz9LBegQBLl/FwK3aWhXX22lNI8CRYME6l2RlWBwk66KCLTqWBtvfCOp3EzrSQXprMhQlDPTNOL5608qGXJk1D6nN0+nGAbf9hd+BijCzHkt1+YA8hz5/iyra6jAR77NufpFNT9PAVcwD7Zke9l3f72fsE3PefDq2D6yFzY4OkfuYc/Dh/hNmDee69cB93Fq6GyIwVEji2QTuV9tKkWbSH7FB0EOBQtB4iMzZ2Gz7E0YNurgEvjduLIW0D7AWv0OnPODNjZUnd5Bps2XX/SAvzGOZZRL/r1AXO3Fh09tpT6GGba6DGqY2yPXH2w7VnBpOdfbAN0ji9WLYn4+DbIBNAcFi529Y51mIWmwCGpSLWKxVoAoaGtOXIjcXopbpyS8uPF8fjwexlvyATgKNHmIXP0IJAgnvcA+SYgHC3RUZViYuz0YsVuV4pLfTr4lguGHoCKnFdYDzcYh+Y0485egs+AQSvOfoshfvYn9VDjt7OYA+Iv7wdSf3KPTBLcJ+jt16aNEM9K7CkB5U7FRVCCCGEEEIIIYQQQgghhBBCCCGEEEII0Wj8BW3/3m0HjJ8xAAAAAElFTkSuQmCC

在此成功解码http://www.freeformatter.com/base64-encoder.html

这是解码图像数据的android端:

private Bitmap getCatBitmap() {

    byte[] decodedByte = null;

    try {
        decodedByte = Base64.decode(catB64Bitmap, Base64.DEFAULT);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    }
    return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}


private BitmapDescriptor getIconMapFromCategory(int category) {

    Bitmap bm;

    BitmapDescriptor BMDesc;

    int mode = 1;

    if (mode == 0) {
        // works
        bm = drawableToBitmap(mActivity.getResources().getDrawable(R.drawable.logo_gm));

        BMDesc =  BitmapDescriptorFactory.fromBitmap(bm);

    } else {
        // Cause cluster to crash
        bm = site.getSiteCategoriesList().get(category).getCatBitmap();
        BMDesc = BitmapDescriptorFactory.fromBitmap(bm);
    }

    return BMDesc;
}

所以我执行这段代码:

markerOptions.position(sitePos)
.title(cursor.getString(cursor.getColumnIndex(SitesDBOpenHelper.KEY_NAME)))
.snippet(categoryName)
.icon(getIconMapFromCategory(category));

site.getMarkersList().put(Long.parseLong(cursor.getString(cursor.getColumnIndex(SitesDBOpenHelper.KEY_REMOTE_ID))),
        SiteMarker.getSiteMarkerInfos(
                Long.parseLong(cursor.getString(cursor.getColumnIndex(SitesDBOpenHelper.KEY_REMOTE_ID))),
                cursor.getString(cursor.getColumnIndex(SitesDBOpenHelper.KEY_TYPE)),
                markerOptions));

mClusterManager.addItem(site.getMarkersList().get(key));

mClusterManager.cluster();

此代码在渲染标记时崩溃。如果我用drawable中的位图替换base64编码图像,则会正确呈现标记。

你能告诉我这里有什么问题吗?

编辑:添加崩溃报告:并修改getIconMapFromCategory()以更好地解释导致群集崩溃的情况。

11-29 10:41:01.959 6283-6283/com.ndguide.ndguide E/AndroidRuntime: FATAL EXCEPTION: main
   Process: com.ndguide.ndguide, PID: 6283
   java.lang.NullPointerException
   com.google.a.a.ae.a(Unknown Source)
   com.google.maps.api.android.lib6.d.dn.<init>(Unknown Source)
   com.google.maps.api.android.lib6.d.dm.a(Unknown Source)
   com.google.maps.api.android.lib6.d.ag.<init>(Unknown Source)
   com.google.maps.api.android.lib6.d.eu.a(Unknown Source)
   com.google.android.gms.maps.internal.j.onTransact(SourceFile:167)
   android.os.Binder.transact(Binder.java:387)
   com.google.android.gms.maps.internal.IGoogleMapDelegate$zza$zza.addMarker(Unknown Source)
   com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)
   com.google.maps.android.MarkerManager$Collection.addMarker(MarkerManager.java:154)
   com.google.maps.android.clustering.view.DefaultClusterRenderer$CreateMarkerTask.perform(DefaultClusterRenderer.java:816)
   com.google.maps.android.clustering.view.DefaultClusterRenderer$CreateMarkerTask.access$2000(DefaultClusterRenderer.java:785)
   com.google.maps.android.clustering.view.DefaultClusterRenderer$MarkerModifier.performNextTask(DefaultClusterRenderer.java:624)
                                                                   at com.google.maps.android.clustering.view.DefaultClusterRenderer$MarkerModifier.handleMessage(DefaultClusterRenderer.java:596)
   android.os.Handler.dispatchMessage(Handler.java:102)
   android.os.Looper.loop(Looper.java:148)
   android.app.ActivityThread.main(ActivityThread.java:5417)
   java.lang.reflect.Method.invoke(Native Method)
   com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

当DefautClusterRender.class执行时发生崩溃:

markerWithPosition2 = DefaultClusterRenderer.this.mClusterManager.getMarkerCollection().addMarker(markerOptions1);

但奇怪的是,查看崩溃报告时,Binder.java:387不存在,当我双击它时,我不会进行一行代码和SourceFile:167提出2个java文件但是167行没有也不存在。 我可以在googleplayservices_lib上结束我使用的问题吗?

0 个答案:

没有答案