Ankush
Ankush

Reputation: 197

Issue in loading image from web

I am developing an android application in which I am loading an image from the server but when I am doing so there is no image coming(displaying that portion blank) in android app. Can anybody tell me what is the wrong am I doing? Here is the code which I am implementing :

public class ad extends Activity {

ImageView image_view;
final static String imageLocation="http://example.com/ads/banner320.png"; 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);      
    setContentView(R.layout.select);    

    image_view = (ImageView)findViewById(R.id.imageView1);
    loadImage(imageLocation);        

}
Bitmap bitmap;
void loadImage(String image_location){

    URL imageURL = null;

    try {
     imageURL = new URL(image_location);
     } 

    catch (MalformedURLException e) {
        e.printStackTrace();
     }

    try {
     HttpURLConnection connection= (HttpURLConnection)imageURL.openConnection();
     connection.setDoInput(true);
     connection.connect();
        InputStream inputStream = connection.getInputStream();

        bitmap = BitmapFactory.decodeStream(inputStream);//Convert to bitmap
        image_view.setImageBitmap(bitmap);
    }
    catch (IOException e) {

         e.printStackTrace();
    }
}

}

Please help by suggesting some changes in this code or mistakes what I am doing?

Upvotes: 0

Views: 73

Answers (3)

The Ray of Hope
The Ray of Hope

Reputation: 738

use that code

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class LoadImageActivity extends Activity {

 ImageView image_view;
 Button btnLoadImg ;
    final static String imageLocation="http://www.codeincloud.tk/play.png"; //Use any image location. 

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        image_view = (ImageView)findViewById(R.id.imageview);
        btnLoadImg = (Button)findViewById(R.id.btn_imgload);

        btnLoadImg.setOnClickListener(loadImage);
    }


    View.OnClickListener loadImage = new View.OnClickListener(){
     public void onClick(View view) {
         loadImage(imageLocation);
            }
     };

     Bitmap bitmap;
    void loadImage(String image_location){

          URL imageURL = null;

          try {
           imageURL = new URL(image_location);
           } 

          catch (MalformedURLException e) {
              e.printStackTrace();
           }

          try {
           HttpURLConnection connection= (HttpURLConnection)imageURL.openConnection();
           connection.setDoInput(true);
           connection.connect();
              InputStream inputStream = connection.getInputStream();

              bitmap = BitmapFactory.decodeStream(inputStream);//Convert to bitmap
              image_view.setImageBitmap(bitmap);
          }
          catch (IOException e) {

               e.printStackTrace();
          }
    }
  }

write in manifest file <uses-permission android:name="android.permission.INTERNET"/>

Upvotes: 0

Niranj Patel
Niranj Patel

Reputation: 33248

Bitmaps take up a lot of memory, so everytime download same image is not good idea so better to use cache functionality for this ..

If you need to download same image manytime so better to save that image in cache and use it later..

here is best sample example.

Upvotes: 1

Hariharan
Hariharan

Reputation: 24853

Try this..

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);      
    setContentView(R.layout.select);    

    image_view = (ImageView)findViewById(R.id.imageView1);
    new MyClass().execute(imageLocation);     

}

and

class MyClass extends AsyncTask<String, Void, Bitmap> {

    private Exception exception;

    protected Bitmap doInBackground(String... imageLocation) {
        URL imageURL = null;

    Bitmap bitmap = null;
    try {
        imageURL = new URL(utl2);
    }

    catch (MalformedURLException e) {
        e.printStackTrace();
    }

    try {
        HttpURLConnection connection = (HttpURLConnection) imageURL
                .openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream inputStream = connection.getInputStream();

        bitmap = BitmapFactory.decodeStream(inputStream);

        Log.v("bitmap--", "" + bitmap);

    } catch (IOException e) {
        e.printStackTrace();
    }

    return bitmap;
    }

    protected void onPostExecute(Bitmap bitmap) {
        // TODO: check this.exception 
        // TODO: do something with the feed

           image_view.setImageBitmap(bitmap);

}
 }

Upvotes: 0

Related Questions