public class TakePhotos extends Activity implements android.view.View.OnClickListener { Button takePhoto; Bitmap photo; String picPath; Button capture; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super .onCreate(savedInstanceState); setContentView(R.layout.activity_photo); takePhoto = (Button) findViewById(R.id.button1); capture = (Button) findViewById(R.id.capture); takePhoto.setOnClickListener( this ); capture.setOnClickListener( this ); } @Override public void onClick(View viewid) { switch (viewid.getId()) { case R.id.button1: { // 打開相機 String state = Environment.getExternalStorageState(); // 獲取內存卡可用狀態 if (state.equals(Environment.MEDIA_MOUNTED)) { // 內存卡狀態可用 Intent intent = new Intent( "android.media.action.IMAGE_CAPTURE" ); startActivityForResult(intent, 1 ); } else { // 不可用 Toast.makeText(TakePhotos. this , "內存不可用" , Toast.LENGTH_LONG) .show(); } break ; } case R.id.capture: { // 打開相冊 // 打開本地相冊 Intent i = new Intent( Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); // 設定結果返回 startActivityForResult(i, 2 ); break ; } default : break ; } } @Override protected void onActivityResult( int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super .onActivityResult(requestCode, resultCode, data); if (data != null ) { switch (requestCode) { case 1 : // 兩種方式 獲取拍好的圖片 if (data.getData() != null || data.getExtras() != null ) { // 防止沒有返回結果 Uri uri = data.getData(); if (uri != null ) { this .photo = BitmapFactory.decodeFile(uri.getPath()); // 拿到圖片 } if (photo == null ) { Bundle bundle = data.getExtras(); if (bundle != null ) { photo = (Bitmap) bundle.get( "data" ); FileOutputStream fileOutputStream = null ; try { // 獲取 SD 卡根目錄 生成圖片並 String saveDir = Environment .getExternalStorageDirectory() + "/dhj_Photos" ; // 新建目錄 File dir = new File(saveDir); if (!dir.exists()) dir.mkdir(); // 生成文件名 SimpleDateFormat t = new SimpleDateFormat( "yyyyMMddssSSS" ); String filename = "MT" + (t.format( new Date())) + ".jpg" ; // 新建文件 File file = new File(saveDir, filename); // 打開文件輸出流 fileOutputStream = new FileOutputStream(file); // 生成圖片文件 this .photo.compress(Bitmap.CompressFormat.JPEG, 100 , fileOutputStream); // 相片的完整路徑 this .picPath = file.getPath(); ImageView imageView = (ImageView) findViewById(R.id.imageView1); imageView.setImageBitmap( this .photo); } catch (Exception e) { e.printStackTrace(); } finally { if (fileOutputStream != null ) { try { fileOutputStream.close(); } catch (Exception e) { e.printStackTrace(); } } } Toast.makeText(getApplicationContext(), "獲取到了" , Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(), "找不到圖片" , Toast.LENGTH_SHORT).show(); } } } break ; case 2 : { //打開相冊並選擇照片,這個方式選擇單張 // 獲取返回的數據,這裏是android自定義的Uri地址 Uri selectedImage = data.getData(); String[] filePathColumn = { MediaStore.Images.Media.DATA }; // 獲取選擇照片的數據視圖 Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null , null , null ); cursor.moveToFirst(); // 從數據視圖中獲取已選擇圖片的路徑 int columnIndex = cursor.getColumnIndex(filePathColumn[ 0 ]); String picturePath = cursor.getString(columnIndex); cursor.close(); // 將圖片顯示到界面上 ImageView imageView = (ImageView) findViewById(R.id.imageView1); imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); break ; } default : break ; } } } } |