android最新的选择图片及上传
来源:互联网 发布:android 数据库加密 编辑:程序博客网 时间:2024/04/29 10:23
首先声明参数:
public static final int TAKE_PHOTO = 2001; public static final int GALLERY = 2002;
//照相
if (hasSDCard()) { getNewPath(); Log.e(TAG, "拍摄的照片路径:" + path); Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE"); cameraIntent.putExtra("scale", true); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), path))); startActivityForResult(cameraIntent, TAKE_PHOTO); }//相册 Intent intentFromGallery = new Intent(); intentFromGallery.setType("image/*"); // 设置文件类型 intentFromGallery.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(intentFromGallery, GALLERY);//onActivityResult(requestCode,resultCode,data)
//在此处来得到拍照返回的结果
switch (requestCode) { case TAKE_PHOTO: if (hasSDCard()) presenter.setHead(FileUtils.getImageAbsolutePath(getActivity(), Uri.fromFile(new File(Environment.getExternalStorageDirectory(), path)))); break; case GALLERY: if (null != data) { String img_url = FileUtils.getImageAbsolutePath(getActivity(), data.getData()); Log.e(TAG, "上传路径为:" + img_url); presenter.setHead(img_url); } break; }
//是否有SD卡
public boolean hasSDCard() { return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); }
重头戏来了:
由于android各个版本得到图片路径不一样,由此使用一个类来根据uri来得到路径
//根据uri来获得路径public class FileUtils { @TargetApi(19) public static String getImageAbsolutePath(Activity context, Uri imageUri) { if (context == null || imageUri == null) return null; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT && DocumentsContract.isDocumentUri(context, imageUri)) { if (isExternalStorageDocument(imageUri)) { String docId = DocumentsContract.getDocumentId(imageUri); String[] split = docId.split(":"); String type = split[0]; if ("primary".equalsIgnoreCase(type)) { return Environment.getExternalStorageDirectory() + "/" + split[1]; } } else if (isDownloadsDocument(imageUri)) { String id = DocumentsContract.getDocumentId(imageUri); Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); return getDataColumn(context, contentUri, null, null); } else if (isMediaDocument(imageUri)) { String docId = DocumentsContract.getDocumentId(imageUri); String[] split = docId.split(":"); 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; } String selection = MediaStore.Images.Media._ID + "=?"; String[] selectionArgs = new String[]{split[1]}; return getDataColumn(context, contentUri, selection, selectionArgs); } } // MediaStore (and general) else if ("content".equalsIgnoreCase(imageUri.getScheme())) { // Return the remote address if (isGooglePhotosUri(imageUri)) return imageUri.getLastPathSegment(); return getDataColumn(context, imageUri, null, null); } // File else if ("file".equalsIgnoreCase(imageUri.getScheme())) { return imageUri.getPath(); } return null; } public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) { Cursor cursor = null; String column = MediaStore.Images.Media.DATA; String[] projection = {column}; try { cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); if (cursor != null && cursor.moveToFirst()) { int index = cursor.getColumnIndexOrThrow(column); return cursor.getString(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()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is Google Photos. */ public static boolean isGooglePhotosUri(Uri uri) { return "com.google.android.apps.photos.content".equals(uri.getAuthority()); } public static String savePicToSdcard(Bitmap bitmap, String path) { String filePath = ""; if (bitmap == null) { return filePath; } else { File destFile = new File(path); OutputStream os = null; try { os = new FileOutputStream(destFile); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os); os.flush(); os.close(); } catch (IOException e) { filePath = ""; } } return filePath; }}
将路径传入presenter中,就可以将图片上传,设置,我用的是univasial-image-loader,就不贴代码了
还有一件重要的事情:在给imageview设置图片的时候,需要在路径前面加一个"file:///",就像:`final String newHead = "file:///" + head;ImageLoader.getInstance().displayImage(newHead , listViewHolder.getIv_vehicle_img());
`
基本的流程就是这样了
0 0
- android最新的选择图片及上传
- Android 选择图片、上传图片
- Android 选择图片裁剪,上传
- Android 选择图片剪切上传
- Android-图片的上传及加载
- Android-图片选择,压缩,上传,加载 的 实例
- Android-图片选择,压缩,上传,加载的实例 (图片选择,压缩实现 )
- jquery实现当修改上传的图片时,默认显示当前图片的名称,当选择新的图片时,显示最新的图片名称
- Android图片选择及裁剪
- Android 图片获取及上传
- Android高仿微信图片选择上传工具
- android 选择图片上传报错
- 仿微信图片选择及多张图片上传
- Android的图片上传
- Android的图片上传
- Android个人中心的头像上传,图片编码及截取
- 在项目中使用最新的ckeditor4.3.1的全过程+增加自己的上传本地图片按钮及将上传图片返回、保存
- dedecms 自定义模型中图片字段无法选择及无法上传问题的解决方案
- Ubuntu 14.04 SNMP安装与配置
- LeetCode 36 Valid Sudoku
- 聊聊Oracle 11g中的char类型使用
- libxml/tree.h file not found
- android 开发的7/365 设计模式复习
- android最新的选择图片及上传
- int 运算符重载
- js与jQuery的DOM操作比较(二)
- R packages: dplyr拾遗
- C/C++中tm结构
- java设计模式简要
- tar打包压缩指定目录文件
- JSONObject.toBean时多个类转换
- listview和imageview实现条目单选效果