最近看了幾篇關於Android照相機的一些文章,如今總結以下,直接上源代碼把,該說的都用註釋說完了java
package org.android.test; import java.io.File; import android.app.Activity; import android.content.Intent; 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.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; public class Android_mytestActivity extends Activity { /** Called when the activity is first created. */ // 定義一個button打開照相機,定義一個imageview顯示照相機所拍攝的相片; Button but,upload_image; ImageView img; // 獲取sd卡根目錄地址,並建立圖片父目錄文件對象和文件的對象; String file_str = Environment.getExternalStorageDirectory().getPath(); File mars_file = new File(file_str + "/my_camera"); File file_go = new File(file_str + "/my_camera/file.jpg"); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); but = (Button) findViewById(R.id.my_camare_button); upload_image=(Button)findViewById(R.id.upload_image); img = (ImageView) findViewById(R.id.my_img_view); //拍照 but.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub // 驗證sd卡是否正確安裝: if (Environment.MEDIA_MOUNTED.equals(Environment .getExternalStorageState())) { // 先建立父目錄,若是新建立一個文件的時候,父目錄沒有存在,那麼必須先建立父目錄,再新建文件。 if (!mars_file.exists()) { mars_file.mkdirs(); } /*//常規狀況下,咱們這裏會 建立子目錄,但在這裏不用系統拍照完畢後會根據所給的圖片路徑自動去實現; if(!file_go.exists()) { try { file_go.createNewFile(); } catch (IOException e) { }} */ // 設置跳轉的系統拍照的activity爲:MediaStore.ACTION_IMAGE_CAPTURE ; Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // 並設置拍照的存在方式爲外部存儲和存儲的路徑; intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file_go)); //跳轉到拍照界面; startActivityForResult(intent, 0x1); } else { Toast.makeText(Android_mytestActivity.this, "請先安裝好sd卡", Toast.LENGTH_LONG).show(); } } }); //上傳 upload_image.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub if(file_go.exists()) { //驗證圖片存在後就實現,上傳功能,獲得與服務器的輸出流... //什麼URLconnection ,HttpURLconnectio等均可以....... Toast.makeText(Android_mytestActivity.this, "上傳中....", Toast.LENGTH_LONG).show(); } else { Toast.makeText(Android_mytestActivity.this, "請先拍照....", Toast.LENGTH_LONG).show(); } } }); } //拍照結束後顯示圖片; @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub // 判斷請求碼和結果碼是否正確,若是正確的話就在activity上顯示剛剛所拍照的圖片; if (requestCode == 0x1 && resultCode == this.RESULT_OK) { /* 使用BitmapFactory.Options類防止OOM(Out Of Memory)的問題; 建立一個BitmapFactory.Options類用來處理bitmap;*/ BitmapFactory.Options myoptions=new BitmapFactory.Options(); /* 設置Options對象inJustDecodeBounds的屬性爲true,用於在BitmapFactory的 decodeFile(String path, Options opt)後獲取圖片的高和寬; 並且設置了他的屬性值爲true後使用BitmapFactory的decodeFile()方法沒法返回一張 圖片的bitmap對象,僅僅是把圖片的高和寬信息給Options對象; */ myoptions.inJustDecodeBounds=true; BitmapFactory.decodeFile(file_go.getAbsolutePath(),myoptions); //根據在圖片的寬和高,獲得圖片在不變形的狀況指定大小下的縮略圖,設置寬爲222; int height=myoptions.outHeight*222/myoptions.outWidth; myoptions.outWidth=222; myoptions.outHeight=height; //在從新設置玩圖片顯示的高和寬後記住要修改,Options對象inJustDecodeBounds的屬性爲false; //否則沒法顯示圖片; myoptions.inJustDecodeBounds=false; //還沒完這裏纔剛開始,要節約內存還須要幾個屬性,下面是最關鍵的一個; myoptions.inSampleSize=myoptions.outWidth/222; //還能夠設置其餘幾個屬性用於縮小內存; myoptions.inPurgeable=true; myoptions.inInputShareable=true; myoptions.inPreferredConfig=Bitmap.Config.ARGB_4444;// 默認是Bitmap.Config.ARGB_8888 //成功了,下面就顯示圖片咯; Bitmap bitmat = BitmapFactory.decodeFile(file_go.getAbsolutePath(),myoptions); img.setImageBitmap(bitmat); } else { System.out.println("不顯示圖片"); } super.onActivityResult(requestCode, resultCode, data); } }
2.佈局文件linux
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#aaaaaa" android:orientation="vertical" > <Button android:id="@+id/my_camare_button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="點擊拍照" /> <Button android:id="@+id/upload_image" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="上傳相片" /> <ImageView android:id="@+id/my_img_view" android:layout_width="wrap_content" android:layout_height="fill_parent" /> </LinearLayout>
3.主配置文件android
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:Android="http://schemas.android.com/apk/res/android" package="org.android.test" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <!-- <!– 對SD卡進行寫權限 –> --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <!-- 提供建立與刪除文件的權限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".Android_mytestActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 注意必定要加關於文件讀寫的權限,有個權限我沒有加(就是照相機的): <uses-permission android:name="android.permission.CAMERA" /> 由於咱們是跳轉的系統的照相機的activity,因此關於拍照仍然是系統完成的,所以這裏不須要加上這個權限。