Click here to Skip to main content
15,891,019 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
down vote favorite


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

    }
}
}
Posted

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900