经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android实现拍照或者选取本地图片
来源:jb51  时间:2022/3/29 13:47:06  对本文有异议

本文实例为大家分享了Android实现拍照或者选取本地图片的具体代码,供大家参考,具体内容如下

总体流程

从selectPhotoActivity中启动图册或者相机,再根据获取的uri进行裁剪,返回uri,再对这个uri执行一系列操纵。

从相册选取图片

  1. private void pickPhoto() {
  2. ? ? ? ? Intent intent = new Intent(Intent.ACTION_PICK,
  3. ? ? ? ? ? ? ? ? android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  4. ? ? ? ? startActivityForResult(intent, SELECT_PIC_BY_PICK_PHOTO);
  5. ? ? }

使用隐式intent启动相机。

拍照获取图片

  1. private void takePhoto() {
  2. ? ? ? ? // 执行拍照前,应该先判断SD卡是否存在
  3. ? ? ? ? String SDState = Environment.getExternalStorageState();
  4. ? ? ? ? if (SDState.equals(Environment.MEDIA_MOUNTED)) {
  5. ? ? ? ? ? ? Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// "android.media.action.IMAGE_CAPTURE"
  6. ? ? ? ? ? ? File path = Environment
  7. ? ? ? ? ? ? ? ? ? ? .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
  8. ? ? ? ? ? ? File file = new File(path, IMAGE_FILE_NAME);
  9. ? ? ? ? ? ? takePhoto = Uri.fromFile(file);
  10. ? ? ? ? ? ? intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, takePhoto);
  11. ? ? ? ? ? ? startActivityForResult(intent, SELECT_PIC_BY_TACK_PHOTO);
  12. ? ? ? ? } else {
  13. ? ? ? ? ? ? Toast.makeText(getApplicationContext(), "内存卡不存在",
  14. ? ? ? ? ? ? ? ? ? ? Toast.LENGTH_SHORT).show();
  15. ? ? ? ? }
  16. ? ? }

值得注意的一点是,intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, takePhoto)中,设置了拍完照照片的存放路径takePhoto,在此情况下,部分机型的onActivityResult()中不会返回数据,即data.getData()为空,因为可以根据存放路径即可获取拍照图片。

裁剪图片

  1. public void startPhotoZoom(Uri uri) {
  2. ? ? ? ? Intent intent = new Intent("com.android.camera.action.CROP");
  3. ? ? ? ? intent.setDataAndType(uri, "image/*");
  4. ? ? ? ? // 设置裁剪
  5. ? ? ? ? intent.putExtra("crop", "true");
  6. ? ? ? ? // aspectX aspectY 是宽高的比例
  7. ? ? ? ? intent.putExtra("aspectX", 1);
  8. ? ? ? ? intent.putExtra("aspectY", 1);
  9. ? ? ? ? // outputX outputY 是裁剪图片宽高
  10. ? ? ? ? intent.putExtra("outputX", 340);
  11. ? ? ? ? intent.putExtra("outputY", 340);
  12. ? ? ? ? //将URI指向相应的file:///…
  13. ? ? ? ? intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
  14. ? ? ? ? // 不返回图片文件
  15. ? ? ? ? intent.putExtra("return-data", false);
  16. ? ? ? ? startActivityForResult(intent, RESULT_REQUEST_CODE);
  17. ? ? }

裁剪方法调用android自带的裁剪库,部分深度定制的机型,如魅族,可能不存在该库,那么就需要自定义或者使用开源裁剪库。

返回的数据的处理

  1. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  2. ? ? ? ? if (resultCode != Activity.RESULT_OK) {
  3. ? ? ? ? ? ? Log.e("TAG","ActivityResult resultCode error");
  4. ? ? ? ? ? ? return;
  5. ? ? ? ? }
  6.  
  7. ? ? ? ? switch (requestCode){
  8. ? ? ? ? ? ? case SELECT_PIC_BY_PICK_PHOTO:
  9. ? ? ? ? ? ? ? ? Uri uri = data.getData();
  10. ? ? ? ? ? ? ? ? if (!TextUtils.isEmpty(uri.getAuthority())) {
  11. ? ? ? ? ? ? ? ? ? ? //查询选择图片
  12. ? ? ? ? ? ? ? ? ? ? Cursor cursor = getContentResolver().query(
  13. ? ? ? ? ? ? ? ? ? ? ? ? uri,
  14. ? ? ? ? ? ? ? ? ? ? ? ? new String[] { MediaStore.Images.Media.DATA },
  15. ? ? ? ? ? ? ? ? ? ? ? ? null,
  16. ? ? ? ? ? ? ? ? ? ? ? ? null,
  17. ? ? ? ? ? ? ? ? ? ? ? ? null);
  18. ? ? ? ? ? ? ? ? ? ? //返回 没找到选择图片
  19. ? ? ? ? ? ? ? ? ? ? if (null == cursor) {
  20. ? ? ? ? ? ? ? ? ? ? ? ? return;
  21. ? ? ? ? ? ? ? ? ? ? }
  22. ? ? ? ? ? ? ? ? ? ? //光标移动至开头 获取图片路径
  23. ? ? ? ? ? ? ? ? ? ? cursor.moveToFirst();
  24. ? ? ? ? ? ? ? ? ? ? picPath = cursor.getString(cursor
  25. ? ? ? ? ? ? ? ? ? ? ? ? .getColumnIndex(MediaStore.Images.Media.DATA));
  26. ? ? ? ? ? ? ? ? ? ? Log.d("图片路径啊啊啊啊啊啊",picPath);
  27. ? ? ? ? ? ? ? ? }
  28. ? ? ? ? ? ? ? ? break;
  29. ? ? ? ? ? ? case SELECT_PIC_BY_TACK_PHOTO:
  30. ? ? ? ? ? ? ? ? //裁剪图片
  31. ? ? ? ? ? ? ? ? startPhotoZoom(takePhoto);
  32. ? ? ? ? ? ? ? ? break;
  33. ? ? ? ? ? ? case RESULT_REQUEST_CODE :
  34. ? ? ? ? ? ? ? ? if (data != null) {
  35. ? ? ? ? ? ? ? ? ? ? Log.d("图片路径",data.getData().toString());
  36. ? ? ? ? ? ? ? ? ? ? picPath = getPathByUri4kitkat(getApplicationContext(),data.getData());
  37. ? ? ? ? ? ? ? ? ? ? Log.d("图片路径啊啊啊啊啊啊",picPath);
  38. ? ? ? ? ? ? ? ? }
  39. ? ? ? ? ? ? ? ? break;
  40. ? ? ? ? }
  41. ? ? ? ? if(requestCode != SELECT_PIC_BY_TACK_PHOTO) {
  42. ? ? ? ? ? ? lastIntent.putExtra(KEY_PHOTO_PATH, picPath);
  43. ? ? ? ? ? ? setResult(Activity.RESULT_OK, lastIntent);
  44. ? ? ? ? ? ? finish();
  45. ? ? ? ? }
  46. ? ? ? ? super.onActivityResult(requestCode, resultCode, data);
  47.  
  48. ? ? }

因为在本activity中可能启动三个新的activity,即拍照activity,相册activity,裁剪activity。需要注意,拍完照的图片需要经过裁剪,即,只有从相册选取和裁剪返回的数据可以setRuselt(),故需要添加一个if语句加以判别。

根据Uri获取真实路径

还是因为机型适配的问题,以下提供两种方法,大家自己尝试:

方法一

  1. public static String getRealPathFromURI(final Context context, final Uri uri ) {
  2. ? ? ? ? if ( null == uri ) return null;
  3. ? ? ? ? final String scheme = uri.getScheme();
  4. ? ? ? ? String data = null;
  5. ? ? ? ? if ( scheme == null )
  6. ? ? ? ? ? ? data = uri.getPath();
  7. ? ? ? ? else if ( ContentResolver.SCHEME_FILE.equals( scheme ) ) {
  8. ? ? ? ? ? ? data = uri.getPath();
  9. ? ? ? ? } else if ( ContentResolver.SCHEME_CONTENT.equals( scheme ) ) {
  10. ? ? ? ? ? ? Cursor cursor = context.getContentResolver().query( uri, new String[] { MediaStore.Images.ImageColumns.DATA }, null, null, null );
  11. ? ? ? ? ? ? if ( null != cursor ) {
  12. ? ? ? ? ? ? ? ? if ( cursor.moveToFirst() ) {
  13. ? ? ? ? ? ? ? ? ? ? int index = cursor.getColumnIndex( MediaStore.Images.ImageColumns.DATA );
  14. ? ? ? ? ? ? ? ? ? ? if ( index > -1 ) {
  15. ? ? ? ? ? ? ? ? ? ? ? ? data = cursor.getString( index );
  16. ? ? ? ? ? ? ? ? ? ? }
  17. ? ? ? ? ? ? ? ? }
  18. ? ? ? ? ? ? ? ? cursor.close();
  19. ? ? ? ? ? ? }
  20. ? ? ? ? }
  21. ? ? ? ? return data;
  22. ? ? }

方法二

  1. @SuppressLint("NewApi")
  2. ? ? public static String getPathByUri4kitkat(final Context context, final Uri uri) {
  3. ? ? ? ? final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
  4. ? ? ? ? // DocumentProvider
  5. ? ? ? ? if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
  6. ? ? ? ? ? ? if (isExternalStorageDocument(uri)) {// ExternalStorageProvider
  7. ? ? ? ? ? ? ? ? final String docId = DocumentsContract.getDocumentId(uri);
  8. ? ? ? ? ? ? ? ? final String[] split = docId.split(":");
  9. ? ? ? ? ? ? ? ? final String type = split[0];
  10. ? ? ? ? ? ? ? ? if ("primary".equalsIgnoreCase(type)) {
  11. ? ? ? ? ? ? ? ? ? ? return Environment.getExternalStorageDirectory() + "/" + split[1];
  12. ? ? ? ? ? ? ? ? }
  13. ? ? ? ? ? ? } else if (isDownloadsDocument(uri)) {// DownloadsProvider
  14. ? ? ? ? ? ? ? ? final String id = DocumentsContract.getDocumentId(uri);
  15. ? ? ? ? ? ? ? ? final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
  16. ? ? ? ? ? ? ? ? ? ? ? ? Long.valueOf(id));
  17. ? ? ? ? ? ? ? ? return getDataColumn(context, contentUri, null, null);
  18. ? ? ? ? ? ? } else if (isMediaDocument(uri)) {// MediaProvider
  19. ? ? ? ? ? ? ? ? final String docId = DocumentsContract.getDocumentId(uri);
  20. ? ? ? ? ? ? ? ? final String[] split = docId.split(":");
  21. ? ? ? ? ? ? ? ? final String type = split[0];
  22. ? ? ? ? ? ? ? ? Uri contentUri = null;
  23. ? ? ? ? ? ? ? ? if ("image".equals(type)) {
  24. ? ? ? ? ? ? ? ? ? ? contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  25. ? ? ? ? ? ? ? ? } else if ("video".equals(type)) {
  26. ? ? ? ? ? ? ? ? ? ? contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
  27. ? ? ? ? ? ? ? ? } else if ("audio".equals(type)) {
  28. ? ? ? ? ? ? ? ? ? ? contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
  29. ? ? ? ? ? ? ? ? }
  30. ? ? ? ? ? ? ? ? final String selection = "_id=?";
  31. ? ? ? ? ? ? ? ? final String[] selectionArgs = new String[] { split[1] };
  32. ? ? ? ? ? ? ? ? return getDataColumn(context, contentUri, selection, selectionArgs);
  33. ? ? ? ? ? ? }
  34. ? ? ? ? } else if ("content".equalsIgnoreCase(uri.getScheme())) {// MediaStore
  35. ? ? ? ? ? ? // (and
  36. ? ? ? ? ? ? // general)
  37. ? ? ? ? ? ? return getDataColumn(context, uri, null, null);
  38. ? ? ? ? } else if ("file".equalsIgnoreCase(uri.getScheme())) {// File
  39. ? ? ? ? ? ? return uri.getPath();
  40. ? ? ? ? }
  41. ? ? ? ? return null;
  42. ? ? }
  43.  
  44. ? ? /**
  45. ? ? ?* Get the value of the data column for this Uri. This is useful for
  46. ? ? ?* MediaStore Uris, and other file-based ContentProviders.
  47. ? ? ?*
  48. ? ? ?* @param context
  49. ? ? ?* ? ? ? ? ? ?The context.
  50. ? ? ?* @param uri
  51. ? ? ?* ? ? ? ? ? ?The Uri to query.
  52. ? ? ?* @param selection
  53. ? ? ?* ? ? ? ? ? ?(Optional) Filter used in the query.
  54. ? ? ?* @param selectionArgs
  55. ? ? ?* ? ? ? ? ? ?(Optional) Selection arguments used in the query.
  56. ? ? ?* @return The value of the _data column, which is typically a file path.
  57. ? ? ?*/
  58. ? ? public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
  59. ? ? ? ? Cursor cursor = null;
  60. ? ? ? ? final String column = "_data";
  61. ? ? ? ? final String[] projection = { column };
  62. ? ? ? ? try {
  63. ? ? ? ? ? ? cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
  64. ? ? ? ? ? ? if (cursor != null && cursor.moveToFirst()) {
  65. ? ? ? ? ? ? ? ? final int column_index = cursor.getColumnIndexOrThrow(column);
  66. ? ? ? ? ? ? ? ? return cursor.getString(column_index);
  67. ? ? ? ? ? ? }
  68. ? ? ? ? } finally {
  69. ? ? ? ? ? ? if (cursor != null)
  70. ? ? ? ? ? ? ? ? cursor.close();
  71. ? ? ? ? }
  72. ? ? ? ? return null;
  73. ? ? }
  74.  
  75. ? ? /**
  76. ? ? ?* @param uri
  77. ? ? ?* ? ? ? ? ? ?The Uri to check.
  78. ? ? ?* @return Whether the Uri authority is ExternalStorageProvider.
  79. ? ? ?*/
  80. ? ? public static boolean isExternalStorageDocument(Uri uri) {
  81. ? ? ? ? return "com.android.externalstorage.documents".equals(uri.getAuthority());
  82. ? ? }
  83.  
  84. ? ? /**
  85. ? ? ?* @param uri
  86. ? ? ?* ? ? ? ? ? ?The Uri to check.
  87. ? ? ?* @return Whether the Uri authority is DownloadsProvider.
  88. ? ? ?*/
  89. ? ? public static boolean isDownloadsDocument(Uri uri) {
  90. ? ? ? ? return "com.android.providers.downloads.documents".equals(uri.getAuthority());
  91. ? ? }
  92.  
  93. ? ? /**
  94. ? ? ?* @param uri
  95. ? ? ?* ? ? ? ? ? ?The Uri to check.
  96. ? ? ?* @return Whether the Uri authority is MediaProvider.
  97. ? ? ?*/
  98. ? ? public static boolean isMediaDocument(Uri uri) {
  99. ? ? ? ? return "com.android.providers.media.documents".equals(uri.getAuthority());
  100. ? ? }

整体代码

  1. public class selectPhotoActivity extends Activity implements View.OnClickListener{
  2.  
  3. ? ? /** 使用照相机拍照获取图片 */
  4. ? ? public static final int SELECT_PIC_BY_TACK_PHOTO = 1;
  5. ? ? /** 使用相册中的图片 */
  6. ? ? public static final int SELECT_PIC_BY_PICK_PHOTO = 2;
  7. ? ? /** 裁剪图片 */
  8. ? ? private static final int RESULT_REQUEST_CODE = 3;
  9. ? ? /** 开启相机 */
  10. ? ? private Button btn_take_photo;
  11. ? ? /** 开启图册 */
  12. ? ? private Button btn_pick_photo;
  13. ? ? /** 取消 */
  14. ? ? private Button btn_cancel;
  15. ? ? /** 图片名称 */
  16. ? ? private static final String IMAGE_FILE_NAME = "image.jpg";
  17. ? ? /** 获取到的图片路径 */
  18. ? ? private String picPath;
  19. ? ? //保存裁剪后的图像
  20. ? ? private Bitmap photo;
  21. ? ? private Intent lastIntent;
  22. ? ? private Uri takePhoto;
  23. ? ? /** 从Intent获取图片路径的KEY */
  24. ? ? public static final String KEY_PHOTO_PATH = "photo_path";
  25. ? ? @Override
  26. ? ? protected void onCreate(Bundle savedInstanceState) {
  27. ? ? ? ? super.onCreate(savedInstanceState);
  28. ? ? ? ? setContentView(R.layout.activity_select_photo);
  29.  
  30. ? ? ? ? btn_take_photo = (Button) findViewById(R.id.btn_take_photo);
  31. ? ? ? ? btn_pick_photo = (Button) findViewById(R.id.btn_pick_photo);
  32. ? ? ? ? btn_cancel = (Button) findViewById(R.id.btn_cancel);
  33.  
  34. ? ? ? ? lastIntent = getIntent();
  35.  
  36. ? ? ? ? btn_take_photo.setOnClickListener(this);
  37. ? ? ? ? btn_pick_photo.setOnClickListener(this);
  38. ? ? ? ? btn_cancel.setOnClickListener(this);
  39. ? ? }
  40.  
  41. ? ? @Override
  42. ? ? public void onClick(View v) {
  43. ? ? ? ? switch (v.getId()) {
  44. ? ? ? ? ? ? case R.id.btn_take_photo : // 开启相机
  45. ? ? ? ? ? ? ? ? takePhoto();
  46. ? ? ? ? ? ? ? ? break;
  47. ? ? ? ? ? ? case R.id.btn_pick_photo : // 开启图册
  48. ? ? ? ? ? ? ? ? pickPhoto();
  49. ? ? ? ? ? ? ? ? break;
  50. ? ? ? ? ? ? case R.id.btn_cancel : // 取消操作
  51. ? ? ? ? ? ? ? ? this.finish();
  52. ? ? ? ? ? ? ? ? break;
  53. ? ? ? ? ? ? default :
  54. ? ? ? ? ? ? ? ? break;
  55. ? ? ? ? }
  56. ? ? }
  57.  
  58. ? ? /**
  59. ? ? ?* 拍照获取图片
  60. ? ? ?*/
  61. ? ? private void takePhoto() {
  62. ? ? ? ? // 执行拍照前,应该先判断SD卡是否存在
  63. ? ? ? ? String SDState = Environment.getExternalStorageState();
  64. ? ? ? ? if (SDState.equals(Environment.MEDIA_MOUNTED)) {
  65. ? ? ? ? ? ? Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// "android.media.action.IMAGE_CAPTURE"
  66. ? ? ? ? ? ? File path = Environment
  67. ? ? ? ? ? ? ? ? ? ? .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
  68. ? ? ? ? ? ? File file = new File(path, IMAGE_FILE_NAME);
  69. ? ? ? ? ? ? takePhoto = Uri.fromFile(file);
  70. ? ? ? ? ? ? intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, takePhoto);
  71. ? ? ? ? ? ? startActivityForResult(intent, SELECT_PIC_BY_TACK_PHOTO);
  72. ? ? ? ? } else {
  73. ? ? ? ? ? ? Toast.makeText(getApplicationContext(), "内存卡不存在",
  74. ? ? ? ? ? ? ? ? ? ? Toast.LENGTH_SHORT).show();
  75. ? ? ? ? }
  76. ? ? }
  77.  
  78. ? ? /***
  79. ? ? ?* 从相册中取图片
  80. ? ? ?*/
  81. ? ? private void pickPhoto() {
  82. ? ? ? ? Intent intent = new Intent(Intent.ACTION_PICK,
  83. ? ? ? ? ? ? ? ? android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  84. ? ? ? ? startActivityForResult(intent, SELECT_PIC_BY_PICK_PHOTO);
  85. ? ? }
  86.  
  87. ? ? @Override
  88. ? ? public boolean onTouchEvent(MotionEvent event) {
  89. ? ? ? ? finish();
  90. ? ? ? ? return super.onTouchEvent(event);
  91. ? ? }
  92.  
  93. ? ? @Override
  94. ? ? protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  95. ? ? ? ? if (resultCode != Activity.RESULT_OK) {
  96. ? ? ? ? ? ? Log.e("TAG","ActivityResult resultCode error");
  97. ? ? ? ? ? ? return;
  98. ? ? ? ? }
  99.  
  100. ? ? ? ? switch (requestCode){
  101. ? ? ? ? ? ? case SELECT_PIC_BY_PICK_PHOTO:
  102. ? ? ? ? ? ? ? ? Uri uri = data.getData();
  103. ? ? ? ? ? ? ? ? if (!TextUtils.isEmpty(uri.getAuthority())) {
  104. ? ? ? ? ? ? ? ? ? ? //查询选择图片
  105. ? ? ? ? ? ? ? ? ? ? Cursor cursor = getContentResolver().query(
  106. ? ? ? ? ? ? ? ? ? ? ? ? uri,
  107. ? ? ? ? ? ? ? ? ? ? ? ? new String[] { MediaStore.Images.Media.DATA },
  108. ? ? ? ? ? ? ? ? ? ? ? ? null,
  109. ? ? ? ? ? ? ? ? ? ? ? ? null,
  110. ? ? ? ? ? ? ? ? ? ? ? ? null);
  111. ? ? ? ? ? ? ? ? ? ? //返回 没找到选择图片
  112. ? ? ? ? ? ? ? ? ? ? if (null == cursor) {
  113. ? ? ? ? ? ? ? ? ? ? ? ? return;
  114. ? ? ? ? ? ? ? ? ? ? }
  115. ? ? ? ? ? ? ? ? ? ? //光标移动至开头 获取图片路径
  116. ? ? ? ? ? ? ? ? ? ? cursor.moveToFirst();
  117. ? ? ? ? ? ? ? ? ? ? picPath = cursor.getString(cursor
  118. ? ? ? ? ? ? ? ? ? ? ? ? .getColumnIndex(MediaStore.Images.Media.DATA));
  119. ? ? ? ? ? ? ? ? ? ? Log.d("图片路径啊啊啊啊啊啊",picPath);
  120. ? ? ? ? ? ? ? ? }
  121. ? ? ? ? ? ? ? ? break;
  122. ? ? ? ? ? ? case SELECT_PIC_BY_TACK_PHOTO:
  123. ? ? ? ? ? ? ? ? //裁剪图片
  124. ? ? ? ? ? ? ? ? startPhotoZoom(takePhoto);
  125. ? ? ? ? ? ? ? ? break;
  126. ? ? ? ? ? ? case RESULT_REQUEST_CODE :
  127. ? ? ? ? ? ? ? ? if (data != null) {
  128. ? ? ? ? ? ? ? ? ? ? Log.d("图片路径",data.getData().toString());
  129. ? ? ? ? ? ? ? ? ? ? picPath = getPathByUri4kitkat(getApplicationContext(),data.getData());
  130. ? ? ? ? ? ? ? ? ? ? Log.d("图片路径啊啊啊啊啊啊",picPath);
  131. ? ? ? ? ? ? ? ? }
  132. ? ? ? ? ? ? ? ? break;
  133. ? ? ? ? }
  134. ? ? ? ? if(requestCode != SELECT_PIC_BY_TACK_PHOTO) {
  135. ? ? ? ? ? ? lastIntent.putExtra(KEY_PHOTO_PATH, picPath);
  136. ? ? ? ? ? ? setResult(Activity.RESULT_OK, lastIntent);
  137. ? ? ? ? ? ? finish();
  138. ? ? ? ? }
  139. ? ? ? ? super.onActivityResult(requestCode, resultCode, data);
  140.  
  141. ? ? }
  142.  
  143. ? ? /**
  144. ? ? ?* 裁剪图片方法实现
  145. ? ? ?*
  146. ? ? ?* @param uri
  147. ? ? ?*/
  148. ? ? public void startPhotoZoom(Uri uri) {
  149. ? ? ? ? Intent intent = new Intent("com.android.camera.action.CROP");
  150. ? ? ? ? intent.setDataAndType(uri, "image/*");
  151. ? ? ? ? // 设置裁剪
  152. ? ? ? ? intent.putExtra("crop", "true");
  153. ? ? ? ? // aspectX aspectY 是宽高的比例
  154. ? ? ? ? intent.putExtra("aspectX", 1);
  155. ? ? ? ? intent.putExtra("aspectY", 1);
  156. ? ? ? ? // outputX outputY 是裁剪图片宽高
  157. ? ? ? ? intent.putExtra("outputX", 340);
  158. ? ? ? ? intent.putExtra("outputY", 340);
  159. ? ? ? ? //将URI指向相应的file:///…
  160. ? ? ? ? intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
  161. ? ? ? ? // 不返回图片文件
  162. ? ? ? ? intent.putExtra("return-data", false);
  163. ? ? ? ? startActivityForResult(intent, RESULT_REQUEST_CODE);
  164. ? ? }
  165.  
  166. ? ? //Android 4.4后通过Uri获取路径以及文件名一种方法
  167. ? ? public static String getRealPathFromURI(final Context context, final Uri uri ) {
  168. ? ? ? ? if ( null == uri ) return null;
  169. ? ? ? ? final String scheme = uri.getScheme();
  170. ? ? ? ? String data = null;
  171. ? ? ? ? if ( scheme == null )
  172. ? ? ? ? ? ? data = uri.getPath();
  173. ? ? ? ? else if ( ContentResolver.SCHEME_FILE.equals( scheme ) ) {
  174. ? ? ? ? ? ? data = uri.getPath();
  175. ? ? ? ? } else if ( ContentResolver.SCHEME_CONTENT.equals( scheme ) ) {
  176. ? ? ? ? ? ? Cursor cursor = context.getContentResolver().query( uri, new String[] { MediaStore.Images.ImageColumns.DATA }, null, null, null );
  177. ? ? ? ? ? ? if ( null != cursor ) {
  178. ? ? ? ? ? ? ? ? if ( cursor.moveToFirst() ) {
  179. ? ? ? ? ? ? ? ? ? ? int index = cursor.getColumnIndex( MediaStore.Images.ImageColumns.DATA );
  180. ? ? ? ? ? ? ? ? ? ? if ( index > -1 ) {
  181. ? ? ? ? ? ? ? ? ? ? ? ? data = cursor.getString( index );
  182. ? ? ? ? ? ? ? ? ? ? }
  183. ? ? ? ? ? ? ? ? }
  184. ? ? ? ? ? ? ? ? cursor.close();
  185. ? ? ? ? ? ? }
  186. ? ? ? ? }
  187. ? ? ? ? return data;
  188. ? ? }
  189.  
  190. ? ? // 专为Android4.4设计的从Uri获取文件绝对路径,以前的方法已不好使
  191. ? ? @SuppressLint("NewApi")
  192. ? ? public static String getPathByUri4kitkat(final Context context, final Uri uri) {
  193. ? ? ? ? final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
  194. ? ? ? ? // DocumentProvider
  195. ? ? ? ? if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
  196. ? ? ? ? ? ? if (isExternalStorageDocument(uri)) {// ExternalStorageProvider
  197. ? ? ? ? ? ? ? ? final String docId = DocumentsContract.getDocumentId(uri);
  198. ? ? ? ? ? ? ? ? final String[] split = docId.split(":");
  199. ? ? ? ? ? ? ? ? final String type = split[0];
  200. ? ? ? ? ? ? ? ? if ("primary".equalsIgnoreCase(type)) {
  201. ? ? ? ? ? ? ? ? ? ? return Environment.getExternalStorageDirectory() + "/" + split[1];
  202. ? ? ? ? ? ? ? ? }
  203. ? ? ? ? ? ? } else if (isDownloadsDocument(uri)) {// DownloadsProvider
  204. ? ? ? ? ? ? ? ? final String id = DocumentsContract.getDocumentId(uri);
  205. ? ? ? ? ? ? ? ? final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
  206. ? ? ? ? ? ? ? ? ? ? ? ? Long.valueOf(id));
  207. ? ? ? ? ? ? ? ? return getDataColumn(context, contentUri, null, null);
  208. ? ? ? ? ? ? } else if (isMediaDocument(uri)) {// MediaProvider
  209. ? ? ? ? ? ? ? ? final String docId = DocumentsContract.getDocumentId(uri);
  210. ? ? ? ? ? ? ? ? final String[] split = docId.split(":");
  211. ? ? ? ? ? ? ? ? final String type = split[0];
  212. ? ? ? ? ? ? ? ? Uri contentUri = null;
  213. ? ? ? ? ? ? ? ? if ("image".equals(type)) {
  214. ? ? ? ? ? ? ? ? ? ? contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  215. ? ? ? ? ? ? ? ? } else if ("video".equals(type)) {
  216. ? ? ? ? ? ? ? ? ? ? contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
  217. ? ? ? ? ? ? ? ? } else if ("audio".equals(type)) {
  218. ? ? ? ? ? ? ? ? ? ? contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
  219. ? ? ? ? ? ? ? ? }
  220. ? ? ? ? ? ? ? ? final String selection = "_id=?";
  221. ? ? ? ? ? ? ? ? final String[] selectionArgs = new String[] { split[1] };
  222. ? ? ? ? ? ? ? ? return getDataColumn(context, contentUri, selection, selectionArgs);
  223. ? ? ? ? ? ? }
  224. ? ? ? ? } else if ("content".equalsIgnoreCase(uri.getScheme())) {// MediaStore
  225. ? ? ? ? ? ? // (and
  226. ? ? ? ? ? ? // general)
  227. ? ? ? ? ? ? return getDataColumn(context, uri, null, null);
  228. ? ? ? ? } else if ("file".equalsIgnoreCase(uri.getScheme())) {// File
  229. ? ? ? ? ? ? return uri.getPath();
  230. ? ? ? ? }
  231. ? ? ? ? return null;
  232. ? ? }
  233.  
  234. ? ? /**
  235. ? ? ?* Get the value of the data column for this Uri. This is useful for
  236. ? ? ?* MediaStore Uris, and other file-based ContentProviders.
  237. ? ? ?*
  238. ? ? ?* @param context
  239. ? ? ?* ? ? ? ? ? ?The context.
  240. ? ? ?* @param uri
  241. ? ? ?* ? ? ? ? ? ?The Uri to query.
  242. ? ? ?* @param selection
  243. ? ? ?* ? ? ? ? ? ?(Optional) Filter used in the query.
  244. ? ? ?* @param selectionArgs
  245. ? ? ?* ? ? ? ? ? ?(Optional) Selection arguments used in the query.
  246. ? ? ?* @return The value of the _data column, which is typically a file path.
  247. ? ? ?*/
  248. ? ? public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
  249. ? ? ? ? Cursor cursor = null;
  250. ? ? ? ? final String column = "_data";
  251. ? ? ? ? final String[] projection = { column };
  252. ? ? ? ? try {
  253. ? ? ? ? ? ? cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
  254. ? ? ? ? ? ? if (cursor != null && cursor.moveToFirst()) {
  255. ? ? ? ? ? ? ? ? final int column_index = cursor.getColumnIndexOrThrow(column);
  256. ? ? ? ? ? ? ? ? return cursor.getString(column_index);
  257. ? ? ? ? ? ? }
  258. ? ? ? ? } finally {
  259. ? ? ? ? ? ? if (cursor != null)
  260. ? ? ? ? ? ? ? ? cursor.close();
  261. ? ? ? ? }
  262. ? ? ? ? return null;
  263. ? ? }
  264.  
  265. ? ? /**
  266. ? ? ?* @param uri
  267. ? ? ?* ? ? ? ? ? ?The Uri to check.
  268. ? ? ?* @return Whether the Uri authority is ExternalStorageProvider.
  269. ? ? ?*/
  270. ? ? public static boolean isExternalStorageDocument(Uri uri) {
  271. ? ? ? ? return "com.android.externalstorage.documents".equals(uri.getAuthority());
  272. ? ? }
  273.  
  274. ? ? /**
  275. ? ? ?* @param uri
  276. ? ? ?* ? ? ? ? ? ?The Uri to check.
  277. ? ? ?* @return Whether the Uri authority is DownloadsProvider.
  278. ? ? ?*/
  279. ? ? public static boolean isDownloadsDocument(Uri uri) {
  280. ? ? ? ? return "com.android.providers.downloads.documents".equals(uri.getAuthority());
  281. ? ? }
  282.  
  283. ? ? /**
  284. ? ? ?* @param uri
  285. ? ? ?* ? ? ? ? ? ?The Uri to check.
  286. ? ? ?* @return Whether the Uri authority is MediaProvider.
  287. ? ? ?*/
  288. ? ? public static boolean isMediaDocument(Uri uri) {
  289. ? ? ? ? return "com.android.providers.media.documents".equals(uri.getAuthority());
  290. ? ? }
  291.  
  292. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号