0

I have a simple app that when you click on an image from a GridView, a Dialog pops up with a Button that says View. When you click the Button, I want to view the image that has been clicked in the gallery. I can get this to work without a problem when I have the app navigate to the gallery without a dialog, but I need it to hit a dialog first. The error my LogCat is returning Java.lang.ArrayIndexOutOfBoundsExeption. Any ideas as to what I need to do to get this to viewing the image? Below is my code.

public void onClick(View v) {
  Dialog dia = new Dialog(ViewGrid.this);
  dia.setContentView(R.layout.viewimage);
  dia.setTitle("What To Do?");
  dia.show(); 
  dia.setCancelable(true);
  Button button = (Button)dia.findViewById(R.id.viewImage);
  button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      int id = v.getId(); 
      Intent intent = new Intent();  
      intent.setAction(Intent.ACTION_VIEW);     
      intent.setDataAndType(Uri.parse("file://" + arrPath[id]), "image/*");
      startActivity(intent);
    }
  }); 
}

I am getting the error on this line:

intent.setDataAndType(Uri.parse("file://" + arrPath[id]), "image/*");

I figured it out! The below code is the working code. I needed to set my my Id that I got from the view to be from the click of the image, and not from the click of the View button. Thank you everyone for your assistance!

public void onClick(final View viewIt) {
  Dialog dia = new Dialog(ViewGrid.this);
  dia.setContentView(R.layout.viewimage);
  dia.setTitle("What To Do?");
  dia.show(); 
  dia.setCancelable(true);
  Button button = (Button)dia.findViewById(R.id.viewImage);
  button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      int id = viewIt.getId(); 
      Intent intent = new Intent();  
      intent.setAction(Intent.ACTION_VIEW);     
      intent.setDataAndType(Uri.parse("file://" + arrPath[id]), "image/*");
      startActivity(intent);
    }
  }); 
}
BossWalrus
  • 770
  • 1
  • 12
  • 29

4 Answers4

0

Not sure but I believe dia.show(); should be called at the very last - i.e after view.setOnClickListener.

waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • I moved that down, but it didn't change anything. The error I'm getting is on this line:intent.setDataAndType(Uri.parse("file://" + arrPath[id]), "image/*"); – BossWalrus Apr 14 '13 at 02:04
0

From where did this ArrayIndexOutOfBoundsException get thrown? I'm assuming it's thrown when you access arrPath[i]. How do you figure that the view's id should be an index into arrPath? That seems to be the problem.

Sky Kelsey
  • 19,192
  • 5
  • 36
  • 77
  • Correct, the error I'm getting is on this line:intent.setDataAndType(Uri.parse("file://" + arrPath[id]), "image/*"); – BossWalrus Apr 14 '13 at 03:29
  • How do you figure that the view's id should be an index into arrPath? This is where your problem is. You are accessing your array using an index that doesn't exist in the array. How are you putting your images into this array? I don't think that the id you are using to index your array is ever going to contain anything. – Sky Kelsey Apr 14 '13 at 09:12
0

Try printing

arrPath[id]

You should be getting the same error. As Sky Kelsey mentioned, there is something wrong with the view id being the index of the array.

If the file path is for example

File file = new File("/sdcard/yourfile-VIEWID.jpg");

then try instead of using an array to save the paths, do something

File file = new File("/sdcard/yourfile-"+String.valueof(id)+".jpg");
harrane
  • 959
  • 11
  • 24
0

I have figured out the problem and posted my correct code above in the question. Thank you everyone who helped.

BossWalrus
  • 770
  • 1
  • 12
  • 29