简体   繁体   中英

how to display alert dialog box in button if target action fails

I am trying to create an app where the user can take a photo and set it as wallpaper.Now when i click the button and get back without taking a photo and click set wallpaper i want to display an alert dialog box.I have been able to set it when the image capture is not initiated but not when initiated but no image is taken

package com.sagarapp;

import java.io.IOException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

public class Camera extends Activity implements View.OnClickListener{

ImageView img;
ImageButton takepic;
Button setdp;
Intent i;
final static int cameraData = 0;
Bitmap bmp;
final Context context = this;
boolean taken = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.photo);
    initialize();
    takepic.setOnClickListener(this);
    setdp.setOnClickListener(this);

}

private void initialize() {
    // TODO Auto-generated method stub
    img = (ImageView)findViewById(R.id.Ivpic);
    takepic = (ImageButton)findViewById(R.id.Ibcapture);
    setdp = (Button)findViewById(R.id.Bsetdp);
}

@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId())
    {
    case R.id.Ibcapture:
        i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        taken = true;
        startActivityForResult(i,cameraData);
        break;
    case R.id.Bsetdp:
        try {
            if(taken)
                getApplicationContext().setWallpaper(bmp);
            else{
            AlertDialog.Builder alertDialogBulider = new AlerDialog.Builder(context);
            alertDialogBulider.setTitle("Warning!");
            alertDialogBulider.setMessage("No Image Is Available");
            alertDialogBulider.setCancelable(false);
  alertDialogBulider.setNeutralButton("OK", new   DialogInterface.OnClickListener()   {

                @Override
                public void onClick(DialogInterface dialog, int id) {
                    // TODO Auto-generated method stub
                    dialog.cancel();
                }
            });
            AlertDialog alertDialog = alertDialogBulider.create();
            alertDialog.show();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
        }
        break;
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK)
    {
        Bundle extras = data.getExtras();
        bmp = (Bitmap)extras.get("data");
        img.setImageBitmap(bmp);
    }
    else
    {
        AlertDialog.Builder alertDialogBulider = new AlertDialog.Builder(context);
        alertDialogBulider.setTitle("Warning!");
        alertDialogBulider.setMessage("No Photo Is Taken");
        alertDialogBulider.setCancelable(false);
        alertDialogBulider.setNeutralButton("OK", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int id) {
                // TODO Auto-generated method stub
                dialog.cancel();
            }
        });
        AlertDialog alertDialog = alertDialogBulider.create();
        alertDialog.show();

    }
}
}

Your taken boolean variable should only be set to true if you receive a RESULT_OK resultcode in onActivityResult() method. That means that the user actually took a picture and saved it. Remove it from the R.id.Ibcapture View onClick () method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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