实现的功能:当选中照相radiobutton时,单击下面的imagebutton 时 调用系统照相机、并将图片保存到特定目录下;

当选中相册radiobutton时,单击下面的imagebutton 获取系统相册的图片。

xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/camera"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="拍照上传" />

            <RadioButton
                android:id="@+id/gerrly"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="从相册获取" />
        </RadioGroup>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
         >
        
         <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/ic_launcher" />

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:background="@drawable/ic_launcher" />

    
        
    </LinearLayout>
   <ImageButton
        android:id="@+id/imageButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       
        android:background="@drawable/ic_launcher" />
</LinearLayout>

  代码

 

package xiang.camera;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.Toast;


public class MyCameraActivity extends Activity implements OnClickListener {
private ImageButton button1;
private ImageButton button2;
private ImageButton button3;
private RadioButton camera;
private RadioButton gellary;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_camera);
		button1=(ImageButton)findViewById(R.id.imageButton1);
		button2=(ImageButton)findViewById(R.id.imageButton2);
		button3=(ImageButton)findViewById(R.id.imageButton3);
		
		camera =(RadioButton)findViewById(R.id.camera);
		gellary=(RadioButton)findViewById(R.id.gerrly);
		button1.setOnClickListener(this);
		button2.setOnClickListener(this);
		button3.setOnClickListener(this);
		camera.setOnClickListener(this);
		gellary.setOnClickListener(this);
		
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
	
		
		switch (v.getId()) {
		
		case R.id.imageButton1:
		if(camera.isChecked()==true)
		{
			startActivityForResult(intent, 1);

		}
		else if(gellary.isChecked()==true)
		{
			Toast.makeText(this, "从相册获取图片1", Toast.LENGTH_LONG).show();
			  //使用intent调用系统提供的相册功能,使用startActivityForResult是为了获取用户选择的图片

	         systemPhoto(4);
	        
		}
		
			break;
		case R.id.imageButton2:
			if(camera.isChecked()==true)
			{
				startActivityForResult(intent, 2);
//				return ;
			}
			else if(gellary.isChecked()==true)
			{
				Toast.makeText(this, "从相册获取图片2", Toast.LENGTH_LONG).show();
				  systemPhoto(5);
			}
			break;
		case R.id.imageButton3:
			if(camera.isChecked()==true)
			{
				startActivityForResult(intent, 3);

			}
			else if(gellary.isChecked()==true)
			{
				Toast.makeText(this, "从相册获取图片3", Toast.LENGTH_LONG).show();
			}
			break;
		default:
			break;
		}
		
	}
	private void systemPhoto(int i) {  //打开系统相册

		Intent intent = new Intent();

		intent.setType("image/*");

		intent.setAction(Intent.ACTION_GET_CONTENT);

		startActivityForResult(intent, i);
		


	}
@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		String sdState = Environment.getExternalStorageState();
		
		if(!(sdState.equals(Environment.MEDIA_MOUNTED)))
		{
			Toast.makeText(this, "sd is not available/writeable right now ", Toast.LENGTH_SHORT).show();
			return;
		}
		Bitmap bitmap=null;
		FileOutputStream b = null;
		if(requestCode<4)
		{
			Bundle bundle=data.getExtras();
			
			bitmap=(Bitmap) bundle.get("data");
			switch (requestCode) {
			case 1:
				button1.setImageBitmap(bitmap);
				break;
			case 2:
				button2.setImageBitmap(bitmap);
				break;
			case 3:
				button3.setImageBitmap(bitmap);
				break;
			
			default:
				break;
			}
//			File file=new File("/mnt/sdcard/Imagexiang");
//			file.mkdir();
			SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.CHINA);
			
			String str=format.format(new Date());
			String pathname=Environment.getExternalStorageDirectory().getPath()+"/Imagexiang/"+str+".PNG";
			try {
				b=new FileOutputStream(pathname);
				bitmap.compress(Bitmap.CompressFormat.PNG, 0, b);
				//压缩100表示压缩率是0
				
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			finally
			{
				try {
					b.flush();
					b.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
		}
		else if(requestCode>3)
		{
		     ContentResolver resolver = getContentResolver();
		    
		     Uri originalUri = data.getData(); //获得图片的uri
		     String filePathColumn[]={MediaStore.Images.Media.DATA};
		     Cursor cs=resolver.query(originalUri, filePathColumn, null, null, null);
		     cs.moveToFirst();
		     int columnIndex=cs.getColumnIndex(filePathColumn[0]);
		     String filepath=cs.getString(columnIndex);
		 	BitmapFactory.Options options=new BitmapFactory.Options();
			/*
			 * 设置inJustDecodeBounds为true后,decodeFile并不分配空间,但可计算出原始图片的长度和宽度,
			 * 即opts.width和opts.height。
			 * 有了这两个参数,再通过一定的算法,即可得到一个恰当的inSampleSize。
			 */
			options.inJustDecodeBounds=true;
			BitmapFactory.decodeFile(filepath,options);
//			BitmapFactory.decodeResource(res, id, opts)
//			int height=options.outHeight*100 / options.outWidth;
			
			int inSampleSize=8;
			options.inSampleSize=inSampleSize;
			options.inJustDecodeBounds=false;//为decodefile分配内存
			bitmap=BitmapFactory.decodeFile(filepath,options);
		switch (requestCode) {
		case 4:
			
			button1.setImageBitmap(bitmap);
			break;
		case 5:
			button2.setImageBitmap(bitmap);

		default:
			break;
		}
		     

             
		}
		
		


	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.my_camera, menu);
		return true;
	}


}