208_打开相册选择图片并返回
2016-08-28 13:52
204 查看
208_打开相册选择图片并返回
刚刚只是打开了相册
没有进行操作
现在我们把相册里的图片返回过来
然后设置到我们的一个ImageView里面
刚刚只是打开了相册
没有进行操作
现在我们把相册里的图片返回过来
然后设置到我们的一个ImageView里面
//先创建一个intent //给一个action,再给一个uri //action给Intent.ACTION_PICK //uri给android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); //然后设置一个type为"image/*" intent.setType("image/*"); //启动activity startActivityForResult(intent, 0);
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Uri uri = data.getData(); try { Bitmap bitmap = getBitmapFormUri(this, uri); mIv.setImageBitmap(bitmap); } catch (IOException e) { } }
public Bitmap getBitmapFormUri(Activity ac, Uri uri) throws FileNotFoundException, IOException { InputStream input = ac.getContentResolver().openInputStream(uri); BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options(); onlyBoundsOptions.inJustDecodeBounds = true; onlyBoundsOptions.inDither = true;//optional onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;//optional BitmapFactory.decodeStream(input, null, onlyBoundsOptions); input.close(); int originalWidth = onlyBoundsOptions.outWidth; int originalHeight = onlyBoundsOptions.outHeight; if ((originalWidth == -1) || (originalHeight == -1)) return null; //图片分辨率以480x800为标准 float hh = 800f;//这里设置高度为800f float ww = 480f;//这里设置宽度为480f //缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可 int be = 1;//be=1表示不缩放 if (originalWidth > originalHeight && originalWidth > ww) {//如果宽度大的话根据宽度固定大小缩放 be = (int) (originalWidth / ww); } else if (originalWidth < originalHeight && originalHeight > hh) {//如果高度高的话根据宽度固定大小缩放 be = (int) (originalHeight / hh); } if (be <= 0) be = 1; //比例压缩 BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); bitmapOptions.inSampleSize = be;//设置缩放比例 bitmapOptions.inDither = true;//optional bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;//optional input = ac.getContentResolver().openInputStream(uri); Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions); input.close(); return bitmap; }
相关文章推荐
- Android 打开相册,选择一张图片并返回
- android 拍照+从手机相册选择返回图片到imagview
- android 开启本地相册选择图片并返回显示
- unity 打开相册选择图片
- Android 打开相册选择单张图片实现代码
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时回到手机桌面,然后才会跳转到当前activity
- IOS学习:打开照相机与本地相册选择图片
- android 开启本地相册选择图片并返回显示
- Android在选择系统相册图片时,返回的图片地址不同导致程序崩溃!!!
- Qt 打开安卓相册选择图片并获取图片的本地路径
- Android-打开相册选择单张图片
- IOS研究院之打开照相机与本地相册选择图片
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时为什么会回到home界面或者到前一个界面,然后才会跳转到当前activity
- IOS打开照相机与本地相册选择图片
- Android实现选择相册图片和拍照,并手动截图返回图片路径和Bitmap对象
- IOS研究院之打开照相机与本地相册选择图片(六)
- 安卓5.0Intent打开相册选择图片无返回值问题
- 打开系统相册/相机,选择图片
- android 通过Intent打开相册并获取选择的图片
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时回到手机桌面,然后才会跳转到当前activity