package com.demo;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.Calendar;import java.util.Locale;import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.os.Bundle;import android.os.Environment;import android.provider.MediaStore;import android.text.format.DateFormat;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import android.widget.Toast;public class MyCameraActivity extends Activity {/** Called when the activity is first created. */private Button button;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);button = (Button) findViewById(R.id.button);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubIntent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);startActivityForResult(intent, 1);}});}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubsuper.onActivityResult(requestCode, resultCode, data);if (resultCode == Activity.RESULT_OK) {String sdStatus = Environment.getExternalStorageState();if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 檢測sd是否可用Log.i("TestFile","SD card is not avaiable/writeable right now.");return;}String name = new DateFormat().format("yyyyMMdd_hhmmss",Calendar.getInstance(Locale.CHINA)) + ".jpg";Toast.makeText(this, name, Toast.LENGTH_LONG).show();Bundle bundle = data.getExtras();Bitmap bitmap = (Bitmap) bundle.get("data");// 擷取相機返回的資料,並轉換為Bitmap圖片格式FileOutputStream b = null; //???????????????????????????????為什麼不能直接儲存在系統相簿位置呢????????????File file = new File("/sdcard/myImage/");file.mkdirs();// 建立檔案夾String fileName = "/sdcard/myImage/"+name;try {b = new FileOutputStream(fileName);bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把資料寫入檔案} catch (FileNotFoundException e) {e.printStackTrace();} finally {try {b.flush();b.close();} catch (IOException e) {e.printStackTrace();}}((ImageView) findViewById(R.id.imageView)).setImageBitmap(bitmap);// 將圖片顯示在ImageView裡}}}