classCamera

Last-modified: 2015-03-18 (水) 12:28:50
public class Camera extends Activity implements View.OnClickListener{
	ImageButton ib;
	Button b;
	ImageView iv;
	Intent i;
	final static int cameraData = 0;
	Bitmap bmp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO 自動生成されたメソッド・スタブ
		super.onCreate(savedInstanceState);
		setContentView(R.layout.photo);
		initialize();
		InputStream is = getResources().openRawResource(R.drawable.ic_launcher);
		bmp = BitmapFactory.decodeStream(is);
	}
	private void initialize() {
		// TODO 自動生成されたメソッド・スタブ
		ib = (ImageButton)findViewById(R.id.ibTakePic);
		b = (Button)findViewById(R.id.bSetWall);
		iv = (ImageView)findViewById(R.id.ivReturnePic);
		b.setOnClickListener(this);
		ib.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO 自動生成されたメソッド・スタブ
		switch(v.getId()){
			case R.id.bSetWall:
				try{
					getApplicationContext().setWallpaper(bmp);
				} catch(IOException e){
					e.printStackTrace();
				}
				break;
			case R.id.ibTakePic:
				i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
				startActivityForResult(i, cameraData);
				break;
		}
	}

	@Override
	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");
			iv.setImageBitmap(bmp);
		}
	}
}

~
~
~<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/ivReturnePic"
        android:layout_width="250dp"
        android:layout_height="250dp"
        android:src="@drawable/b01"
        android:layout_gravity="center"/>
    <ImageButton
        android:id="@+id/ibTakePic"
        android:layout_width="125dp"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:layout_gravity="center" />
    <Button
        android:id="@+id/bSetWall"
        android:layout_width="125dp"
        android:layout_height="wrap_content"
        android:text="Set Wallper"
        android:layout_gravity="center"
       />
~</LinearLayout>