English 中文(简体)
并查看图像 blob html 和roid 的图像 blob html 。
原标题:store and view image blob html.fromhtml android

I am trying to show a image saved as blob in my DB. I am using ORMLite and Android 1.6. I created a textView who show a html.FromHtml

这是代码:

//Save the image in DB.
    ...
    Bitmap myBitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().toString()+"/images/imagem.png");
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    myBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();
    glossary.setImageBytes(byteArray);

词汇表是词汇表的例子。 谁拥有字段图像Bytes :

@DatabaseField(dataType = DataType.BYTE_ARRAY)  
private byte[] imageBytes;  

在我的活动中, 我将图像Bytes 传送到字符串, 以便使用 html. FromHtml :

//Activity
...
String htmlContent = "<h1> Title </h1> 
                      <img src=""+glossary.getImageBytes.toString()+"" />"

现在在我的适应器中,我想展示 htmlContent 的内容:

//Adapter
...
holder.tvContent.setText(Html.fromHtml(htmlContent, new Html.ImageGetter() {
            @Override
            public Drawable getDrawable(String source) {  
                    byte[] data;
                    data = source.getBytes();
                    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                    //Log.i("bitmap", "bitmap height:" + String.valueOf(bitmap.getHeight) );
                    //THIS LOG RETURN NPE
                    Drawable d = null;  
                    d = new BitmapDrawable(getResources(),bitmap);
                    d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
                    return d;   
                }
            }, null));
...

我认为问题在于当我将字节修改为字符串和字符串到字节时。 因为 Bitmap Factory. decodeFile () 所创建的位图正在返回 NPE 。

但我不知道如何解决这个问题。

Any suggestions? thx!

问题回答

嗯,当然,你的第一个问题就是你不应该这样做:

glossary.getImageBytes().toString()

要将 byte[] 变成 string,你应该做到:

new String(glossary.getImageBytes())

如果您做一个 byte[ ].to String () 它将被设置为类似 :

[B@3487a5cc

而不是由数组中的字节组成的 string

虽然如此, 我仍不确定您能否将 < code> pNG 文件中的字节放入一个 < code> byte < < 数组, 打印成 < code> string 。 这听起来像是在您的 HTML 中打印二进制信息, 我认为这不会正确转换。 另外, 您似乎正在将 < code> PNG 字节放入 < code> byte > [ > < < /code> 数组, > HTML 区域。 这在 AFAIK 中都行不通 。





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签