0

I have implemented an ImageGetter within an Html.fromHtml method by reading the following posts: Html.fromHtml problem, and Html.ImageGetter. Here is the code with the ImageGetter:

if (mItem != null) {
        ((TextView) rootView.findViewById(R.id.exercise_detail))
                .setText(Html.fromHtml(mItem.description, new ImageGetter(){ 

                @Override public Drawable getDrawable(String source) {
                      Drawable drawFromPath;
                      int path =
                            ExerciseDetailFragment.this.getResources().getIdentifier(source, "drawable",
                                    "com.package.ChaitanyaVarier.mytrainer2go");
                      drawFromPath = (Drawable) ExerciseDetailFragment.this.getResources().getDrawable(path);
                      drawFromPath.setBounds(0, 0, drawFromPath.getIntrinsicWidth(),
                         drawFromPath.getIntrinsicHeight());
                      return drawFromPath;
                   }
                }, null));

    }

Here is the HTML within a string called description (inside another activity file) that becomes the path:

<img src =\"file:///android_asset/img/situp1.png\">

I have put an image named situp1.png inside a folder named img which is inside the assets folder.

The problem is that the path within my HTML is not getting parsed properly, and the app crashes when I try to load the item containing the image. I know for a fact that the string within the img tag is getting recognized, and I have verified this through debugging.

Community
  • 1
  • 1
Cvarier
  • 161
  • 13

1 Answers1

0

The way I solved this was to change

<img src =\"file:///android_asset/img/situp1.png\"> 

to

<img src=\"situp1\"> 

as it was already in the resource folder and hence a drawable.

I also had to change

ExerciseDetailFragment.this.getResources().getIdentifier(source, "drawable", "com.package.ChaitanyaVarier.mytrainer2go"); 

to

ExerciseDetailFragment.this.getResources().getIdentifier(source, "drawable", "com.ChaitanyaVarier.mytrainer2go");

A rather silly problem I had, just had to understand the code a little better.

Cvarier
  • 161
  • 13