Android不会在OnActivityResult中显示对话框

时间:2016-03-22 15:19:21

标签: android alertdialog

我正在调用相机意图并处理onActivityResult()中的位图。我正在通过NDK处理图像,工作正常。然后我想打开一个对话框来呈现图像,但没有任何反应。

   private void startIrisRoutine(Bitmap imageBitmap) {
    File tempDir = new File(getFilesDir() + File.separator + Constants.DIR_TEMP);
    tempDir.mkdirs();

    // create file for taken photo
    final File inputFile = new File(tempDir + File.separator + Constants.FILE_INPUT + Constants.END_JPG);

    // create face part files in temp folder
    final File facePartFace = new File(tempDir + File.separator + Constants.FILE_FACE + Constants.END_PNG);
    final File facePartEyeRight = new File(tempDir + File.separator + Constants.FILE_EYE_RIGHT + Constants.END_PNG);
    final File facePartEyeLeft = new File(tempDir + File.separator + Constants.FILE_EYE_LEFT + Constants.END_PNG);

    //create texture files
    final File textureWahetRightFile = new File(tempDir + File.separator + Constants.FILE_TEXTURE + USITHelper.ALGO_SEG_WAHET_SHORT + Constants.FILE_EYE_RIGHT + Constants.END_PNG);
    final File textureCahtRightFile = new File(tempDir + File.separator + Constants.FILE_TEXTURE + USITHelper.ALGO_SEG_CAHT_SHORT + Constants.FILE_EYE_RIGHT + Constants.END_PNG);
    final File textureWahetLeftFile = new File(tempDir + File.separator + Constants.FILE_TEXTURE + USITHelper.ALGO_SEG_WAHET_SHORT + Constants.FILE_EYE_LEFT + Constants.END_PNG);
    final File textureCahtLeftFile = new File(tempDir + File.separator + Constants.FILE_TEXTURE + USITHelper.ALGO_SEG_CAHT_SHORT + Constants.FILE_EYE_LEFT + Constants.END_PNG);

    // create temp segmentation files
    final File segmentationWahetRightFile = new File(tempDir + File.separator + Constants.FILE_SEGMENTS + USITHelper.ALGO_SEG_WAHET_SHORT + Constants.FILE_EYE_RIGHT + Constants.END_JPG);
    final File segmentationCahtRightFile = new File(tempDir + File.separator + Constants.FILE_SEGMENTS + USITHelper.ALGO_SEG_CAHT_SHORT + Constants.FILE_EYE_RIGHT + Constants.END_JPG);
    final File segmentationWahetLeftFile = new File(tempDir + File.separator + Constants.FILE_SEGMENTS + USITHelper.ALGO_SEG_WAHET_SHORT + Constants.FILE_EYE_LEFT + Constants.END_JPG);
    final File segmentationCahtLeftFile = new File(tempDir + File.separator + Constants.FILE_SEGMENTS + USITHelper.ALGO_SEG_CAHT_SHORT + Constants.FILE_EYE_LEFT + Constants.END_JPG);

    try {
        FileOutputStream fos = new FileOutputStream(inputFile);
        imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    mUSITHelper.findFaceParts(inputFile, facePartFace, facePartEyeLeft, facePartEyeRight);

    mUSITHelper.segmentPicture(facePartEyeLeft, textureWahetLeftFile, segmentationWahetLeftFile, USITHelper.ALGO_SEG_WAHET);
    mUSITHelper.segmentPicture(facePartEyeLeft, textureCahtLeftFile, segmentationCahtLeftFile, USITHelper.ALGO_SEG_CAHT);
    mUSITHelper.segmentPicture(facePartEyeRight, textureWahetRightFile, segmentationWahetRightFile, USITHelper.ALGO_SEG_WAHET);
    mUSITHelper.segmentPicture(facePartEyeRight, textureCahtRightFile, segmentationCahtRightFile, USITHelper.ALGO_SEG_CAHT);

    final AlertDialog.Builder alertadd = new AlertDialog.Builder(this);
    alertadd.setCancelable(false);
    LayoutInflater factory = LayoutInflater.from(this);
    View view = factory.inflate(R.layout.dialog_segmen, null);

    alertadd.setTitle("Only select properly segmented pictures:");

    alertadd.setView(view);
    alertadd.setNegativeButton("Retry", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dlg, int sumthin) {
            dispatchTakePictureIntent();
        }
    });
    alertadd.setPositiveButton("Done", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dlg, int sumthin) {
            dlg.dismiss();
        }
    });
    final Dialog dialog = alertadd.create();
    dialog.show();

    ImageView ivWahetLeft = (ImageView) dialog.findViewById(R.id.ivWahetLeft);
    ivWahetLeft.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            createIrisTemplate(textureWahetLeftFile, segmentationWahetLeftFile, USITHelper.ALGO_SEG_WAHET_SHORT, Constants.EYE_POSITION_LEFT);
            v.setClickable(false);
            v.setEnabled(false);
            ((ImageView) v).setImageBitmap(null);
        }
    });

    final ImageView ivCahtLeft = (ImageView) dialog.findViewById(R.id.ivCahtLeft);
    ivCahtLeft.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            createIrisTemplate(textureCahtLeftFile, segmentationCahtLeftFile, USITHelper.ALGO_SEG_CAHT_SHORT, Constants.EYE_POSITION_LEFT);
            v.setClickable(false);
            v.setEnabled(false);
            ((ImageView) v).setImageBitmap(null);
        }
    });

    ImageView ivWahetRight = (ImageView) dialog.findViewById(R.id.ivWahetRight);
    ivWahetRight.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            createIrisTemplate(textureWahetRightFile, segmentationWahetRightFile, USITHelper.ALGO_SEG_WAHET_SHORT, Constants.EYE_POSITION_RIGHT);
            v.setClickable(false);
            v.setEnabled(false);
            ((ImageView) v).setImageBitmap(null);
        }
    });

    ImageView ivCahtRight = (ImageView) dialog.findViewById(R.id.ivCahtRight);
    ivCahtRight.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            createIrisTemplate(textureCahtRightFile, segmentationCahtRightFile, USITHelper.ALGO_SEG_CAHT_SHORT, Constants.EYE_POSITION_RIGHT);
            v.setClickable(false);
            v.setEnabled(false);
            ((ImageView) v).setImageBitmap(null);
        }
    });

    ImageLoader imageLoader = ImageLoader.getInstance();
    imageLoader.displayImage("file:///" + segmentationWahetLeftFile, ivWahetLeft);
    imageLoader.displayImage("file:///" + segmentationCahtLeftFile, ivCahtLeft);
    imageLoader.displayImage("file:///" + segmentationCahtRightFile, ivCahtRight);
    imageLoader.displayImage("file:///" + segmentationWahetRightFile, ivWahetRight);
}

现在我发现在其他机器上(不在我的机器上)会抛出错误:

MainActivity has leaked window that was originally added here

我觉得onCreate()方法的代码有时会取消对话框吗?虽然只启动了UI内容和一些后端通信

UPDATE :在另一个deivce上运行应用程序时,tid 12737(RenderThread)中的致命信号11(SIGSEGV),代码1,故障地址0x24(更快,SGS6而不是SGS $)。对话框显示,单击其中一个图像视图时出现此错误

3 个答案:

答案 0 :(得分:4)

尝试在OnActivityResult中设置您需要显示对话框的标记,并在onResume中检查该标记,然后在OnActivityResult而不是<tr><td><a ng-href="#">Call us</a> <span class="glyphicon glyphicon-phone" > </span> </td> 中创建对话框。

有关为什么在活动生命周期方法中提交片段事务(在本例中显示对话框)不是一个坏主意的更多信息可以找到here

答案 1 :(得分:0)

如果您要在MainActivity中显示对话框,则可以检查MainActivity位于前台的标记。

可以这样:

boolean isActivityResumed = true;

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    // When activity is in focus, it will be on resumed.
    super.onResume();
    isActivityResumed = true;
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    // When activity is out of focus, it will be on paused.
    super.onPause();
    isActivityResumed = false;
}

然后,在显示对话框时检查此标志:

if (isActivityResumed && dialog != null) {
    dialog.show();
}

答案 2 :(得分:0)

尝试按如下方式创建对话框:

final Dialog dialog = new Dialog(context);
    dialog.setTitle("Only select properly segmented pictures:");
    dialog.setContentView(R.layout.dialog_segmen);
    Button dialogButtonRetry= (Button) dialog.findViewById(R.id.dialogButtonRetry);
    dialogButtonRetry.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dispatchTakePictureIntent();
        }
    });
    Button dialogButtonDone= (Button) dialog.findViewById(R.id.dialogButtonDone);
    dialogButtonDone.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    dialog.show();

获取ivWahetLeft

ImageView ivWahetLeft = (ImageView) dialog.findViewById(R.id.ivWahetLeft);