史上最全选择本地图片和拍照上传,超简单解决获取不到图片问题
来源:互联网 发布:网络目标人群分析 编辑:程序博客网 时间:2024/04/27 18:48
相信很多朋友做上传图片的时候都苦恼过获取不到图片,本篇博客解决你的烦恼
这里是选择本地图片try { // 选择本地文件 Intent fileIntent = new Intent( Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); /* 取得相片后返回本画面 */ startActivityForResult(fileIntent, myRequestCode); } catch (Exception e) { LogUtil.e(e); }//这里是拍照上传// 调用系统照相功能 try { // 调用系统摄像头,进行拍照 String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { Intent phoneIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); String saveDir = GlobalParams.SAVE_PATH; File dir = new File(saveDir); if (!dir.exists()) { dir.mkdir(); } file = new File(saveDir, System.currentTimeMillis()+".jpg"); phoneIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); startActivityForResult(phoneIntent, 2); } } catch (Exception e) { LogUtil.e(e); }
下面就开始怎么获取图片了
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); try { if (requestCode == myRequestCode) { Uri uri = data.getData(); if (bitmap != null) { bitmap.recycle(); bitmap = null; file = null; } if(uri != null){ String[] proj = { MediaStore.Images.Media.DATA }; Cursor actualimagecursor = managedQuery(uri, proj, null, null, null); int actual_image_column_index = actualimagecursor .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); actualimagecursor.moveToFirst(); String img_path = actualimagecursor .getString(actual_image_column_index); file = new File(img_path); if (Integer.parseInt(Build.VERSION.SDK) < 14) { actualimagecursor.close(); } ImageSize imageSize = ImageSizeUtil .getImageViewSize(iv_preview); // 2、压缩图片 // 获得图片的宽和高,并不把图片加载到内存中 BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(img_path, options); options.inSampleSize = ImageSizeUtil.caculateInSampleSize( options, imageSize.width, imageSize.height); // 使用获得到的InSampleSize再次解析图片 options.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeFile(img_path, options); }else{ //获取图片 Bundle extras = data.getExtras(); bitmap = (Bitmap) extras.get("data"); } iv_preview.setImageBitmap(bitmap); }else if(requestCode == 2){ if(file != null){ //压缩图片 compressionImage(); } } } catch (Exception e) { Log.e("Exception", e.getMessage(), e); } } /** * 压缩图片 */ private void compressionImage() { /*ImageSize imageSize = ImageSizeUtil .getImageViewSize(iv_preview);*/ // 2、压缩图片 // 获得图片的宽和高,并不把图片加载到内存中 BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(file.getAbsolutePath(), options); /*options.inSampleSize = ImageSizeUtil.caculateInSampleSize( options, imageSize.width, imageSize.height);*/ options.inSampleSize = 8; // 使用获得到的InSampleSize再次解析图片 options.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options); iv_preview.setImageBitmap(bitmap); }
就这么简单,就这么任性搞定选择图片和拍照上传获取不到图片问题
1 0
- 史上最全选择本地图片和拍照上传,超简单解决获取不到图片问题
- Android解决部分机型获取不到系统拍照图片,图库图片问题的解决
- Android获取本地图片或拍照上传
- android拍照和本地选择图片
- android 客户端通过选择本地相册和拍照上传本地图片到服务器端
- 系统拍照和本地图册获取图片
- Android 拍照和从本地获取图片
- Android 拍照上传和拍照本地显示,拍照剪切上传,选取本地图片剪切上传
- android获取拍照图片、本地图片简单实现!
- Android 拍照 以及从本地选择图片 上传
- Android 拍照 以及从本地选择图片 上传
- 从本地图库获取图片和拍照获取图片
- ionic 拍照选择本地图片
- Android拍照,选择本地图片
- Android 拍照获取本地图片
- 手机拍照获取不到图片?<部分手机>
- Android上传本地图片或者拍照上传
- Android 图片拍照上传、本地上传
- HDU 1619 Unidirectional TSP (dp,dfs)
- JavaScript---在html中使用JavaScript(一)
- Convert Sorted List to Binary Search Tree -- leetcodeGiven a singly linked list where elements are s
- Saruman's_Army
- ARM开发板上iconv_open("utf-8", "gb2312") 调用失败的解决方法
- 史上最全选择本地图片和拍照上传,超简单解决获取不到图片问题
- diff patch用法
- CoreData报错:The model used to open the store is incompatible with the one used to create the store
- 第八周项目四--String类的构造
- 路径中 斜杠/和反斜杠\ 的区别
- mysqli_fetch_array() 报错
- android 线程实现异步网络数据获取
- java内存溢出
- 基于XMPP协议的手机多方多端即时通讯方案