<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
本文範例為大家分享了Android實現拍照或者選取本地圖片的具體程式碼,供大家參考,具體內容如下
從selectPhotoActivity中啟動圖冊或者相機,再根據獲取的uri進行裁剪,返回uri,再對這個uri執行一系列操縱。
從相簿選取圖片
private void pickPhoto() { Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, SELECT_PIC_BY_PICK_PHOTO); }
使用隱式intent啟動相機。
拍照獲取圖片
private void takePhoto() { // 執行拍照前,應該先判斷SD卡是否存在 String SDState = Environment.getExternalStorageState(); if (SDState.equals(Environment.MEDIA_MOUNTED)) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// "android.media.action.IMAGE_CAPTURE" File path = Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); File file = new File(path, IMAGE_FILE_NAME); takePhoto = Uri.fromFile(file); intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, takePhoto); startActivityForResult(intent, SELECT_PIC_BY_TACK_PHOTO); } else { Toast.makeText(getApplicationContext(), "記憶體卡不存在", Toast.LENGTH_SHORT).show(); } }
值得注意的一點是,intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, takePhoto)中,設定了拍完照照片的存放路徑takePhoto,在此情況下,部分機型的onActivityResult()中不會返回資料,即data.getData()為空,因為可以根據存放路徑即可獲取拍照圖片。
裁剪圖片
public void startPhotoZoom(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // 設定裁剪 intent.putExtra("crop", "true"); // aspectX aspectY 是寬高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX outputY 是裁剪圖片寬高 intent.putExtra("outputX", 340); intent.putExtra("outputY", 340); //將URI指向相應的file:///… intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); // 不返回圖片檔案 intent.putExtra("return-data", false); startActivityForResult(intent, RESULT_REQUEST_CODE); }
裁剪方法呼叫android自帶的裁剪庫,部分深度客製化的機型,如魅族,可能不存在該庫,那麼就需要自定義或者使用開源裁剪庫。
返回的資料的處理
protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode != Activity.RESULT_OK) { Log.e("TAG","ActivityResult resultCode error"); return; } switch (requestCode){ case SELECT_PIC_BY_PICK_PHOTO: Uri uri = data.getData(); if (!TextUtils.isEmpty(uri.getAuthority())) { //查詢選擇圖片 Cursor cursor = getContentResolver().query( uri, new String[] { MediaStore.Images.Media.DATA }, null, null, null); //返回 沒找到選擇圖片 if (null == cursor) { return; } //遊標移動至開頭 獲取圖片路徑 cursor.moveToFirst(); picPath = cursor.getString(cursor .getColumnIndex(MediaStore.Images.Media.DATA)); Log.d("圖片路徑啊啊啊啊啊啊",picPath); } break; case SELECT_PIC_BY_TACK_PHOTO: //裁剪圖片 startPhotoZoom(takePhoto); break; case RESULT_REQUEST_CODE : if (data != null) { Log.d("圖片路徑",data.getData().toString()); picPath = getPathByUri4kitkat(getApplicationContext(),data.getData()); Log.d("圖片路徑啊啊啊啊啊啊",picPath); } break; } if(requestCode != SELECT_PIC_BY_TACK_PHOTO) { lastIntent.putExtra(KEY_PHOTO_PATH, picPath); setResult(Activity.RESULT_OK, lastIntent); finish(); } super.onActivityResult(requestCode, resultCode, data); }
因為在本activity中可能啟動三個新的activity,即拍照activity,相簿activity,裁剪activity。需要注意,拍完照的圖片需要經過裁剪,即,只有從相簿選取和裁剪返回的資料可以setRuselt(),故需要新增一個if語句加以判別。
根據Uri獲取真實路徑
還是因為機型適配的問題,以下提供兩種方法,大家自己嘗試:
方法一
public static String getRealPathFromURI(final Context context, final Uri uri ) { if ( null == uri ) return null; final String scheme = uri.getScheme(); String data = null; if ( scheme == null ) data = uri.getPath(); else if ( ContentResolver.SCHEME_FILE.equals( scheme ) ) { data = uri.getPath(); } else if ( ContentResolver.SCHEME_CONTENT.equals( scheme ) ) { Cursor cursor = context.getContentResolver().query( uri, new String[] { MediaStore.Images.ImageColumns.DATA }, null, null, null ); if ( null != cursor ) { if ( cursor.moveToFirst() ) { int index = cursor.getColumnIndex( MediaStore.Images.ImageColumns.DATA ); if ( index > -1 ) { data = cursor.getString( index ); } } cursor.close(); } } return data; }
方法二
@SuppressLint("NewApi") public static String getPathByUri4kitkat(final Context context, final Uri uri) { final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; // DocumentProvider if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) { if (isExternalStorageDocument(uri)) {// ExternalStorageProvider final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; if ("primary".equalsIgnoreCase(type)) { return Environment.getExternalStorageDirectory() + "/" + split[1]; } } else if (isDownloadsDocument(uri)) {// DownloadsProvider final String id = DocumentsContract.getDocumentId(uri); final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); return getDataColumn(context, contentUri, null, null); } else if (isMediaDocument(uri)) {// MediaProvider final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; Uri contentUri = null; if ("image".equals(type)) { contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; } else if ("video".equals(type)) { contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; } else if ("audio".equals(type)) { contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; } final String selection = "_id=?"; final String[] selectionArgs = new String[] { split[1] }; return getDataColumn(context, contentUri, selection, selectionArgs); } } else if ("content".equalsIgnoreCase(uri.getScheme())) {// MediaStore // (and // general) return getDataColumn(context, uri, null, null); } else if ("file".equalsIgnoreCase(uri.getScheme())) {// File return uri.getPath(); } return null; } /** * Get the value of the data column for this Uri. This is useful for * MediaStore Uris, and other file-based ContentProviders. * * @param context * The context. * @param uri * The Uri to query. * @param selection * (Optional) Filter used in the query. * @param selectionArgs * (Optional) Selection arguments used in the query. * @return The value of the _data column, which is typically a file path. */ public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { Cursor cursor = null; final String column = "_data"; final String[] projection = { column }; try { cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) { final int column_index = cursor.getColumnIndexOrThrow(column); return cursor.getString(column_index); } } finally { if (cursor != null) cursor.close(); } return null; } /** * @param uri * The Uri to check. * @return Whether the Uri authority is ExternalStorageProvider. */ public static boolean isExternalStorageDocument(Uri uri) { return "com.android.externalstorage.documents".equals(uri.getAuthority()); } /** * @param uri * The Uri to check. * @return Whether the Uri authority is DownloadsProvider. */ public static boolean isDownloadsDocument(Uri uri) { return "com.android.providers.downloads.documents".equals(uri.getAuthority()); } /** * @param uri * The Uri to check. * @return Whether the Uri authority is MediaProvider. */ public static boolean isMediaDocument(Uri uri) { return "com.android.providers.media.documents".equals(uri.getAuthority()); }
整體程式碼
public class selectPhotoActivity extends Activity implements View.OnClickListener{ /** 使用照相機拍照獲取圖片 */ public static final int SELECT_PIC_BY_TACK_PHOTO = 1; /** 使用相簿中的圖片 */ public static final int SELECT_PIC_BY_PICK_PHOTO = 2; /** 裁剪圖片 */ private static final int RESULT_REQUEST_CODE = 3; /** 開啟相機 */ private Button btn_take_photo; /** 開啟圖冊 */ private Button btn_pick_photo; /** 取消 */ private Button btn_cancel; /** 圖片名稱 */ private static final String IMAGE_FILE_NAME = "image.jpg"; /** 獲取到的圖片路徑 */ private String picPath; //儲存裁剪後的影象 private Bitmap photo; private Intent lastIntent; private Uri takePhoto; /** 從Intent獲取圖片路徑的KEY */ public static final String KEY_PHOTO_PATH = "photo_path"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_select_photo); btn_take_photo = (Button) findViewById(R.id.btn_take_photo); btn_pick_photo = (Button) findViewById(R.id.btn_pick_photo); btn_cancel = (Button) findViewById(R.id.btn_cancel); lastIntent = getIntent(); btn_take_photo.setOnClickListener(this); btn_pick_photo.setOnClickListener(this); btn_cancel.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_take_photo : // 開啟相機 takePhoto(); break; case R.id.btn_pick_photo : // 開啟圖冊 pickPhoto(); break; case R.id.btn_cancel : // 取消操作 this.finish(); break; default : break; } } /** * 拍照獲取圖片 */ private void takePhoto() { // 執行拍照前,應該先判斷SD卡是否存在 String SDState = Environment.getExternalStorageState(); if (SDState.equals(Environment.MEDIA_MOUNTED)) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// "android.media.action.IMAGE_CAPTURE" File path = Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); File file = new File(path, IMAGE_FILE_NAME); takePhoto = Uri.fromFile(file); intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, takePhoto); startActivityForResult(intent, SELECT_PIC_BY_TACK_PHOTO); } else { Toast.makeText(getApplicationContext(), "記憶體卡不存在", Toast.LENGTH_SHORT).show(); } } /*** * 從相簿中取圖片 */ private void pickPhoto() { Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, SELECT_PIC_BY_PICK_PHOTO); } @Override public boolean onTouchEvent(MotionEvent event) { finish(); return super.onTouchEvent(event); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode != Activity.RESULT_OK) { Log.e("TAG","ActivityResult resultCode error"); return; } switch (requestCode){ case SELECT_PIC_BY_PICK_PHOTO: Uri uri = data.getData(); if (!TextUtils.isEmpty(uri.getAuthority())) { //查詢選擇圖片 Cursor cursor = getContentResolver().query( uri, new String[] { MediaStore.Images.Media.DATA }, null, null, null); //返回 沒找到選擇圖片 if (null == cursor) { return; } //遊標移動至開頭 獲取圖片路徑 cursor.moveToFirst(); picPath = cursor.getString(cursor .getColumnIndex(MediaStore.Images.Media.DATA)); Log.d("圖片路徑啊啊啊啊啊啊",picPath); } break; case SELECT_PIC_BY_TACK_PHOTO: //裁剪圖片 startPhotoZoom(takePhoto); break; case RESULT_REQUEST_CODE : if (data != null) { Log.d("圖片路徑",data.getData().toString()); picPath = getPathByUri4kitkat(getApplicationContext(),data.getData()); Log.d("圖片路徑啊啊啊啊啊啊",picPath); } break; } if(requestCode != SELECT_PIC_BY_TACK_PHOTO) { lastIntent.putExtra(KEY_PHOTO_PATH, picPath); setResult(Activity.RESULT_OK, lastIntent); finish(); } super.onActivityResult(requestCode, resultCode, data); } /** * 裁剪圖片方法實現 * * @param uri */ public void startPhotoZoom(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // 設定裁剪 intent.putExtra("crop", "true"); // aspectX aspectY 是寬高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX outputY 是裁剪圖片寬高 intent.putExtra("outputX", 340); intent.putExtra("outputY", 340); //將URI指向相應的file:///… intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); // 不返回圖片檔案 intent.putExtra("return-data", false); startActivityForResult(intent, RESULT_REQUEST_CODE); } //Android 4.4後通過Uri獲取路徑以及檔名一種方法 public static String getRealPathFromURI(final Context context, final Uri uri ) { if ( null == uri ) return null; final String scheme = uri.getScheme(); String data = null; if ( scheme == null ) data = uri.getPath(); else if ( ContentResolver.SCHEME_FILE.equals( scheme ) ) { data = uri.getPath(); } else if ( ContentResolver.SCHEME_CONTENT.equals( scheme ) ) { Cursor cursor = context.getContentResolver().query( uri, new String[] { MediaStore.Images.ImageColumns.DATA }, null, null, null ); if ( null != cursor ) { if ( cursor.moveToFirst() ) { int index = cursor.getColumnIndex( MediaStore.Images.ImageColumns.DATA ); if ( index > -1 ) { data = cursor.getString( index ); } } cursor.close(); } } return data; } // 專為Android4.4設計的從Uri獲取檔案絕對路徑,以前的方法已不好使 @SuppressLint("NewApi") public static String getPathByUri4kitkat(final Context context, final Uri uri) { final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; // DocumentProvider if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) { if (isExternalStorageDocument(uri)) {// ExternalStorageProvider final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; if ("primary".equalsIgnoreCase(type)) { return Environment.getExternalStorageDirectory() + "/" + split[1]; } } else if (isDownloadsDocument(uri)) {// DownloadsProvider final String id = DocumentsContract.getDocumentId(uri); final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); return getDataColumn(context, contentUri, null, null); } else if (isMediaDocument(uri)) {// MediaProvider final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; Uri contentUri = null; if ("image".equals(type)) { contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; } else if ("video".equals(type)) { contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; } else if ("audio".equals(type)) { contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; } final String selection = "_id=?"; final String[] selectionArgs = new String[] { split[1] }; return getDataColumn(context, contentUri, selection, selectionArgs); } } else if ("content".equalsIgnoreCase(uri.getScheme())) {// MediaStore // (and // general) return getDataColumn(context, uri, null, null); } else if ("file".equalsIgnoreCase(uri.getScheme())) {// File return uri.getPath(); } return null; } /** * Get the value of the data column for this Uri. This is useful for * MediaStore Uris, and other file-based ContentProviders. * * @param context * The context. * @param uri * The Uri to query. * @param selection * (Optional) Filter used in the query. * @param selectionArgs * (Optional) Selection arguments used in the query. * @return The value of the _data column, which is typically a file path. */ public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { Cursor cursor = null; final String column = "_data"; final String[] projection = { column }; try { cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) { final int column_index = cursor.getColumnIndexOrThrow(column); return cursor.getString(column_index); } } finally { if (cursor != null) cursor.close(); } return null; } /** * @param uri * The Uri to check. * @return Whether the Uri authority is ExternalStorageProvider. */ public static boolean isExternalStorageDocument(Uri uri) { return "com.android.externalstorage.documents".equals(uri.getAuthority()); } /** * @param uri * The Uri to check. * @return Whether the Uri authority is DownloadsProvider. */ public static boolean isDownloadsDocument(Uri uri) { return "com.android.providers.downloads.documents".equals(uri.getAuthority()); } /** * @param uri * The Uri to check. * @return Whether the Uri authority is MediaProvider. */ public static boolean isMediaDocument(Uri uri) { return "com.android.providers.media.documents".equals(uri.getAuthority()); } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45