11

fragmentactivity から呼び出されるフラグメントを使用してギャラリーから画像を取得しようとしていますがonActivityResult、フラグメント クラスは呼び出されていません。これは FragmentActivity クラスが原因で発生する可能性があることがわかりましたがonActivityResult、両方のクラスのログを確認すると、呼び出されていないため、ギャラリーから選択した画像にアクセスできません。

ほとんどの解決策を試しましたが、まだ問題がわかりません。

私のメインフラグメントアクティビティ

public class CreateEvent extends FragmentActivity{

private int CREATE =0;
private int INVITE = 1;
private int FRAGMENT_COUNT = INVITE +1;
private Fragment[] fragment = new Fragment[FRAGMENT_COUNT]; 
private boolean isResumed = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FragmentManager fm = getSupportFragmentManager();
        fragment[CREATE] = fm.findFragmentById(R.id.create);
        fragment[INVITE] = fm.findFragmentById(R.id.invite);
        FragmentTransaction transaction = fm.beginTransaction();
        for(int i = 0 ; i<fragment.length; i++)
        {
            transaction.hide(fragment[i]);
        }
        transaction.commit();
    };
        private void showfragment(int fragmentIndex, boolean addToBackStack)
        {
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction transaction = fm.beginTransaction();
                for (int i = 0; i<fragment.length;i++)
            {
                if (i == fragmentIndex)
                {
                    transaction.show(fragment[i]);
                }
                else
                {
                    transaction.hide(fragment[i]);
                }
            }
            if (addToBackStack)
            {
                transaction.addToBackStack(null);
            }
            transaction.commit();
        }

// I've tried both including and excluding onresume and onresumefragment methods
@Override
protected void onResumeFragments() {
    super.onResumeFragments();
    {
            isResumed = true;
            showfragment(INVITE, false);
    }
  }
@Override
protected void onResume() {
        super.onResume();
        isResumed = true;
}

// I've tried both including and excluding this method as well
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
           super.onActivityResult(requestCode, resultCode, data);
           EventActivity activity = (EventActivity) getSupportFragmentManager().findFragmentById(R.id.create);
           activity.onActivityResult(requestCode, resultCode, data);

    }
}

私のフラグメントクラス

public class Event extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.createevent,container,false);

    imgcover = (ImageView) view.findViewById(R.id.newcover_img);
    btnupload = (Button) view.findViewById(R.id.newcover_upload);

    btnupload.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View arg0) {
        Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, 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 selectedImg = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(selectedImg,
                    filePathColumn, null, null, null);
                cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
        imgcover = (ImageView) findViewById(R.id.newcover_img);
        imgcover .setImageBitmap(BitmapFactory.decodeFile(picturePath));
                cursor.close();
    }
  }

}

4

2 に答える 2

12

私はこれをやっていて、うまくいきます!

それが役に立てば幸い!!

mImgProfile.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, IMAGE_PICKER_SELECT);
        }
    });


public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == IMAGE_PICKER_SELECT
            && resultCode == Activity.RESULT_OK) {
        String path = getPathFromCameraData(data, this.getActivity());
        Log.i("PICTURE", "Path: " + path);
        if (path != null) {
            setFullImageFromFilePath(mImgProfile, path);
        }
    }
}

public static String getPathFromCameraData(Intent data, Context context) {
    Uri selectedImage = data.getData();
    String[] filePathColumn = { MediaStore.Images.Media.DATA };
    Cursor cursor = context.getContentResolver().query(selectedImage,
            filePathColumn, null, null, null);
    cursor.moveToFirst();
    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    String picturePath = cursor.getString(columnIndex);
    cursor.close();
    return picturePath;
}
于 2014-07-02T18:07:22.213 に答える
-1
public class FragmentProfile extends Fragment {

    private static final int RESULT_OK = -1;
    Bitmap image;
    private GetPhoto gph;

    private static int RESULT_LOAD_IMAGE = 1;


    View myView;
    private ImageView profImg;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.profile_layout, container, false);


        //Setting up the content
        profImg = (ImageView) myView.findViewById(R.id.prfimg);


        //Choosing profile pic from clicking on the ImageView
        profImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });


        return myView;
    }

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

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

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

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


            profImg.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }
    }
}
于 2016-10-29T09:39:38.503 に答える