android - Can't display an image from a url -


i'm pretty new programming , experience light c# , javascript.

i want make simple android app display picture , data net i'm having trouble displaying image(haven't tried else yet). i've searched , web code examples can't them work.

here's androidmanifest.xml

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="my.test"     android:versioncode="1"     android:versionname="1.0" >      <uses-sdk android:minsdkversion="15" />     <uses-permission android:name="android.permission.internet" />      <application         android:icon="@drawable/ic_launcher"         android:label="@string/app_name" >         <activity             android:name=".testactivity"             android:label="@string/app_name" >             <intent-filter>                 <action android:name="android.intent.action.main" />                  <category android:name="android.intent.category.launcher" />             </intent-filter>         </activity>     </application>  </manifest> 

and main.xml

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" >  <textview     android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/hello" />    <imageview     android:id="@+id/imageview1"     android:layout_width="match_parent"     android:layout_height="match_parent" />  </linearlayout> 

and activity

package my.test;  import java.io.inputstream; import java.net.url;  import android.app.activity; import android.graphics.drawable.drawable; import android.os.bundle; import android.widget.imageview;  public class testactivity extends activity { /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.main);     imageview imgview =(imageview)findviewbyid(r.id.imageview1);     drawable drawable = loadimagefromweboperations("http://www.androidpeople.com/wp-content/uploads/2010/03/android.png");     imgview.setimagedrawable(drawable);     }     private drawable loadimagefromweboperations(string url)     {         try         {             inputstream = (inputstream) new url(url).getcontent();             drawable d = drawable.createfromstream(is, "src name");             return d;         }catch (exception e) {             system.out.println("exc="+e);             return null;         }     }  } 

i've tried other examples both using avd , phone same result: blank screen. doing wrong?

public bitmap fetchimage(url url) {     try {         urlconnection conn = url.openconnection();         conn.connect();         final bufferedinputstream bis = new bufferedinputstream(conn.getinputstream());         final bitmap bm = bitmapfactory.decodestream(bis);         bis.close();         return bm;     } catch (ioexception e) {}     return null; } 

now have use setimagebitmap method imageview show image. make sure downloading part in asynctask.

below snippet you

downloadhelper.java

public interface downloadhelper {     public void onsucess(bitmap bitmap);     public void onfailure(string response); } 

mainactivity.java

public class galleryexample extends activity {     /** called when activity first created. */     @override     public void oncreate(bundle icicle) {         super.oncreate(icicle);         setcontentview(r.layout.main);          downloadhelper downloadhelper = new downloadhelper()         {             @override             public void onsucess(bitmap bitmap)             {                 imageview imageview=(imageview)findviewbyid(r.id.imageview);                 imageview.setimagebitmap(bitmap);             }              @override             public void onfailure(string response)             {                 toast.maketext(context, response, toast.length_long).show();             }         };         new mytask(this,downloadhelper).execute("image url");     } 

mytask.java

public class downloadtask extends asynctask<string, integer, object> {     private context context;     private downloadhelper downloadhelper;     private progressdialog dialog;       public downloadtask(context context,downloadhelper downloadhelper)     {         this.context = context;      }      @override     protected void onpreexecute()     {         dialog = new progressdialog(context);         dialog.settitle("please wait");         dialog.setmessage("fetching data!!");         dialog.setcancelable(false);         dialog.show();         super.onpreexecute();     }      @override     protected object doinbackground(string... params)     {         url aurl = new url(myremoteimages[position]);         urlconnection conn = aurl.openconnection();         conn.connect();         inputstream = conn.getinputstream();          bufferedinputstream bis = new bufferedinputstream(is);         /* decode url-data bitmap. */         bitmap bm = bitmapfactory.decodestream(bis);         bis.close();         is.close();         return bm;     }      @override     protected void onpostexecute(object result)     {         dialog.dismiss();         if (result != null)         {             downloadhelper.onsucess((bitmap)result);         }          else         {             downloadhelper.onfailure("error in downloading data!!");         }         super.onpostexecute(result);     } } 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -