Payday Loan Payday Loan

Hi.
Once again, this is Bogdan with another article for androidgenuine.com.

Today I will show you a simple but very usefull thing. I will show you how to start anactivityforresult to the camera and how the taken photo will be transformed into a bitmap a seen on an imageview.

The tutorial is simple and pretty short. I have only one class : ImageCapture.class and an xml file : mylayout.xml (like usual, I am too lazy to change the name of the layout).

Here is the Java code for ImageCapture class:

public class ImageCapture extends Activity implements View.OnClickListener{	
	//The declaration part...
	Button b;
	ImageView iv;
	final static int cameraData = 0;
	Bitmap  bmp;
 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mylayout);
		//We call for initialize method
		initialize();
		//We use the inputstream to get the drawable resource
		InputStream is = getResources().openRawResource(R.drawable.ic_launcher);
		bmp = BitmapFactory.decodeStream(is);
	}
	private void initialize() {
		// TODO Auto-generated method stub
		//Stuff with imageview and button, I suppose you know these.
		iv = (ImageView) findViewById(R.id.ivReturnedPic);
		b = (Button) findViewById (R.id.bTakePic);
 
		b.setOnClickListener(this);
	}
	 public void onClick(View v) {
		// TODO Auto-generated method stub
 
		 //Here is the intent that we use to start the Camera
			Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
			//Here we start the activity for result with the unique code "cameraData" and intent i.
			startActivityForResult(i, cameraData);
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		//Check the unique code if it matches with the RESULT_OK
		if (resultCode == RESULT_OK){
			//The bundle where we collect the extras
			Bundle extras = data.getExtras();
			// Here is our bitmap - the photo we'd just taken
			bmp = (Bitmap) extras.get("data");
			//Here we set the bitmap to our imageview
			iv.setImageBitmap(bmp);
		}
	}
 
}

And here is the code for the xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
    <ImageView
        android:id="@+id/ivReturnedPic"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:layout_gravity="center"
        android:layout_marginTop="20dip"
        android:src="@drawable/ic_launcher" />
 
 
 
    <Button
        android:layout_gravity="center"
        android:id="@+id/bTakePic"
        android:layout_width="150dp"
        android:layout_marginTop="10dip"
        android:layout_height="wrap_content"
        android:text="Take picture" />
 
</LinearLayout>

Here is a picture where you can see how the application looks like :

Like always, you can download the entire project from here.

This was all for this tutorial . I hope you enjoyed it and you’ll read my future articles , too. Until next tutorial, bye !

||||| 0 I Like It! |||||

You can pass some info’s that you need to the next Intent by following the next steps:
You can send a String, a int value, a long, char, or any other value just like this:
FirstIntent.java

package com.intents;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
 
 
public class FirstIntent extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.firstintent);
/*when we click this button z...we lunch a new Activity called SecondIntent but we send also 2 values...
a string, and a int declared and initialized below...This values can be whatever you need to transmit to the other Intent..*/
String value1="Example of String";
int value2=3;
 Button z=(Button)findViewById(R.id.Button01);
 
 z.setOnClickListener(new OnClickListener(){
 
			@Override
			public void onClick(View v) {
				 Intent intent = new Intent( getBaseContext(),SecondIntent.class);
 
                                  intent.putExtra("keyword1",value1);
                                  intent.putExtra("keyword2",value2);		
		    startActivity( intent);
 
 
			}}); 
}
}

Code for the SecondIntent.java where we will put these values in same type of variables and we will use them as we want:

package com.intents;
 
import android.app.Activity;
import android.os.Bundle;
 
 
 
public class SecondIntent extends Activity{
		 @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.secondintent);
	          Bundle extras=getIntent().getExtras();
	     String value1=extras.getString("keyword1"));
int value2=extras.getInt("keyword2");}
//we have just loaded our values so we use them and display them in a Toast
 
  Toast.makeText(getBaseContext(), value1, Toast.LENGTH_LONG).show();
  Toast.makeText(getBaseContext(), value2, Toast.LENGTH_LONG).show();
}
}
||||| 4 I Like It! |||||

Search

Popular

Sponsors