Download Random Images from file server
===

This is my attempt. My files are hosted on my own servers mostly JPGs files. I'm trying to download them into my app. I failed to generate those images into my app. I follow the guide from this blog http://getablogger.blogspot.gr/2008/01/android-download-image-from-server-and.html

My xml 

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello World, HTTPImage load test"
    />
    <Button android:id="@+id/get_imagebt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Get an image"
    android:layout_gravity="center"
    /> 
    <ImageView android:id="@+id/imview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    />
    </LinearLayout>

Coding

    package com.example.downloadimages;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Random;
    
    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ImageView;
    
    
    
    
    public class MainActivity extends Activity {
    
    	ImageView imView;
    	String imageUrl="http://myfilehosting.com/";
    	Random r;
    	
    	
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            r= new Random();
            
            Button bt3= (Button)findViewById(R.id.get_imagebt);
            bt3.setOnClickListener(getImgListener);
            imView = (ImageView)findViewById(R.id.imview);
        }
        
        View.OnClickListener getImgListener = new View.OnClickListener()
        {
    
        	public void onClick(View view) {
        	// TODO Auto-generated method stub
    
        	//i tried to randomize the file download, in my server i put 4 files with name like
        	//jpg0.jpg, jpg1.jpg, jpg2.jpg so different file is downloaded in button press
        	int i =r.nextInt()%4;
        	downloadFile(imageUrl+i+".jpg");
        	
        	}
    
        	Bitmap bmImg;
        	void downloadFile(String fileUrl){
        	URL myFileUrl =null; 
        	try {
        	myFileUrl= new URL(fileUrl);
        	} catch (MalformedURLException e) {
        	// TODO Auto-generated catch block
        	e.printStackTrace();
        	}
        	try {
        	HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
        	conn.setDoInput(true);
        	conn.connect();
        	int length = conn.getContentLength();
        	int[] bitmapData =new int[length];
        	byte[] bitmapData2 =new byte[length];
        	InputStream is = conn.getInputStream();
    
        	bmImg = BitmapFactory.decodeStream(is);
        	imView.setImageBitmap(bmImg);
        	} catch (IOException e) {
        	// TODO Auto-generated catch block
        	e.printStackTrace();
        	}
    
        	
        	
        	
       
    };
    };
    }


