編輯:關於 Android 程式設計
先看成果,如果這正是你想要的效果,那這段程式碼就可以直接參考;實際套用時記得依需求靈活調整。

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/take_photo" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Take Photo" /> <Button android:id="@+id/get_photo" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="get Photo" /> <ImageView android:id="@+id/picture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" /> </LinearLayout>
package com.example.choosepictest;
import java.io.File; import java.io.IOException; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; 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; public class MainActivity extends Activity implements OnClickListener { public static final int TAKE_PHOTO = 1; public static final int CROP_PHOTO = 2; public static final int GET_PHOTO = 3; private Button takePhoto; private Button getPhoto; private ImageView picture; private Uri headImgUri; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); takePhoto = (Button) findViewById(R.id.take_photo); getPhoto = (Button) findViewById(R.id.get_photo); picture = (ImageView) findViewById(R.id.picture); takePhoto.setOnClickListener(this); getPhoto.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.take_photo: takePhoto(); break; case R.id.get_photo: getPhoto(); break; default: break; } } // 拍照 private void takePhoto() { File appDir = new File(Environment.getExternalStorageDirectory(), "/etoury/picCache"); if (!appDir.exists()) { appDir.mkdir(); } String fileName = "user_head" + ".jpg"; File outputImage = new File(appDir, fileName); try { if (outputImage.exists()) { outputImage.delete(); } outputImage.createNewFile(); } catch (IOException e) { e.printStackTrace(); } headImgUri = Uri.fromFile(outputImage); Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); intent.putExtra(MediaStore.EXTRA_OUTPUT, headImgUri); startActivityForResult(intent, TAKE_PHOTO); } // 定向到圖片庫 private void getPhoto() { Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, GET_PHOTO); } /** * 裁剪 */ private void crop(Uri uri) { // 裁剪圖片意圖 Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // 下面這個crop=true是設置在開啟的Intent中設置顯示的VIEW可裁剪 intent.putExtra("crop", "true"); intent.putExtra("scale", true);// 去黑邊 // 裁剪框的比例,1:1 intent.putExtra("aspectX", 1);// 輸出是X方向的比例 intent.putExtra("aspectY", 1); // 裁剪後輸出圖片的尺寸大小,不能太大500程序崩潰 intent.putExtra("outputX", 256); intent.putExtra("outputY", 256); // 圖片格式 /* intent.putExtra("outputFormat", "JPEG"); */ intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); // intent.putExtra("noFaceDetection", true);// 取消人臉識別 intent.putExtra("return-data", true);// true:返回uri,false:不返回uri // 同一個地址下 裁剪的圖片覆蓋拍照的圖片 intent.putExtra(MediaStore.EXTRA_OUTPUT, headImgUri); startActivityForResult(intent, CROP_PHOTO); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case GET_PHOTO: if (resultCode == RESULT_OK) { crop(data.getData()); } break; case TAKE_PHOTO: if (resultCode == RESULT_OK) { crop(headImgUri); } break; case CROP_PHOTO: if (resultCode == RESULT_OK) { Bitmap cropbitmap = data.getParcelableExtra("data"); picture.setImageBitmap(cropbitmap); } break; default: break; } } }
1. 拍照會回傳一張圖,可以是完整原始尺寸的版本。
2. 拍照回傳的是圖片路徑,也就是某個目錄下的檔案。
3. 裁剪後的輸出路徑,會覆蓋掉原本全尺寸圖片的位置。
4. 相簿的 intent 回傳的是 Uri,不是字串。
5. 裁剪的輸出路徑不能覆蓋相簿回傳的 Uri,這一點特別容易出錯,務必留意。
Android 自定義控件屬性詳解:TypedArray 與 attrs 的用法全流程
近來在研究 Android 自定義控件的屬性,把 TypedArray 和 attrs 這套機制徹底摸了一遍,收穫頗多。本文就把整個流程梳理清楚,後續還會有實戰應用的續篇
SlidingMenu 入門:三種集成方式與實作解析
SlidingMenu 是一個廣受歡迎的側滑選單開源庫。本文整理了三種常見的集成方式,並附上完整程式碼,方便快速上手。 方式一:手動建立並附加 這種做法的步驟如下: 1
Android 在圖片上疊加可拖動文字:從需求變通到截圖合成
這個 Demo 的起點是個相當無奈的需求:用戶拍完照分享到微信,同時想附一段備註,而業務方希望能拿到用戶在微信彈窗裡輸入的內容,存進自己的資料庫。 問題在於,那段文字對
用 ViewPager 打造新手引導頁:動態加載指示器實作
來聊個老生常談的話題——ViewPager,估計大家都用過它來做新手引導頁。這次的主題乍看老套,但用法可能和你以往見到的不太一樣,從標題你就該猜到重點了:動態加載指示器。