简体   繁体   中英

I want to to get multiple images from the url and have to use them in my image view

My application crashes on execution i have used bitmap array for storing images .can anyone help me out special thanks in advance not providing the url but i am using the valid url of images so there is not a problem.

thank you.

    package abc.expandable;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.ArrayList;

    import android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.ImageView;
    import android.widget.Toast;

    public class MainActivity extends Activity {

        public static final String URL =
                " some url";
        public static final String URL1="some url";
        ImageView imageView;
        ImageView imageView1;
        ArrayList<Bitmap> bitmapArray=new ArrayList<Bitmap>(4);
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            imageView = (ImageView) findViewById(R.id.imageView);
            imageView1=(ImageView)findViewById(R.id.imageView1);
            // Create an object for subclass of AsyncTask
            GetXMLTask task = new GetXMLTask();
            // Execute the task
            int a=0;
            task.execute(new String[]{URL});
            imageView.setImageBitmap(bitmapArray.get(0));

            Toast.makeText(MainActivity.this, "downloading image please wait ", Toast.LENGTH_SHORT).show();


           // task.execute(new String[]{URL1});
            //imageView1.setImageBitmap(bitmapArray.get(a));
        }

        private class GetXMLTask extends AsyncTask<String, Void, Bitmap> {
            @Override
            protected Bitmap doInBackground(String... urls) {
                Bitmap map = null;
                for (String url : urls) {
                    map = downloadImage(url);
                }
                return map;
            }

            // Sets the Bitmap returned by doInBackground
            @Override
            protected void onPostExecute(Bitmap result)
            {

    //            imageView.setImageBitmap(bitmapArray.get(0));
                Toast.makeText(MainActivity.this, "downloaded", Toast.LENGTH_SHORT).show();
                Log.i("bitmap", "bitmap ");
            }

            // Creates Bitmap from InputStream and returns it
            private Bitmap downloadImage(String url) {
                Bitmap bitmap = null;
                InputStream stream = null;
                BitmapFactory.Options bmOptions = new BitmapFactory.Options();
                bmOptions.inSampleSize = 1;

                try {
                    stream = getHttpConnection(url);
                    bitmap = BitmapFactory.
                            decodeStream(stream, null, bmOptions);
                    stream.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                bitmapArray.add(bitmap);
                return bitmap;

            }

            // Makes HttpURLConnection and returns InputStream
            private InputStream getHttpConnection(String urlString)
                    throws IOException {
                InputStream stream = null;
                URL url = new URL(urlString);
                URLConnection connection = url.openConnection();

                try {
                    HttpURLConnection httpConnection = (HttpURLConnection) connection;
                    httpConnection.setRequestMethod("GET");
                    httpConnection.connect();

                    if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                        stream = httpConnection.getInputStream();
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                return stream;
            }

        }
    }

here is the log cat

03-04 18:59:24.307 18588-18588/abc.expandable E/AndroidRuntime: FATAL EXCEPTION: main
                                                                Process: abc.expandable, PID: 18588
                                                                java.lang.RuntimeException: Unable to start activity ComponentInfo{abc.expandable/abc.expandable.MainActivity}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
                                                                    at android.app.ActivityThread.access$800(ActivityThread.java:155)
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                    at android.os.Looper.loop(Looper.java:135)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5343)
                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                    at java.lang.reflect.Method.invoke(Method.java:372)
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
                                                                 Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
                                                                    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
                                                                    at java.util.ArrayList.get(ArrayList.java:308)
                                                                    at abc.expandable.MainActivity.onCreate(MainActivity.java:38)
                                                                    at android.app.Activity.performCreate(Activity.java:6010)
                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413) 
                                                                    at android.app.ActivityThread.access$800(ActivityThread.java:155) 
                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317) 
                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                    at android.os.Looper.loop(Looper.java:135) 
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5343) 
                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                    at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) 
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 

Remove this line and it should work:

imageView.setImageBitmap(bitmapArray.get(0));

You are accessing an empty array and this causes the exception because your array members are added in a different thread that is slower than your UI thread. You should set your imageview in the onPostExecute of you ASyncTask. This way, you make sure that the ASyncTask has finished and has added members to your array before you try to access them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM