Android调用手机自带图库选择图片
来源:互联网 发布:科比2004年总决赛 数据 编辑:程序博客网 时间:2024/04/28 08:46
这里我们采用的布局文件中 有一个ImageView(set_pic)和Button,布局较为简单(这里就不再给出)。其中Button用于打开手机自带图库进行选择图片,而ImageView就用于显示选中的文件。
Button注册了点击事件监听器,内部代码如下:
// 调用android自带的图库Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);startActivityForResult(intent, ActivityRequestCode.SHOW_MAP_DEPOT);
其中ActivityRequestCode.SHOW_MAP_DEPOT只是一个常量,作为请求码,表示活动:
public interface ActivityRequestCode {public static final int SHOW_MAP_DEPOT = 1; //显示Android自带图库,用于选择用户自己的图片}其中调用android自带图库的那个活动还需添加一个回调方法,用于接收自带图库返回的图片路径:
public void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if (data != null) {if (requestCode == ActivityRequestCode.SHOW_MAP_DEPOT&& resultCode == Activity.RESULT_OK)showYourPic(data);}}
// 调用android自带图库,显示选中的图片private void showYourPic(Intent data) {Uri selectedImage = data.getData();String[] filePathColumn = { MediaStore.Images.Media.DATA };Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);cursor.moveToFirst();int columnIndex = cursor.getColumnIndex(filePathColumn[0]);String picturePath = cursor.getString(columnIndex);cursor.close();if (picturePath.equals(""))return;pic_path = picturePath; // 保存所添加的图片的路径// 缩放图片, width, height 按相同比例缩放图片BitmapFactory.Options options = new BitmapFactory.Options();// options 设为true时,构造出的bitmap没有图片,只有一些长宽等配置信息,但比较快,设为false时,才有图片options.inJustDecodeBounds = true;Bitmap bitmap = BitmapFactory.decodeFile(picturePath, options);int scale = (int) (options.outWidth / (float) 300);if (scale <= 0)scale = 1;options.inSampleSize = scale;options.inJustDecodeBounds = false;bitmap = BitmapFactory.decodeFile(picturePath, options);set_pic.setImageBitmap(bitmap);set_pic.setMaxHeight(350);set_pic.setVisibility(ImageView.VISIBLE);}这样我们就可以使用android自带图库选择图片了。
2 0
- Android调用手机自带图库选择图片
- android 调用自带的图库查看图片
- Android调用手机图库选择图片并调用手机的图片裁剪工具
- android 从手机自带图库选取图片作为应用背景图
- android 调用图库中选择的图片
- android 自带图库
- 怎么调用系统自带的图库看图片
- android 图片剪裁 ,从android自带图库提取的
- Android 开发 调用图库选择图片实现和参数详解
- Android中 调用图库 选择 图片的参数理解
- Android 开发 调用图库选择图片实现和参数详解
- android 调用图库并显示选择的图片
- Android调用图库选择本地图片的功能
- Android 开发 调用图库选择图片实现和参数详解
- Android中 调用图库选择图片的参数详解
- 调用Android中相机或图库选择图片并剪切
- 照相录像(调用系统自带+调用系统图库与裁剪图片)
- 调用Android手机自带的闪光灯
- 《编程常见数据结构知识》系列技术文章整理收藏
- 工作日志--mfcButton和线程
- poj 3274 Gold Balanced Lineup Hash
- codeforces 394C Dominoes
- CentOS6.x 升级 CentOS7.1
- Android调用手机自带图库选择图片
- 关于使用python logging模块的几点总结
- 解决Win7 64位玩游戏不兼容的问题
- 《编程实用算法实现整理》系列技术文章整理收藏
- 非智力因素与成功
- org.apache.hadoop.security.AccessControlException: Permission denied: 权限错误
- 权限设计解决方案
- maven初识
- 设计一个算法,求非空二叉树b的宽度(即具有节点最多的那一层的节点个数)