如何转换base64数据:url链接url?

时间:2016-10-19 02:31:11

标签: java android

示例我有url:data:image / png; base64,ajshgvdkau ....

我想将其转换为:http://example.com/pic.png

有没有在java android中执行此操作的代码?

==================================

编辑1

我试图将图片放入网址并将其显示为textview。当我使用案例2时,图像显示完美,

但是在案例1中没有。有错误"未知协议:数据"和" W / AwContents:nativeOnDraw失败;清除背景颜色"

        //case 1
//          String base_url = "<p>Image 1 : <img src=\"data:image/jpeg;base64,/9j/4AAQSk...

        //case 2
        String base_url = "<p>Image 1 : <img src=\"http://example.com/android/tryout/logo.png\"></img></p>";

        Spanned span2 = Html.fromHtml(base_url,getImageHTML(),null);

        TextView tv = (TextView)findViewById(R.id.target);
        tv.setText(span2);

这是我的功能

public Html.ImageGetter getImageHTML() {

    Html.ImageGetter imageGetter = new Html.ImageGetter() {
        public Drawable getDrawable(String source) {
            try {
                Drawable drawable = Drawable.createFromStream(new URL(source).openStream(), "src");
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());

                return drawable;
            } catch(IOException exception) {
                Log.v("IOException", exception.getMessage());
                return null;
            }
        }
    };

    return imageGetter;
}

感谢您的回答。

2 个答案:

答案 0 :(得分:1)

您可以使用Base64类将编码的字符串解码为字节数组https://developer.android.com/reference/android/util/Base64.html

byte[] decodedString = Base64.decode(encodedUrl, Base64.DEFAULT);

然后你可以从字节数组创建位图。

Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 

请确保在解码网址时删除元信息(即数据:image / png; base64)。 更新: 这里的示例基于您的代码:

public Html.ImageGetter getImageHTML() {

    Html.ImageGetter imageGetter = new Html.ImageGetter() {
        public Drawable getDrawable(String source) {
            try {
                String base64Image = source.substring("data:image/jpeg;base64,".length);
                byte[] decodedString = Base64.decode(base64Image, Base64.DEFAULT);
                Bitmap bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
                Drawable drawable = new BitmapDrawable(getActivity().getResources()/*or other way to get resource reference*/, bitmap);
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());

                return drawable;
            } catch(IOException exception) {
                Log.v("IOException", exception.getMessage());
                return null;
            }
        }
    };

    return imageGetter;
}

答案 1 :(得分:0)

我已经解决了这个问题。我正在使用Base64InputStream。首先删除“data:image / jpeg; base64”以获取有效的base64代码。

 public Html.ImageGetter getImageHTML() {

    Html.ImageGetter imageGetter = new Html.ImageGetter() {
        public Drawable getDrawable(String source) {
            try {

                String[] str = source.split("base64");

                Base64InputStream is = new Base64InputStream(new ByteArrayInputStream(str[1].getBytes()), 0);

                Bitmap decodedByte = BitmapFactory.decodeStream(is);

                Drawable drawable = new BitmapDrawable(getResources(), decodedByte);

                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

                return drawable;
            }catch(Exception e){
                e.getMessage();
                return null;
            }
        }
    };

    return imageGetter;
}