Android 调用系统相册选择图片并显示
来源:互联网 发布:优化glsl着色器 编辑:程序博客网 时间:2024/05/18 01:39
主要代码:
package wkk.app2;import android.app.Activity;import android.content.Intent;import android.database.Cursor;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.net.Uri;import android.os.Bundle;import android.provider.MediaStore;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.ImageView;public class MainActivity extends AppCompatActivity { //调用系统相册-选择图片 private static final int IMAGE = 1; //所需权限// <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onClick(View v) { //调用相册 Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, IMAGE); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); //获取图片路径 if (requestCode == IMAGE && resultCode == Activity.RESULT_OK && data != null) { Uri selectedImage = data.getData(); String[] filePathColumns = {MediaStore.Images.Media.DATA}; Cursor c = getContentResolver().query(selectedImage, filePathColumns, null, null, null); c.moveToFirst(); int columnIndex = c.getColumnIndex(filePathColumns[0]); String imagePath = c.getString(columnIndex); showImage(imagePath); c.close(); } } //加载图片 private void showImage(String imaePath){ Bitmap bm = BitmapFactory.decodeFile(imaePath); ((ImageView)findViewById(R.id.image)).setImageBitmap(bm); }}
xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="wkk.app2.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onClick" android:text="点击选择图片" /> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" /></LinearLayout>
2016/12/05 10:17
Demo下载地址:
http://download.csdn.net/detail/w18756901575/9701803
3 0
- Android 调用系统相册选择图片并显示
- Android 调用系统相册选择图片并显示
- Android 调用系统相册选择图片并显示
- Android调用系统相册和相机选择图片并显示在imageview中
- 【Android】调用系统相册选择图片
- 调用Android系统相册并裁剪图片
- android 加载系统相册图片并显示
- Android调用系统相册和相机选择图片显示在界面
- 实践--调用系统相册,裁剪图片并设置圆形显示
- android 开启本地相册选择图片并返回显示
- android 开启本地相册选择图片并返回显示
- android调用系统相册将图片复制到指定目录下并显示复制进度
- Android调用系统相册选择照片并裁剪
- Android从相册选择图片和调用系统摄像头拍照
- android之 h5调用系统相机和相册并显示
- android 调用系统相册并得到图片地址
- 调用系统摄像头和相册选择图片
- Android WebView 选择图片并上传(调用相机拍照/相册/选择文件)
- At Commands:从白痴到大师的修炼历程(一)
- MxNet : use the MxNet windows versioin
- 眼睛区域提取研究
- 注解方式配置Spring实现Ioc
- Python时间转换
- Android 调用系统相册选择图片并显示
- 四大组件Service的工作过程
- C++笔记
- Cf 364div2 [C.尺取法]
- 关于ionic中popup在Android中多次按返回键弹出多次的解决方法
- Python3爬虫学习笔记1.0——什么是爬虫?
- 获取ntoskrnl的基址
- 【内蒙古&山西の游记(7.17~7.24)】二周目
- RabbitMQ持久化和高并发性能的mq