我是靠谱客的博主 天真冰淇淋,这篇文章主要介绍Android开发实现从相册中选择照片功能详解,现在分享给大家,希望可以做个参考。

本文实例讲述了Android开发实现从相册中选择照片功能。分享给大家供大家参考,具体如下:

实际效果图:

代码实现:

1. 权限配置
2. 点击事件绑定
3. 相册访问
4. 根据路径设置图片
5. 其他方法

权限

首先,现在 mainfest.xml 文件中添加以下权限:

复制代码
1
2
3
<!--获取照片权限--> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

点击事件

点击跳转相册

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
imageView01.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){ ActivityCompat.requestPermissions(MainActivity.this,new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE },1); } Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, IMAGE_REQUEST_CODE); } });

不同手机返回图片uri不同,此处进行转换

可以不添加( 如果,不添加,则其他方法也没用 )

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@TargetApi(19) private void handleImageOmKitKat(Intent data){ String imagePath = null; Uri uri = data.getData(); if (DocumentsContract.isDocumentUri(this,uri)){ //如果document类型是U日,则通过document id处理 String docId = DocumentsContract.getDocumentId(uri); if ("com.android.providers.media.documents".equals(uri.getAuthority())){ String id = docId.split(":")[1];//解析出数字格式id String selection = MediaStore.Images.Media._ID + "=" + id; imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection); }else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())){ Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),Long.valueOf(docId)); imagePath = getImagePath(contentUri,null); } }else if ("content".equalsIgnoreCase(uri.getScheme())){ //如果是普通类型 用普通方法处理 imagePath = getImagePath(uri,null); }else if ("file".equalsIgnoreCase(uri.getScheme())){ //如果file类型位uri直街获取图片路径即可 imagePath = uri.getPath(); } displayImage(imagePath); }

关于方法:onActivityResult()

在这里通过放回路径设置头像,但由于图片路径生成可能有一定延时,所以这里开一个线程等待:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*定义一个Handler,定义延时执行的行为*/ public void chnage(){ new Thread(){ @Override public void run() { while ( bitmap == null ){ bitmap = BitmapFactory.decodeFile(path); Log.v("qwe","123"); } Message message = handler.obtainMessage(); message.obj = 0; handler.sendMessage(message); } }.start(); }

其他方法:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
private void handleImageBeforeKitKat(Intent data){ Uri uri = data.getData(); String imagePath = getImagePath(uri,null); displayImage(imagePath); } private String getImagePath(Uri uri, String selection){ String path = null; //通过Uri和selection来获取真实图片路径 Cursor cursor = getContentResolver().query(uri, null, selection, null, null); if (cursor != null){ if (cursor.moveToFirst()){ path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); } cursor.close(); } return path; } private void displayImage(String imagePath){ if (imagePath != null){ Bitmap bitmap = BitmapFactory.decodeFile(imagePath); imageView01.setImageBitmap(bitmap); }else { Toast.makeText(MainActivity.this,"fail to get image",Toast.LENGTH_SHORT).show(); } }

相关变量:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//从相册获得图片 Bitmap bitmap; //判断返回到的Activity private static final int IMAGE_REQUEST_CODE = 0; //图片路径 private String path ; private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { if((Integer)msg.obj==0){ imageView01.setImageBitmap(bitmap); } super.handleMessage(msg); } };

Demo源码点击此处本站下载

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android图形与图像处理技巧总结》、《Android开发入门与进阶教程》、《Android调试技巧与常见问题解决方法汇总》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》

希望本文所述对大家Android程序设计有所帮助。

最后

以上就是天真冰淇淋最近收集整理的关于Android开发实现从相册中选择照片功能详解的全部内容,更多相关Android开发实现从相册中选择照片功能详解内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(137)

评论列表共有 0 条评论

立即
投稿
返回
顶部