0

i have put image view layout in xml layout file

<ImageView 
  android:id="@+id/imageData"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginLeft="10sp"
  android:layout_marginTop="10sp"
/>

and setting the same in my code as..

ImageView imageData = (ImageView) findViewById(R.id.imageview);

now this imageData should hold the image when the activity launch..this is my code…

public class TestImageClass extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.imagetable);
        ImageView mChart = (ImageView) findViewById(R.id.imageview);
        String testurl = "http://....some image name....jpg";
        mChart.setTag(testurl);
        new DownloadImagesTask().execute(mChart);


    }   
    public class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> {

        ImageView imageView = null;

        @Override
        protected Bitmap doInBackground(ImageView... imageViews) {
            this.imageView = imageViews[0];
            return download_Image((String)imageView.getTag());
        }

        @Override
        protected void onPostExecute(Bitmap result) {
            imageView.setImageBitmap(result);
        }


        private Bitmap download_Image(String url) {

            Bitmap bm = null;
            try {
                Log.e("inside", "download image.....");
                URL aURL = new URL(url);
                URLConnection conn = aURL.openConnection();
                conn.connect();
                InputStream is = conn.getInputStream();
                BufferedInputStream bis = new BufferedInputStream(is);
                bm = BitmapFactory.decodeStream(bis);
                bis.close();
                is.close();
                Log.e("going out of", "download image.....");
            } catch (IOException e) {
                Log.e("Hub","Error getting the image from server : " + e.getMessage().toString());
            } 

            return bm;          
}
}
}

so now how to set this bitmap object "bm" to my ImageView?

mak
  • 57
  • 4
  • 11
  • I use Prime for all of my image loading, it would handle this easily. https://github.com/DHuckaby/Prime – HandlerExploit May 11 '12 at 11:57
  • If you have url as String, then you can check this http://stackoverflow.com/questions/2471935/how-to-load-an-imageview-by-url-in-android – Ponmalar May 11 '12 at 11:59

1 Answers1

0

Try this,

Drawable d;
        try {
            InputStream is = (InputStream) new URL("http://trivetts.iconnectgroup.com/Uploads/Pictures/noname.jpg").getContent();
            d = Drawable.createFromStream(is, "src name");
            imageView.setBackgroundDrawable(d);
        } catch (Exception e) {

        }
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
  • thanks for the reply but the image view is still blank.do i need to do any change to the xml file? – mak May 11 '12 at 11:55
  • are you running into any exception? did you check the logcat if any exception is being catched? . But still try providing the internet permission in your manifest, – Andro Selva May 11 '12 at 11:58
  • 1
    no i am not getting any exception and i have already set the internet permission.. – mak May 11 '12 at 12:01