2016-03-19 4 views

Antwort

1

Nun, vor ein paar Tagen machte ich das gleiche. Ich habe html mit Textview mit ImageGetter angezeigt. Aber es gab das vorgeschlagene Ergebnis nicht. Also dann habe ich Jsoup libaray verwendet und ich extrahiere die Elemente und zeige sie in Textvew und wenn img Tag kommt, zeige ich das Bild in Imageview mit UIL universal image libarary. Nun, diese Bibliothek hat die Option, die Bilder zwischenzuspeichern. Sie müssen nur den HTML-String zwischenspeichern und ihn dann zusammen mit dem Layout-Rest, den jsoup und UIL bearbeiten, an die folgende Klasse übergeben.

public class PostContentHandler { 
Context context; 
String content; 
LinearLayout linearLayout; 
public PostContentHandler(Context context, String content , LinearLayout linearLayout){ 
    this.context=context; 
    this.content=content; 
    this.linearLayout=linearLayout; 

} 

public void setContentToView(){ 
    List<String> p = new ArrayList<>(); 
    List<String> src = new ArrayList<>(); 
    Document doc = Jsoup.parse(content); 

    Elements elements = doc.getAllElements(); 

    for(Element element :elements){ 
     Tag tag = element.tag(); 
     if(tag.getName().matches("h[1-6]{1}")){ 
      String heading = element.select(tag.getName().toString()).text(); 
      TextView textView = new TextView(context); 
      textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT)); 
      textView.setTextSize(20); 
      textView.setText(heading); 
      textView.setPadding(5, 0, 5, 0); 
      textView.setTextColor(R.color.black); 
      linearLayout.addView(textView); 
     } 

     if(tag.getName().equalsIgnoreCase("p")){ 
      element.select("img").remove(); 
      String body= element.html(); 
      TextView textView = new TextView(context); 
      textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT)); 
      textView.setText(Html.fromHtml(body)); 
      textView.setTextColor(R.color.black); 
      textView.setPadding(10,0,10,0); 
      linearLayout.addView(textView); 
      p.add(body); 
     } 
     if (tag.getName().equalsIgnoreCase("img")){ 
      String url = element.select("img").attr("src"); 


      final ImageView imageView = new ImageView(context); 
      imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT)); 
      ImageLoader imageLoader = ImageLoader.getInstance(); 
      imageLoader.displayImage(url, imageView, new SimpleImageLoadingListener() { 
       @Override 
       public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { 
        super.onLoadingComplete(imageUri, view, loadedImage); 
        int height=loadedImage.getHeight(); 
        imageView.getLayoutParams().height=getScreenWidth(); 
        imageView.setAdjustViewBounds(true); 
        imageView.requestLayout(); 

       } 

       @Override 
       public void onLoadingStarted(String imageUri, View view) { 
        super.onLoadingStarted(imageUri, view); 
       } 
      }); 

      linearLayout.addView(imageView); 
      src.add(url); 
     } 

    } 
} 
public static int getScreenWidth() { 
    return Resources.getSystem().getDisplayMetrics().widthPixels; 
} 
} 
+0

Darf ich wissen, wie kann ich diese Funktion aufrufen, wie kann ich die HTML-Zeichenfolge in diese Funktion übergeben? Entschuldigung, ich bin neu in der Android-Programmierung. –

+0

Verwenden Sie die Bildansicht, um das Bild anzuzeigen. nicht textview –

+0

Vielen Dank für Ihr Feedback. Schließlich finde ich deinen Code heraus. In der Zeile von imageLoader.displayImage() liegt jedoch ein Syntaxfehler vor. Es kommt heraus Fehler verursacht durch: java.lang.IllegalStateException: ImageLoader muss init mit der Konfiguration vor der Verwendung sein –

0

Hier ist mein Code. Ich versuche es zu debuggen, es zeigt, dass der Code zu onLoadingFailed() geht und nicht zu onLoadingComplete() geht. Ich habe keine Idee warum.

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    linearLayout = (LinearLayout)findViewById(R.id.linearLayout1); 
    DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder() 
      .cacheOnDisc(true).cacheInMemory(true) 
      .imageScaleType(ImageScaleType.EXACTLY) 
      .displayer(new FadeInBitmapDisplayer(100)).build(); 

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
      getApplicationContext()) 
      .defaultDisplayImageOptions(defaultOptions) 
      .memoryCache(new WeakMemoryCache()) 
      .discCacheSize(10 * 1024 * 1024).build(); 

    ImageLoader.getInstance().init(config); 
    setContentToView(); 
} 

public void setContentToView(){ 
    List<String> p = new ArrayList<>(); 
    List<String> src = new ArrayList<>(); 
    Document doc = Jsoup.parse(content); 
    Elements elements = doc.getAllElements(); 

    for(Element element :elements){ 
     Tag tag = element.tag(); 
     if(tag.getName().matches("h[1-6]{1}")){ 
      String heading = element.select(tag.getName().toString()).text(); 
      TextView textView = new TextView(this); 
      textView.setText(Html.fromHtml(heading)); 
      textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT)); 
      linearLayout.addView(textView); 
     } 


     if(tag.getName().equalsIgnoreCase("p")){ 
      element.select("img").remove(); 
      String body= element.html(); 
      TextView textView = new TextView(this); 
      textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT)); 
      textView.setText(Html.fromHtml(body)); 
      this.linearLayout.addView(textView); 
      p.add(body); 
     } 


     if (tag.getName().equalsIgnoreCase("img")){ 
      String url = element.select("img").attr("src"); 
      final ImageView imageView = new ImageView(this); 
      imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
        LinearLayout.LayoutParams.WRAP_CONTENT)); 
      ImageLoader imageLoader = ImageLoader.getInstance(); 
      imageLoader.displayImage(url, imageView, new SimpleImageLoadingListener() { 
       @Override 
       public void onLoadingStarted(String imageUri, View view) { 
        super.onLoadingStarted(imageUri, view); 

       } 

       @Override 
       public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { 
        super.onLoadingComplete(imageUri, view, loadedImage); 
        int height=loadedImage.getHeight(); 
        imageView.getLayoutParams().height=getScreenWidth(); 
        imageView.setAdjustViewBounds(true); 
        imageView.requestLayout(); 
       } 

       @Override 
       public void onLoadingCancelled(String imageUri, View view) { 
        super.onLoadingCancelled(imageUri, view); 
       } 

       @Override 
       public void onLoadingFailed(String imageUri, View view, FailReason failReason) { 
        super.onLoadingFailed(imageUri, view, failReason); 
       } 
      }); 

      linearLayout.addView(imageView); 
      src.add(url); 
     } 
    } 
} 

public static int getScreenWidth() { 
    return Resources.getSystem().getDisplayMetrics().widthPixels; 
} 
+0

@Zeeshan Shabbir Hier ist mein Code –