save image path in edit text
<p>In my application i   want  to browse an image and then display the path of my image in my edit text.  </p>

<p>Here is my .java:
         package ianco.test.andrei;</p>

<pre><code>     import android.app.Activity;
     import android.content.Intent;
     import android.database.Cursor;
     import android.net.Uri;
     import android.os.Bundle;
     import android.provider.MediaStore;
     import android.view.View;
     import android.view.View.OnClickListener;
     import android.widget.Button;
     import android.widget.EditText;

     public class BrowsePicture extends Activity {

//YOU CAN EDIT THIS TO WHATEVER YOU WANT
private static final int SELECT_PICTURE = 1;

private String selectedImagePath;
//ADDED
private String filemanagerstring;
public EditText mytext;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

   Button sButton = (Button) findViewById(R.id.button1);
   sButton.setOnClickListener(new OnClickListener() {
      EditText mytext=(EditText)findViewById(R.id.editText1);
        public void onClick(View arg0) {

            // in onCreate or any event where your want the user to
            // select a file
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent,
                    "Select Picture"), SELECT_PICTURE);
        }
    });
}

//UPDATED
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();

            //OI FILE Manager
            filemanagerstring = selectedImageUri.getPath();

            //MEDIA GALLERY
            selectedImagePath = getPath(selectedImageUri);
       mytext.setText(selectedImagePath);


            //NOW WE HAVE OUR WANTED STRING
            if(selectedImagePath!=null)
                System.out.println("selectedImagePath is the right one for you!");
            else
                System.out.println("filemanagerstring is the right one for you!");
        }
    }
}

//UPDATED!
public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    if(cursor!=null)
    {
        //HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
        //THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
        int column_index = cursor
        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    else return null;
}
}
</code></pre>

<p>and this is the error i get:</p>

<pre><code>    12-24 11:48:15.438: E/AndroidRuntime(4396): FATAL EXCEPTION: main
12-24 11:48:15.438: E/AndroidRuntime(4396): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/72 }} to activity {ianco.test.andrei/ianco.test.andrei.BrowsePicture}: java.lang.NullPointerException
12-24 11:48:15.438: E/AndroidRuntime(4396):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2553)
12-24 11:48:15.438: E/AndroidRuntime(4396):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:2595)
12-24 11:48:15.438: E/AndroidRuntime(4396):     at android.app.ActivityThread.access$2000(ActivityThread.java:121)
12-24 11:48:15.438: E/AndroidRuntime(4396):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:973)
12-24 11:48:15.438: E/AndroidRuntime(4396):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-24 11:48:15.438: E/AndroidRuntime(4396):     at android.os.Looper.loop(Looper.java:130)
12-24 11:48:15.438: E/AndroidRuntime(4396):     at android.app.ActivityThread.main(ActivityThread.java:3701)
12-24 11:48:15.438: E/AndroidRuntime(4396):     at java.lang.reflect.Method.invokeNative(Native Method)
12-24 11:48:15.438: E/AndroidRuntime(4396):     at java.lang.reflect.Method.invoke(Method.java:507)
12-24 11:48:15.438: E/AndroidRuntime(4396):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
12-24 11:48:15.438: E/AndroidRuntime(4396):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
12-24 11:48:15.438: E/AndroidRuntime(4396):     at dalvik.system.NativeStart.main(Native Method)
12-24 11:48:15.438: E/AndroidRuntime(4396): Caused by: java.lang.NullPointerException
12-24 11:48:15.438: E/AndroidRuntime(4396):     at ianco.test.andrei.BrowsePicture.onActivityResult(BrowsePicture.java:54)
12-24 11:48:15.438: E/AndroidRuntime(4396):     at android.app.Activity.dispatchActivityResult(Activity.java:3908)
12-24 11:48:15.438: E/AndroidRuntime(4396):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2549)
12-24 11:48:15.438: E/AndroidRuntime(4396):     ... 11 more
</code></pre>
