Android 从活动到片段的图像拾取

Android 从活动到片段的图像拾取,android,image,android-fragments,Android,Image,Android Fragments,我想从我的相册中得到一张照片。。。下面的代码可以完美地用于活动,但当我从片段中使用它时。。。它没有崩溃,只是没有达到“onActivityResult”方法。。。有人能对正在发生的事情提出建议吗 @Override public void onClick(View v) { if (v.getId() == mCIB.getId()) { Intent i = new Intent( Intent.ACTION_PICK,

我想从我的相册中得到一张照片。。。下面的代码可以完美地用于活动,但当我从片段中使用它时。。。它没有崩溃,只是没有达到“onActivityResult”方法。。。有人能对正在发生的事情提出建议吗

@Override
public void onClick(View v) {
    if (v.getId() == mCIB.getId()) {
        Intent i = new Intent(
                Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(Intent.createChooser(i, ""), RESULT_LOAD_IMAGE);
    }

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RESULT_LOAD_IMAGE && data != null) {
        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();

        //BitmapFactory.decodeFile(picturePath)
        mCIB.setBitmapDrawable((BitmapDrawable) BitmapDrawable.createFromPath(picturePath), picturePath);
        try {
            mCIB.createImagePath();
            mCIB.addImageToDB();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
请尝试此代码

父活动:SampleActivity.java

public class SampleActivity extends FragmentActivity {

    FragmentManager fragmentManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.parent_layout);
        fragmentManager = getSupportFragmentManager();
        if (savedInstanceState == null) {
            fragmentManager.beginTransaction().add(R.id.container,
                            new SampleFragment(),"sample_fragment").commit();
        }

}
父布局:Parent.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="MergeRootFrame" />

可能有帮助:请参阅此问题[onActivityResult未在片段中调用][1][1]:很抱歉,但没有任何功能工作。。。我想我做错了把你的日志贴在这里
    public class SampleFragment extends Fragment {

        int reqCode = 111;
        ImageView image;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.activity_sample, null);
            image = (ImageView) rootView.findViewById(R.id.image);

            image.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (v.getId() == image.getId()) {
                    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    //photoPickerIntent.setType("image/*");
                    startActivityForResult(Intent.createChooser(photoPickerIntent, ""), reqCode);

                }

                }
            });
            return rootView;
        }

     @Override
     public void onActivityResult(int requestCode, int resultCode,
            Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
        if (null != imageReturnedIntent && requestCode == 111) {
            //String strUriPatPic = imageReturnedIntent.getData().toString();
            Uri selectedImage = imageReturnedIntent.getData();
            onPictureSelected(selectedImage);
        } 
     }

    private void onPictureSelected(Uri uri) {
        Bitmap userPictureBitmap = decodeSampledBitmapFromUri(getActivity(),
                uri, 100, 100);
        image.setImageBitmap(userPictureBitmap);
    }

    private Bitmap decodeSampledBitmapFromUri(Activity callingActivity,
            Uri uri, int reqWidth, int reqHeight){
     // TODO decode as you want
    }
 }