Retrieve an image from internet...

           In this application,by pressing a button we can retrieve a image from internet by giving a link to the application code.For this we need a xml layout containing a button and a imageview widget.When the button is pressed,the image data is retrieved and showed in the imageview widget.Here is the xml code of the layout:
<?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"
    />
<Button android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Connect"></Button>

<ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
</LinearLayout>
      Now we will use three java class for this.In the first one named ImageReadMain ,we initialize the button and the imageview and define the onClick method of the button.In the second java class connection1 the code for connection is written in setNetwork() method.In the last one named Data class,we simply write a static bitmap variable,that we will use in previous two classes.

Now look at the ImageReadMain class...


package Image.Read.a;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;



public class ImageReadMain extends Activity 
{
   //Reference www.androidcookers.co.cc
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button btn=(Button)findViewById(R.id.Button01);
        final ImageView imgVi=(ImageView)findViewById(R.id.ImageView01);
        
        btn.setOnClickListener(new OnClickListener()
        {
                public void onClick(View v) 
                 {    
                    Connecetion1 con=new Connecetion1();
                    con.setNetwork();
                    imgVi.setImageBitmap(Data.StoreImg);
                                      
                 }
         });
                
    }
}


Here the retrieved image stored in Data.StoreImg is shown in the imageview.Now the connection1 class that is used in ImageReadMain class.

package Image.Read.a;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.graphics.BitmapFactory;

public class Connecetion1 
{
     public void setNetwork()
     {
        try
        {

            URL url = new URL("http://3.bp.blogspot.com/_9UYLMDqrnnE/S4UgSrTt8LI/AAAAAAAADxI/drlWsmQ8HW0/s400/sachin_tendulkar_double_century.jpg");

            URLConnection connection=url.openConnection();
           
            HttpURLConnection HCon=(HttpURLConnection)connection;
            
            int ResCode=HCon.getResponseCode();

            System.out.println("Responce Code is = "+ResCode);
            
            
            if(ResCode==HttpURLConnection.HTTP_OK)
            {
                
            InputStream ins=((URLConnection)HCon).getInputStream();    
                
                  Data.StoreImg=BitmapFactory.decodeStream(ins);
            
            
            }
  
        }
        catch (MalformedURLException e) 
            {
            
            e.printStackTrace();
        } catch (IOException e) 
            {
        
              e.printStackTrace();
             }
        
     }

}

Here the image data is retrieved by ((URLConnection)HCon).getInputStream().This is stored in Data.StoreImg after converting to bitmap image via BitmapFactory.decodeStream(ins).

Now the code of Data class:
package Image.Read.a;

import android.graphics.Bitmap;

public class Data
{
    public static Bitmap StoreImg;

}


Now the important thing is ,we have to add the user permission internet to the Android manifest by opening the permission tab before running the application.
 
After running the image can be show in imageview widget.

10 comments:

  1. Hello this is sοmewhat of off toρic but I was wondeгing if blogs usе WҮЅIWYG еditors or
    if уou have to mаnually cоde ωith HTML.
    ӏ'm starting a blog soon but have no coding experience so I wanted to get advice from someone with experience. Any help would be greatly appreciated!

    my web page: Lower price Brand new Orleans Resorts

    ReplyDelete
  2. Hello, Neаt post. Τhere's a problem along with your web site in web explorer, may test this? IE nonetheless is the market chief and a good element of folks will leave out your wonderful writing due to this problem.

    Also visit my web page - Thailand Phuket Resorts

    ReplyDelete
  3. Attractіvе poгtiоn of content. І simρly stumblеd
    upon youг web sitе аnd іn aсcession capitаl to
    say that I aсquіrе іn fаct enjoуeԁ aсcount your blοg
    pοѕts. Anywаy I ωіll
    bе subscribіng to уour augment and еνen I
    ѕuccеѕs yоu gеt right of entry tο constantly fast.



    Look аt my pаgе; The capital could be a great place to visit. Rome Resorts

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. You need tо taκe ρart in a cοnteѕt
    foг one οf the most useful webѕіteѕ
    online. I mοѕt certainly will recommend
    this sіte!

    Feel free to vіsit mу weblog Rome could be a good spot to go to. Ancient rome Motels

    ReplyDelete
  6. What's up colleagues, pleasant post and nice arguments commented here, I am genuinely enjoying by these.

    Feel free to surf to my website ... seapp.ihandssocial.com

    ReplyDelete
  7. Whаt a materіal οf un-ambiguіty and ρreѕeгveness οf precіоus knowledge on the topiс of unprеdictеd feelіngѕ.


    Tаkе а lоok аt my wеbpage
    - Lodges at TravelSupermarket | Evaluate and e-book low-priced inns on-line - TravelSupermarket.com

    ReplyDelete
  8. Its liκe you rеаd my mind! You seem tο knοw a lоt
    about this, likе you wгote the book in іt or sоmething.

    ӏ think that you could do ωіth some pics tо drive thе message home
    a bit, but other than that, this is gгeat blog.
    A fantastic read. I will defіnіtely be back.


    Also visit my webpage - just click the up coming page

    ReplyDelete
  9. Hmm it appears like your site ate my first comment (it wаs ехtгеmеly long) sо I
    guess I'll just sum it up what I submitted and say, I'm thorοughly еnϳoуing your
    blog. I toο аm an aspiring blog blogger but I'm still new to everything. Do you have any suggestions for first-time blog writers? I'd definitely apρrеciate іt.


    my site ... krefeldtreffpunkt.de

    ReplyDelete
  10. Hi there mates, its wonderful piece of writing
    concerning tutoringand entirely explained, keep it up all the time.


    Also visit my page; seo training for beginners

    ReplyDelete