2017-09-12 5 views
1

Ich versuche mit diesem Code und es funktioniert nicht. Wie kann ich dieses Bild herunterladen und anzeigen? Wie man ein php Bild in der Bildansicht zeigt?

mein Java-Code

private ImageView iv; 
private Bitmap bitmap; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_showprofile); 

    iv = (ImageView) findViewById(R.id.virus); 
    bitmap = getBitmapFromURL("http://getbloodbd.org/image.php?no=4"); 
    iv.setImageBitmap(bitmap);} 

public Bitmap getBitmapFromURL(String src) { 
    try { 
     URL url = new URL(src); 
     HttpURLConnection connection = (HttpURLConnection) url 
       .openConnection(); 
     connection.setDoInput(true); 
     connection.connect(); 
     InputStream input = connection.getInputStream(); 
     Bitmap myBitmap = BitmapFactory.decodeStream(input); 
     return myBitmap; 

    } catch (Exception e) { 
     // TODO: handle exception 
     e.printStackTrace(); 
     return null; 
    } 
} 

und mein xml-Code ist

<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="org.getbloodbd.getbloodbd.ShowprofileActivity" 
    tools:showIn="@layout/app_bar_showprofile"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <ImageView 
      android:id="@+id/virus" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentEnd="true" 
      android:layout_alignParentStart="true" /> 

    </RelativeLayout> 

und mein PHP-Code ist und in webview Methode ist es gut funktionieren, aber die Bildansicht nicht funktioniert, aber Ich brauche Bildansichtsmethode.

<?php include 'config.php';header("Content-type: image/gif");$no=$_GET['no']; $dn = mysql_query('select image from images where no="'.$no.'"'); $dnn = mysql_fetch_array($dn); $data = $dnn['image'];echo base64_decode($data);?> 

Wie löst man es?

+0

geben Sie die vollständige URL –

Antwort

0

Keine Notwendigkeit, Code für das Laden von Bildern zu tun. Es gibt eine berühmte Bibliothek für das Laden von Bildern für Android-Anwendungen, Picasso wird sich um die Wiederholung von Anrufen kümmern, auch um das Bild im App-Cache zu speichern. Also, das gleiche Bild wird nicht jedes Mal heruntergeladen.

Abhängigkeit für picasso ist

compile 'com.squareup.picasso:picasso:2.5.2' 

Picasso.with(context).load(your_image_url).into(imageView); 
1

können Sie das Bild aus der URL wie laden unter

URL url = new URL("image full path"); 
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream()); 
imageView.setImageBitmap(bmp); 

eine andere Art und Weise

private Bitmap bmp; 

    new AsyncTask<Void, Void, Void>() {     
     @Override 
     protected Void doInBackground(Void... params) { 
      try { 
       InputStream in = new URL(IMAGE_URL).openStream(); 
       bmp = BitmapFactory.decodeStream(in); 
      } catch (Exception e) { 
       // log error 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      if (bmp != null) 
       imageView.setImageBitmap(bmp); 
     } 

    }.execute(); 

Bitte reference hier um das Bild zu laden. Bitte versuchen Sie es mit

0

Folgen Sie diesem Link Click Here.

Stellen Sie sicher, dass in Ihrer AndroidManifest.xml die folgenden Berechtigungen für den Zugriff auf das Internet festgelegt sind.

<uses-permission android:name="android.permission.INTERNET" /> 
+0

Erlaubnis gebend getan –

+0

Ok ... Link oben Coole folgen. –

0

Sie sollten Bibliothek Glide verwenden: https://github.com/bumptech/glide

Import

compile 'com.github.bumptech.glide:glide:4.1.1' 
annotationProcessor 'com.github.bumptech.glide:compiler:4.1.1' 

Verwendung:

Glide.with(this).load("http://getbloodbd.org/image.php?no=4").into(iv); 

Oder wenn Sie wollen Bitmap:

Glide.with(this).asBitmap().load("http://getbloodbd.org/image.php?no=4").listener(new RequestListener<Bitmap>() { 
     @Override 
     public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) { 
      return false; 
     } 

     @Override 
     public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) { 
      iv.setImageBitmap(resource); 
      return false; 
     } 
    }); 
0

Yo Picasso oder Glide für das Erhalten Bild vom Server und lädt in Image mit vielen Option verwenden kann.

  • Beispiel für Picasso

    Picasso.with(context) 
    .load(url) 
    .centerCrop() 
    .placeholder(R.drawable.user_placeholder) 
    .error(R.drawable.user_placeholder_error) 
    .into(imageView); 
    
  • Beispiel für Glide

    GlideApp 
    .with(context) 
    .load(url) 
    .centerCrop() 
    .placeholder(R.drawable.loading_spinner) 
    .into(imageView); 
    

vergessen Sie nicht, Internet-Erlaubnis in AndroidManifest.xml Datei hinzuzufügen.

<uses-permission android:name="android.permission.INTERNET" /> 
Verwandte Themen