Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 未在片段中调用Android onactivityresult()_Java_Android_Android Fragments_Onactivityresult - Fatal编程技术网

Java 未在片段中调用Android onactivityresult()

Java 未在片段中调用Android onactivityresult(),java,android,android-fragments,onactivityresult,Java,Android,Android Fragments,Onactivityresult,这是我调用活动结果的代码。我正在使用一个对话框,提示从画廊或相机中选择图像。相同的代码在活动中工作,但在片段中不工作。我已经尝试了stackoverflow的所有先前答案。请帮忙 AlertDialog.Builder builder = new AlertDialog.Builder( getActivity()); builder.setTitle("Choose Image Source");

这是我调用活动结果的代码。我正在使用一个对话框,提示从画廊或相机中选择图像。相同的代码在活动中工作,但在片段中不工作。我已经尝试了stackoverflow的所有先前答案。请帮忙

AlertDialog.Builder builder = new AlertDialog.Builder(
                        getActivity());

                builder.setTitle("Choose Image Source");
                builder.setItems(new CharSequence[] { "Pick from Gallery",
                        "Take from Camera" },
                        new DialogInterface.OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                switch (which) {
                                case 0:

                                    Intent intent = new Intent(
                                            Intent.ACTION_PICK,
                                            android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);

                                    intent.putExtra("crop", "true");
                                    intent.putExtra("aspectX", 1);
                                    intent.putExtra("aspectY", 1);
                                    intent.putExtra("outputX", 250);
                                    intent.putExtra("outputY", 250);

                                    try {

                                        intent.putExtra("return-data", true);
                                        startActivityForResult(
                                                Intent.createChooser(
                                                        intent,
                                                        "Complete action using"),
                                                PICK_FROM_GALLERY);

                                    } catch (ActivityNotFoundException e) {

                                    }

                                    break;

                                case 1:

                                    Intent takePictureIntent = new Intent(
                                            MediaStore.ACTION_IMAGE_CAPTURE);
                                    if (takePictureIntent
                                            .resolveActivity(getActivity()
                                                    .getPackageManager()) != null) {

                                        startActivityForResult(
                                                takePictureIntent,
                                                PICK_FROM_CAMERA);

                                    }

                                    break;

                                default:
                                    break;
                                }
                            }
                        });
                builder.show();





public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub

    try {
        if (requestCode == PICK_FROM_GALLERY) {
            System.out.print("ho ja please");
            Bundle extras2 = data.getExtras();
            if (extras2 != null) {
                bitmap = extras2.getParcelable("data");

                Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                        bitmap.getHeight(), Config.ARGB_8888);


                dp.setImageBitmap(output);


            }
        }

        if (requestCode == PICK_FROM_CAMERA) {

            Bundle extras = data.getExtras();
            Bitmap bitmap1 = (Bitmap) extras.get("data");

            bitmap = Bitmap.createScaledBitmap(bitmap1, 250, 250, true);

            Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
                    bitmap.getHeight(), Config.ARGB_8888);

            dp.setImageBitmap(output);



        }

您可以通过使用活动上下文调用startActivityForResult进行尝试,即

getActivity().startActivityForResult(Intent.createChooser(
                                                        intent,
                                                        "Complete action using"),
                                                PICK_FROM_GALLERY);
我正在使用下面的代码显示图像选择器,请检查此代码是否有助于您-

private static final int SELECT_PICTURE = 1; // Declare this variable

Intent pickIntent = new Intent();
                pickIntent.setType("image/*");
                pickIntent.setAction(Intent.ACTION_GET_CONTENT);

                Intent takePhotoIntent = new Intent(
                        MediaStore.ACTION_IMAGE_CAPTURE);

                String pickTitle = "Select or take picture";

                // strings.xml
                Intent chooserIntent = Intent.createChooser(pickIntent,
                        pickTitle);
                chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
                        new Intent[] { takePhotoIntent });

                getActivity().startActivityForResult(chooserIntent,
                        SELECT_PICTURE);
然后,在您的活动中,您可以使用此-

protected void onActivityResult(int requestCode, int resultCode,
            Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
        if (imageReturnedIntent != null) {
            if (imageReturnedIntent.getData() != null) {
                Uri selectedImage = imageReturnedIntent.getData(); // use this URI for getting and updating the fragment


            }
        }
    }

这是因为警报对话框调用Activity.startActivityForResult而不是Fragment.startActivityForResult。如果要修复该行为,请使用对话框中的片段引用调用startActivityForResult


UPD:同样如前所述,不要忘记在Activity.onActivityResult方法中调用super.onActivityResult,如果被覆盖

我已经尝试了你的代码,并且它对我来说工作得很好。您在何处编写了此代码,仅在片段中正确?仅在片段中正确我在oncreateview中使用了您的alertbox并选中了。它工作得很好。你可以发布片段的代码我已经在片段中编写了代码。。对话框出现,但当我从gallery中选择照片时,图像未在imageview上设置。让我们来看看。当使用getActivity()时,startActivityForResult则OnActivTyresult应位于Activity而不是Fragment中,因为Activity中的请求代码正确,但不在Fragment中。请尝试添加-super.onActivityResult(请求代码、结果代码、意图);作为method@AshishTamrakar也尝试过这个。它不起作用检查编辑,我已经从我的应用程序中添加了一些代码。希望这对你有帮助