1

I can load one image from mysql databse using Async Task, but now i want to load 5 images and show them in a list view so i want first to know how to make doInbackground method return a list of images got from mysql databse :

 @Override
        protected Bitmap doInBackground(String... params) {
            String id = params[0];
            String add = "http://192.168.1.11/save/load_image_from_db.php?id=" + id;
            URL url;
            Bitmap image = null;
            try {
                final BitmapFactory.Options options = new BitmapFactory.Options();
                //options.inJustDecodeBounds = true;
                //options.inSampleSize = 4;
                //options.inJustDecodeBounds = false;
                url = new URL(add);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                image = BitmapFactory.decodeStream(connection.getInputStream(),null,options);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return image;
        }

2 Answers 2

1

In this case you have to return an ArrayList of images, the code will be something like this,, (note: I have not tests this code)

    @Override protected Bitmap doInBackground(String... params) {
     String id = params[0]; 

    String add1 = "http://192.168.1.11/save/load_image_from_db.php?id=" + id; 
    String add2="http://another photo";
    String add3="http://another photo";
    String[] adds={add1, add2, add3};
    URL url;

    ArrayList <Bitmap> imageList=new ArrayList();

     Bitmap image = null; 

    for (int i=0;i<adds.length;i++){

    try { 
    final BitmapFactory.Options options = new BitmapFactory.Options(); null //options.inJustDecodeBounds = true; //options.inSampleSize = 4;  null//options.inJustDecodeBounds = false;

 url = new URL(adds[i]); null HttpURLConnection connection =(HttpURLConnection) url.openConnection();  nullimage = BitmapFactory.decodeStream(connection.getInputStream(),null,options);  null} catch (MalformedURLException e) { e.printStackTrace();  null} catch (IOException e) { e.printStackTrace();  null}

    imageList.add( image);
    image=null;
    }
     return imageList; 
    }
Sign up to request clarification or add additional context in comments.

4 Comments

can't we get list of image from one URL ?
each image has it's url,, what do you mean by list of images from one url?? list of image has list of url, that is the correct one.. Yes, of course you can get some images and load them into the listview
i'm wondering if it is possible to write a php script that displays all the images stored in mysql database, and use this url in android apps to get all the images from just one url
You can use php to fetch the data from the database then encode it to JSON,,, then from the mobile get the JSON and download images and texts form the website.. Volley is the best to do it..
1
public class MyAsyncTask extends AsyncTask<String, Void, ArrayList<Bitmap>> {

    @Override
    protected ArrayList<Bitmap> doInBackground(String... params) {
        ArrayList<Bitmap> bmps = new ArrayList<>();
        for(int i = 0; i < params.length; i++) {
            String id = params[i];
            String add = "http://192.168.1.11/save/load_image_from_db.php?id=" + id;
            URL url;
            Bitmap image = null;
            try {
                final BitmapFactory.Options options = new BitmapFactory.Options();
                //options.inJustDecodeBounds = true;
                //options.inSampleSize = 4;
                //options.inJustDecodeBounds = false;
                url = new URL(add);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                image = BitmapFactory.decodeStream(connection.getInputStream(), null, options);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            bmps.add(image);
        }

        return bmps;
    }
}

You would then call the execute method with an array of ids... et voila!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.