在编辑文本中保存图像路径

时间:2011-12-24 09:54:03

标签: android

在我的应用程序中,我想浏览图像,然后在编辑文本中显示图像的路径。

这是我的.java:          包ianco.test.andrei;

     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;
}
}

这是我得到的错误:

    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

2 个答案:

答案 0 :(得分:1)

setContentView(R.layout.main);

   Button sButton = (Button) findViewById(R.id.button1);
   sButton.setOnClickListener(new OnClickListener() {
      EditText mytext=(EditText)findViewById(R.id.editText1);
....

重写

 setContentView(R.layout.main);

 EditText mytext=(EditText)findViewById(R.id.editText1);
       Button sButton = (Button) findViewById(R.id.button1);
       sButton.setOnClickListener(new OnClickListener() {

..

您在按钮点击监听器中写了EditText mytext=(EditText)findViewById(R.id.editText1);,这会产生问题,所以只需要删除该行

答案 1 :(得分:0)

 super.onActivityResult(requestCode, resultCode, data); 

                switch(requestCode) { 
                case 1234:
                    if(resultCode == RESULT_OK){  
                        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]);
                        filePath = cursor.getString(columnIndex);
                        Log.i("the path of image is", filePath);
edt.setText(filepath);
                        cursor.close();