2011-12-24 2 views
0

내 응용 프로그램에서 이미지를 탐색 한 다음 편집 이미지에 내 이미지 경로를 표시하고 싶습니다.편집 텍스트에 이미지 경로 저장

내 .java는 다음과 같습니다. package 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 
+0

BrowsePicture.java의 라인 54는 무엇입니까? –

+0

mytext.setText (selectedImagePath); – jonny

답변

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(); 
+0

어디에 넣어야합니까? 활동 결과에 – jonny

+1

케이스 1234를 요청 코드 Select_picture ..로 바꾸십시오. EditText mytext = (EditText) findViewById (R.id.editText1); 이 라인은 버튼 클릭 리스너 내부에 작성됩니다. 청취자에게서이 선을 옮기십시오. – Kamal

1
setContentView(R.layout.main); 

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

재 작성이 문제를 생성 버튼을 클릭 리스너에서 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() { 

.. 

에, 그래서 그냥 가져 그 라인 아웃

관련 문제