0

Whenever I try to click on the browse button , I want to browse my gallery and select a photo from it and display it in a new activity. Until I displayed the photo on the same activity there was no error , but as soon I change the code to transfer the photo on another intent there is a force close error enter image description here

Here is my code:

Main screen :

Button button = (Button) findViewById(R.id.Button01);
    button.setOnClickListener(new OnClickListener() 
    {
          public void onClick(View arg0) 
          {
                     Intent intent1 = new Intent(MainActivity.this,Edit.class);
             startActivity(intent1);
          }

    });

Another screen :

public class Edit extends Activity 
   {

private String selectedImagePath;
private static final int SELECT_PICTURE = 1;
private ImageView img;

      protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edit);

    img = (ImageView)findViewById(R.id.ImageView1);




     Intent intent = new Intent();
         intent.setType("image/*");
         intent.setAction(Intent.ACTION_GET_CONTENT);
         startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);

}

 public void onActivityResult(int requestCode, int resultCode, Intent data) 
 {

    if (resultCode == RESULT_OK) 
        {
            if (requestCode == SELECT_PICTURE) 
            {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                System.out.println("Image Path : " + selectedImagePath);
                img.setImageURI(selectedImageUri);
            }
        }

 }





public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
Sachin Prasad
  • 5,365
  • 12
  • 54
  • 101
Anuj
  • 301
  • 1
  • 4
  • 20
  • show your getPath function code.. – moDev Jan 05 '13 at 20:13
  • public String getPath(Uri uri) { String[] projection = { MediaStore.Images.Media.DATA }; Cursor cursor = managedQuery(uri, projection, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } – Anuj Jan 05 '13 at 20:14
  • 1
    why you are using Intent.createChooser ?? just pass intent object. – moDev Jan 05 '13 at 20:22
  • it wnt work by jst passing object, i tried – Anuj Jan 05 '13 at 20:26
  • like this startActivityForResult(intent, SELECT_PICTURE); – moDev Jan 05 '13 at 20:27
  • its one and the same , tried with object also , not working – Anuj Jan 05 '13 at 20:27
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/22245/discussion-between-mitesh-agrawal-and-anuj) – moDev Jan 05 '13 at 20:28
  • yeah i tried but its not working , as soon as i select a photo from my galary , it redirect back to the activity and it forcecloses. – Anuj Jan 05 '13 at 20:29
  • possible duplicate of [I'm getting a NullPointerException when I use ACTION\_IMAGE\_CAPTURE to take a picture](http://stackoverflow.com/questions/3275749/im-getting-a-nullpointerexception-when-i-use-action-image-capture-to-take-a-pic) – A--C Jan 05 '13 at 23:58

0 Answers0