startActivityForResult返回错误的活动 [英] startActivityForResult returns to the wrong activity

查看:87
本文介绍了startActivityForResult返回错误的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用自定义相机(带有Surfaceview等)的应用程序,我正在使用ObjectActivity中的startActivityForResult转到名为CameraActivity的相机进行的活动.这种情况就是这种情况.

I am working on an app which uses a custom camera (with Surfaceview and such), I am using startActivityForResult from my ObjectActivity to go to the activity with the camera named CameraActivity. This happens in this method.

public void addPicture(View v) {
    final CharSequence[] items = { "Take Photo", "Choose from Gallery", "Cancel" };
    AlertDialog.Builder builder = new AlertDialog.Builder(ObjectActivity.this);
    builder.setTitle("Add Photo!");
    builder.setItems(items, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int item) {
            if (items[item].equals("Take Photo")) {
                executeOnResume = false;
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
                    int hasWriteExternalStoragePermission = checkSelfPermission(Manifest.permission.CAMERA);
                    if (hasWriteExternalStoragePermission != PackageManager.PERMISSION_GRANTED) {
                        requestPermissions(new String[]{Manifest.permission.CAMERA},
                                REQUEST_CODE_ASK_PERMISSIONS);
                    }
                }
                Intent intent = new Intent(ObjectActivity.this,CameraActivity.class);
                startActivityForResult(intent, REQUEST_CAMERA);
            } else if (items[item].equals("Choose from Gallery")) {
                executeOnResume = false;
                Intent intent = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                intent.setType("image/*");
                startActivityForResult(
                        Intent.createChooser(intent, "Select File"),
                        REQUEST_SELECT_FILE);
            } else if (items[item].equals("Cancel")) {
                dialog.dismiss();
            }
        }
    });
    builder.show();
}

更具体地说,它发生在以下行:

To be more specific, it happens at these lines:

Intent intent = new Intent(ObjectActivity.this,CameraActivity.class);
startActivityForResult(intent, REQUEST_CAMERA);

这很好用,但是当我在拍照后尝试返回ObjectActivity时,发生这种情况:

This works pretty well, but when I try to return to ObjectActivity after taking a picture, which happens here:

Camera.PictureCallback mPicture = new Camera.PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        //TODO Code to process picture taken
        //create a new intent...
        Intent intent = new Intent();
        intent.putExtra("data",data);
        //close this Activity...
        setResult(Activity.RESULT_OK, intent);
        finish();
    }
};

它返回到名为MainActivity的ObjectActivity之前的活动,而应该返回到ObjectActivity并调用onActivityResult():

It returns to the activity previous to the ObjectActivity named MainActivity, while it's supposed to go back to ObjectActivity and call onActivityResult() :

@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    executeOnResume = false;
    loadStuff();
    if (resultCode == RESULT_OK) {
        if (requestCode == REQUEST_CAMERA || requestCode == REQUEST_SELECT_FILE) {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
            alertDialog.setTitle("Description");
            alertDialog.setMessage("Enter Description");
            final EditText input = new EditText(this);
            alertDialog.setView(input);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT);
            input.setLayoutParams(lp);
            alertDialog.setView(input);
            alertDialog.setPositiveButton("Ok",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            description = input.getText().toString();
                            if (description == null || description.equals("")) {
                                description = "-";
                            }
                            try {
                                savePhoto(requestCode,data);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    });
            final AlertDialog.Builder tmpDialog = alertDialog;

            final AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
            dlgAlert.setTitle("Direction");
            dlgAlert.setMessage("Stand with your phone facing the same direction as the picture made and press Ok");
            dlgAlert.setPositiveButton("Ok",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            mBearingProvider.updateBearing();
                            bearing = mBearingProvider.getBearing();
                            cardinalDirection = bearingToString(bearing);
                            Log.e("Direction", cardinalDirection + "," + bearing);
                            tmpDialog.create().show();
                            dialog.dismiss();
                        }
                    });
            dlgAlert.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            tmpDialog.create().show();
                        }
                    });
            dlgAlert.create().show();
        }
    }
}

但是它永远不会到达那里. 有人知道为什么会这样吗?

But it never gets there. Does anyone know why this happens?

推荐答案

我找到了解决方案,如果我不使用Intent发送byte [](而是写出一个文件并发送路径)带有Intent的文件),代码似乎可以正常工作,并打开ObjectActivity.

I've found the solution, if I don't send over a byte[] with the Intent (but instead maybe write out a file and send the path of that file with the Intent), the code seems to work and the ObjectActivity opens.

我删除/更改的部分:

intent.putExtra("data",data);

我不知道为什么会这样,我想知道为什么,但是现在我很高兴它能成功.

I don't know why this works, I'd like to know why, but for now I'm happy it works.

这篇关于startActivityForResult返回错误的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆