4ndro1d
4ndro1d

Reputation: 2976

Android does not show dialog in OnActivityResult

I am calling the camera intent and handle the bitmap in the onActivityResult(). I am processing the image via NDK which is working fine. Then I want to open a dialog to present the image, but nothing happens.

   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);
}

Now i figured out that on other machines (not on mine) there is an error thrown:

MainActivity has leaked window that was originally added here

I have the feeling that code from the onCreate() method is sometimes canceling the dialog? Although only UI stuff and some backend communication is started

UPDATE: Fatal signal 11 (SIGSEGV), code 1, fault addr 0x24 in tid 12737 (RenderThread) when running the app on another deivce (faster, SGS6 instead of SGS$). Dialog shows up and I get this error when clicking one of the imageviews

Upvotes: 4

Views: 1423

Answers (3)

Alex Townsend
Alex Townsend

Reputation: 1564

Try setting a flag in OnActivityResult that you need to display the dialog, and check for that flag in onResume, then create your dialog there instead of in OnActivityResult.

More information on why it is a bad idea to commit a fragment transaction (in this case show a dialog) in activity lifecycle method can be found here.

Upvotes: 4

Chaman Saini
Chaman Saini

Reputation: 365

Try to create your dialog like this:

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();

To get your ivWahetLeft

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

Upvotes: 0

Kae10
Kae10

Reputation: 669

If you make to appear your dialog in MainActivity, you may check a flag that your MainActivity is on foreground.

It can be possible like this:

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;
}

Then, check this flag when you show your dialog:

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

Upvotes: 0

Related Questions