简体   繁体   English

从相机或图库上传图像时,startActivityForResult在片段中不起作用

[英]startActivityForResult doesn't work in a Fragment while image upload from camera or gallery

public class Profile extends Fragment implements Profile_frg{


imageView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                final Dialog d = new Dialog(mainActivity);
                d.requestWindowFeature(Window.FEATURE_NO_TITLE);
                d.setContentView(R.layout.activity_custom_dialog);
                d.setCanceledOnTouchOutside(true);


                gallery = (ImageView) d.findViewById(R.id.imageView1);
                camera = (ImageView) d.findViewById(R.id.imageView2);
                cancel = (ImageView) d.findViewById(R.id.imageView3);

                cancel.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        d.dismiss();
                    }
                });


                gallery.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        try {
                            Intent gintent = new Intent();
                            gintent.setType("image/*");
                            gintent.setAction(Intent.ACTION_GET_CONTENT);
                            startActivityForResult(Intent.createChooser(
                                    gintent, "Select Picture"), PICK_IMAGE);
                        } catch (Exception e) {
                            Toast.makeText(mainActivity,
                                    e.getMessage(), Toast.LENGTH_LONG).show();
                            Log.e(e.getClass().getName(), e.getMessage(), e);
                        }

                        d.dismiss();
                    }

                });

                camera.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        // define the file-name to save photo taken by Camera
                        // activity
                        String fileName = "new-photo-name.jpg";
                        // create parameters for Intent with filename
                        ContentValues values = new ContentValues();
                        values.put(MediaStore.Images.Media.TITLE, fileName);
                        values.put(MediaStore.Images.Media.DESCRIPTION,
                                "Image captured by camera");
                        // imageUri is the current activity attribute, define
                        // and save it for later usage (also in
                        // onSaveInstanceState)
                        imageUri = context.getContentResolver().insert(
                                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                                values);
                        // create new Intent
                        Intent intent = new Intent(
                                MediaStore.ACTION_IMAGE_CAPTURE);
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

                        startActivityForResult(intent, PICK_Camera_IMAGE);

                        d.dismiss();
                    }

                });
                d.show();
            }
        });

}// Work Fine till here...



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

    }//didn't detect this method

Yes, there is no onActivityResult() callback in fragments. 是的,片段中没有onActivityResult()回调。 You have to override activityResult method in your host activity(in which your fragment is defined) 您必须在主机活动(其中定义了片段)中覆盖activityResult方法

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   if(requestCode == GALLERY/CAMERA_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
       Fragment yourFragment = getSupportFragmentManager().findFragmentByTag("FRAGMENT_TAG"); // same tag while adding fragment for the first time.
       if (yourFragment != null) {
           yourFragment.onActivityResult(requestCode, resultCode, data); //calling method that should be defined in your fragment.
       }
   }
   super.onActivityResult(requestCode, resultCode, data);
}

And in your fragment do like this : 并在您的片段中这样做:

public void onActivityResult(int requestCode,int resultCode,Intent data) {
   ...
   Pull your image data from data object
   do your further process from here.
   ...
}

Yes, startActivityForResult will not work directly if you are calling it from any fragment. 是的,如果您从任何片段中调用startActivityForResult,它将无法直接工作。 After getting the result, the callback will hit the onActivityResult of the Hosting Activity from where you have to manually redirect it to the respective fragment . 获得结果后,回调将命中Hosting ActivityonActivityResult,您必须在其中手动将其重定向到相应的fragment

Below is the sample code of the onActivityResult of your Activity. 以下是您的Activity的onActivityResult的示例代码。 Please note that this only redirect the result to the respective fragment. 请注意,这只会将结果重定向到相应的片段。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case 1001:
            Fragment frag = getSupportFragmentManager().findFragmentByTag("TAG"); // TAG should be same as the one you entered while adding the fragment
            if (frag != null) {
                frag .onActivityResult(requestCode, resultCode, data);
            }
            break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM