RSS

Android Intent for a camera app

27 Jun

Yesterday I checked out the email Intent that was a bit straightforward than this. I am going to discuss this more in another blog post. But for now here is the source code fo the camera app and how it looks.

package com.learning.gilo;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class Camera extends Activity implements View.OnClickListener{
	
	Button wall,take;
	ImageView pic;
	Intent i;
	int cameraData = 0;
	Bitmap bmp;
	
	protected void onCreate(Bundle b){
		super.onCreate(b);
		setContentView(R.layout.camera);
		
		InputStream is = getResources().openRawResource(R.drawable.back_mao);
		bmp = BitmapFactory.decodeStream(is);
		wall = (Button) findViewById(R.id.bSet);
		take = (Button) findViewById(R.id.bTake);
		pic = (ImageView) findViewById(R.id.ivImage);
		
		
		wall.setOnClickListener(this);
		take.setOnClickListener(this);
		
		
	}
	
	public void onClick(View v){
		switch(v.getId()){
		case R.id.bSet:
			try {
				
				/*
				 * This is a how a toast is made check out
				 * https://mycodeandlife.wordpress.com/2012/06/27/making-a-toast/
				 */
				getApplicationContext().setWallpaper(bmp);
				Toast.makeText(getApplicationContext(), "This is a toast that will say \"Wallpaper set\"", Toast.LENGTH_LONG).show();
				
			} catch (IOException e) {
				e.printStackTrace();
			}
			break;
		case R.id.bTake:
			i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
			startActivityForResult(i,cameraData);
			break;
		
		}
}
	
	/*This will enable the menu to show up when you click menu check out
	 * https://mycodeandlife.wordpress.com/
	 */
	public boolean onCreateOptionsMenu(Menu menu){
		super.onCreateOptionsMenu(menu);
		MenuInflater showMenu = getMenuInflater();
		showMenu.inflate(R.menu.nice_menu, menu);
		
		return true;
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode == RESULT_OK){
			Bundle extras = data.getExtras();
			bmp = (Bitmap) extras.get("data");
			pic.setImageBitmap(bmp);
			
		}
		
		
	}
}

Advertisements
 

3 responses to “Android Intent for a camera app

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: