2014-02-17 109 views
6

我目前正在使用Fragment。我想從圖庫中挑選圖片&將其顯示在onTo片段類中。但是這裏startActivityForResult()在Fragment中不起作用。startActivityForResult在片段中不起作用

我的片段類

Button loadimage=(Button) view.findViewById(R.id.loadButton); 
    loadimage.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
      startActivityForResult(i, RESULT_LOAD_IMAGE); 
     } 
    }); 
@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { 
     Uri selectedImage = data.getData(); 
     String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

     Cursor cursor = getActivity().getContentResolver().query(selectedImage, 
       filePathColumn, null, null, null); 
     cursor.moveToFirst(); 

     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     String picturePath = cursor.getString(columnIndex); 
     cursor.close(); 
     imgview.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 

    } 

    } 

回答

15

在活動類:

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data){ 
    super.onActivityResult(requestCode,resultCode,data); 
} 

在片段:

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data){ 
    // Handle activity result here 
} 

開始活動結果從片段:

Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(intent, 1); 
5

覆蓋您父母片段活動中的活動結果

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
}