Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8639213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:56:47+00:00 2026-06-12T10:56:47+00:00

I am making a custom layout in which I want to add the image

  • 0

I am making a custom layout in which I want to add the image view dynamically from the gallery. But as soon as I pick the image from the gallery , I got force close. I am unable to get what is the error behind the same.

Here is my code..

public class Dynamic extends Activity {
int i;
ImageView[] img_items;
ArrayList<String> values = new ArrayList<String>();
LinearLayout imageLayout;
Button btn;
private static int RESULT_LOAD_IMAGE = 1;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dynamic_load);

    imageLayout = (LinearLayout)findViewById(R.id.image_layout);
    btn = (Button)findViewById(R.id.btn_ADD);
    img_items = new ImageView[values.size()];

    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            values.add("AA");

            Intent i = new Intent(
                    Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(i, RESULT_LOAD_IMAGE);

        }
    });


}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        /*ImageView imageView = (ImageView) findViewById(R.id.imgView);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));*/


        //Creating the image layouts dynamically
        for( i=0;i<values.size();i++){

            img_items[i] = new ImageView(Dynamic.this);

            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);

            params.setMargins(10, 10, 0, 0);
            img_items[i].setLayoutParams(params);

        }

        img_items[i].setImageBitmap(BitmapFactory.decodeFile(picturePath));
        imageLayout.addView(img_items[i]);


    }
}
}

LOG CAT

10-05 16:52:03.422: D/AndroidRuntime(451): Shutting down VM
10-05 16:52:03.422: W/dalvikvm(451): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-05 16:52:03.549: E/AndroidRuntime(451): FATAL EXCEPTION: main
10-05 16:52:03.549: E/AndroidRuntime(451): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/1 }} to activity {com.example.dynamicloading/com.example.dynamicloading.Dynamic}: java.lang.ArrayIndexOutOfBoundsException
10-05 16:52:03.549: E/AndroidRuntime(451):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3515)
10-05 16:52:03.549: E/AndroidRuntime(451):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)
10-05 16:52:03.549: E/AndroidRuntime(451):  at android.app.ActivityThread.access$2800(ActivityThread.java:125)
10-05 16:52:03.549: E/AndroidRuntime(451):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)
10-05 16:52:03.549: E/AndroidRuntime(451):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-05 16:52:03.549: E/AndroidRuntime(451):  at android.os.Looper.loop(Looper.java:123)
10-05 16:52:03.549: E/AndroidRuntime(451):  at android.app.ActivityThread.main(ActivityThread.java:4627)
10-05 16:52:03.549: E/AndroidRuntime(451):  at java.lang.reflect.Method.invokeNative(Native Method)
10-05 16:52:03.549: E/AndroidRuntime(451):  at java.lang.reflect.Method.invoke(Method.java:521)
10-05 16:52:03.549: E/AndroidRuntime(451):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-05 16:52:03.549: E/AndroidRuntime(451):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-05 16:52:03.549: E/AndroidRuntime(451):  at dalvik.system.NativeStart.main(Native Method)
10-05 16:52:03.549: E/AndroidRuntime(451): Caused by: java.lang.ArrayIndexOutOfBoundsException
10-05 16:52:03.549: E/AndroidRuntime(451):  at com.example.dynamicloading.Dynamic.onActivityResult(Dynamic.java:81)
10-05 16:52:03.549: E/AndroidRuntime(451):  at android.app.Activity.dispatchActivityResult(Activity.java:3890)
10-05 16:52:03.549: E/AndroidRuntime(451):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3511)
10-05 16:52:03.549: E/AndroidRuntime(451):  ... 11 more
10-05 16:57:04.140: I/Process(451): Sending signal. PID: 451 SIG: 9
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T10:56:48+00:00Added an answer on June 12, 2026 at 10:56 am

    you need to decrease the i value by one as the i was increment in for loop for example you have iterate for 5 time so the for loop will be iterate 5 time starting with 0 and ending with the 4 but at that time the i value was incremented first then it will check the 2 expression whether the length and for i at that time i value and values.size() are same so it terminate the for loop execute rest of code

    Edited

    if(i==values.size() && i>0)
       i--;
    
    img_items[i].setImageBitmap(BitmapFactory.decodeFile(picturePath));
    imageLayout.addView(img_items[i]);
    

    and one more thing you need set i=0 every time when you in onActivityResult() other wise you always getting i last value

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I making a custom button, and I need to add a PreviewKeyDown event, whenever
I'm making a custom activity for a workflow, but am having trouble with InArguments.
I am making a custom form object in Django which has an overrided __init__
I'm making a custom error dialog in my WPF app and I want to
I am making a custom control I need to add some default keybindings, microsoft
I'm making a custom lock screen. The lock screen is an activity which I
I'm making a custom XML layout to display a cocktail recipe. Here is my
I'm making custom google map for the webview in iPhone4 but all streets and
I created a ListView with a custom layout for each view. I have several
I am making a Custom Layout Android component by extending abstract class ViewGroup (as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.