首页 文章

在android base64中获取null解码的位图图像

提问于
浏览
1

我正在使用base64解码编码的图像字符串,但它总是返回null .

我使用下面的代码编码字符串并将其存储在类文件中 -

Bitmap icon = BitmapFactory.decodeResource(getActivity().getResources(),R.drawable.img);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.PNG, 100, bao);
byte [] ba = bao.toByteArray();
String ba1=Base64.encodeToString(ba,Base64.DEFAULT);

ba1存储在java文件中并在我的活动中访问它以进行解码,如 -

MyStrings mMyString=new MyStrings();
String imageString=mMyString.image1;
Bitmap bm=decodeBase64(imageString);

但是我总是把bm称为null . 是否有任何解决方案来获取图像位图?

1 回答

  • 0

    以这种方式使用

    Bitmap icon = BitmapFactory.decodeResource(getActivity().getResources(),R.drawable.img);
     ByteArrayOutputStream bao = new ByteArrayOutputStream();
     icon.compress(Bitmap.CompressFormat.PNG, 100, bao);
     byte[] byteArray = bao.toByteArray();
     String ba1= Base64.encodeToString(byteArray, Base64.NO_WRAP);
    

相关问题