在让用户自定义头像或皮肤的时候  可能会采取照相或选取相册等方法来实现用户自定义,这里我们就简单实现一下~

在这里先贴出布局-----仅供参考

activity_main://一张图片而已  很简单

    <ImageView android:layout_width="fill_parent"android:layout_height="fill_parent"android:id="@+id/img_true"/>

activity_select_photo://选择图片   其中有一些图片资源和动画,在文末分享给大家

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center_horizontal" ><LinearLayoutandroid:id="@+id/dialog_layout"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_marginLeft="10dip"android:layout_marginRight="10dip"android:layout_marginBottom="10dp"android:gravity="center_horizontal"android:orientation="vertical" ><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:background="@drawable/select_photo_up_bg"android:orientation="vertical"android:paddingBottom="5dp"android:paddingTop="5dp" ><Buttonandroid:id="@+id/btn_take_photo"android:layout_width="fill_parent"android:layout_height="35dp"android:background="@drawable/select_photo_bg"android:text="@string/paizhaoxuanqu"android:textStyle="bold" /><Viewandroid:layout_width="fill_parent"android:layout_height="0.5px"android:background="#828282" /><Buttonandroid:id="@+id/btn_pick_photo"android:layout_width="fill_parent"android:layout_height="35dp"android:layout_marginTop="0dip"android:background="@drawable/select_photo_bg"android:text="@string/xiangcexuanqu"android:textStyle="bold" /></LinearLayout><Buttonandroid:id="@+id/btn_cancel"android:layout_width="fill_parent"android:layout_height="35dp"android:layout_marginTop="20dip"android:background="@drawable/select_photo_bg"android:paddingBottom="5dp"android:paddingTop="5dp"android:text="@string/exit"android:textColor="#ffff0000"android:textStyle="bold" /></LinearLayout></RelativeLayout>

-----------MainActivity-----------

图片监听事件   点击跳转选择页面使用回调

   /** 选择文件 */public static final int TO_SELECT_PHOTO = 1;//图片选择事件imgtrue.setOnClickListener(new OnClickListener() {Intent intent;@Overridepublic void onClick(View v) {// TODO Auto-generated method stubintent = new Intent(this, SelectPhotoActivity.class);startActivityForResult(intent, TO_SELECT_PHOTO);intent = null;}});

回调方法 接收图片地址  设置给控件

   @SuppressWarnings("deprecation")@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if (resultCode == Activity.RESULT_OK && requestCode == TO_SELECT_PHOTO) {picPath = data.getStringExtra(SelectPhotoActivity.KEY_PHOTO_PATH);Bitmap bm = BitmapFactory.decodeFile(picPath);zoomBitmap = zoomBitmap(bm, 300, 300);imgtrue.setBackgroundDrawable(new BitmapDrawable(bm));}super.onActivityResult(requestCode, resultCode, data);}

将获取的图片按宽高进行缩放

    /*** 将原图按照指定的宽高进行缩放* * @param oldBitmap* @param newWidth* @param newHeight* @return*/private Bitmap zoomBitmap(Bitmap oldBitmap, int newWidth, int newHeight) {int width = oldBitmap.getWidth();int height = oldBitmap.getHeight();float scaleWidth = ((float) newWidth) / width;float scaleHeight = ((float) newHeight) / height;Matrix matrix = new Matrix();matrix.postScale(scaleWidth, scaleHeight);Bitmap newBitmap = Bitmap.createBitmap(width, height, Config.RGB_565);Canvas canvas = new Canvas(newBitmap);canvas.drawBitmap(newBitmap, matrix, null);return newBitmap;}

------------------SelectPhotoActivity------------------

//图片选择
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;public class SelectPhotoActivity extends Activity implements OnClickListener {/** 使用照相机拍照获取图片 */public static final int SELECT_PIC_BY_TACK_PHOTO = 1;/** 使用相册中的图片 */public static final int SELECT_PIC_BY_PICK_PHOTO = 2;/** 开启相机 */private Button btn_take_photo;/** 开启图册 */private Button btn_pick_photo;/** 取消 */private Button btn_cancel;/** 获取到的图片路径 */private String picPath;private Intent lastIntent;private Uri photoUri;/** 从Intent获取图片路径的KEY */public static final String KEY_PHOTO_PATH = "photo_path";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_select_photo);btn_take_photo = (Button) findViewById(R.id.btn_take_photo);btn_pick_photo = (Button) findViewById(R.id.btn_pick_photo);btn_cancel = (Button) findViewById(R.id.btn_cancel);lastIntent = getIntent();btn_take_photo.setOnClickListener(this);btn_pick_photo.setOnClickListener(this);btn_cancel.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn_take_photo : // 开启相机takePhoto();break;case R.id.btn_pick_photo : // 开启图册pickPhoto();break;case R.id.btn_cancel : // 取消操作this.finish();break;default :break;}}private void takePhoto() {//  执行拍照前,应该先判断SD卡是否存在String SDState = Environment.getExternalStorageState();if (SDState.equals(Environment.MEDIA_MOUNTED)) {Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// "android.media.action.IMAGE_CAPTURE"/**** 需要说明一下,以下操作使用照相机拍照,拍照后的图片会存放在相册中的 这里使用的这种方式有一个好处就是获取的图片是拍照后的原图* 如果不实用ContentValues存放照片路径的话,拍照后获取的图片为缩略图不清晰*/ContentValues values = new ContentValues();photoUri = this.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, photoUri);startActivityForResult(intent, SELECT_PIC_BY_TACK_PHOTO);} else {Toast.makeText(getApplicationContext(), "内存卡不存在",Toast.LENGTH_SHORT).show();}}/****  从相册中取图片*/private void pickPhoto() {Intent intent = new Intent();intent.setType("image/*");intent.setAction(Intent.ACTION_GET_CONTENT);startActivityForResult(intent, SELECT_PIC_BY_PICK_PHOTO);}@Overridepublic boolean onTouchEvent(MotionEvent event) {finish();return super.onTouchEvent(event);}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {if (resultCode == Activity.RESULT_OK) {doPhoto(requestCode, data);}super.onActivityResult(requestCode, resultCode, data);}/*** 选择图片后,获取图片的路径*/private void doPhoto(int requestCode, Intent data) {if (requestCode == SELECT_PIC_BY_PICK_PHOTO) {// 从相册取图片,有些手机有异常情况,请注意if (data == null) {Toast.makeText(getApplicationContext(), "选择图片文件出错",Toast.LENGTH_SHORT).show();return;}photoUri = data.getData();if (photoUri == null) {Toast.makeText(getApplicationContext(), "选择图片文件出错",Toast.LENGTH_SHORT).show();return;}}String[] pojo = {MediaStore.Images.Media.DATA};@SuppressWarnings("deprecation")Cursor cursor = managedQuery(photoUri, pojo, null, null, null);if (cursor != null) {int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]);cursor.moveToFirst();picPath = cursor.getString(columnIndex);cursor.close();}if (picPath != null&& (picPath.endsWith(".png") || picPath.endsWith(".PNG")|| picPath.endsWith(".jpg") || picPath.endsWith(".JPG"))) {lastIntent.putExtra(KEY_PHOTO_PATH, picPath);setResult(Activity.RESULT_OK, lastIntent);finish();} else {Toast.makeText(getApplicationContext(), "选择图片文件不正确",Toast.LENGTH_SHORT).show();}}}

其中会使用到intent的跳转,我们可以在跳转期间添加动画以及背景透明:

<activityandroid:name="com.seven.activity.SelectPhotoActivity"android:screenOrientation="portrait"android:theme="@style/DialogStyleBottom" ></activity>

资源分享:

动画anim
push_bottom_in.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- 上下滑入式 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" ><translateandroid:duration="200"android:fromYDelta="100%p"android:toYDelta="0" /></set>

push_bottom_out.xml:

<?xml version="1.0" encoding="utf-8"?>
<!-- 上下滑出式 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" ><translateandroid:duration="200"android:fromYDelta="0"android:toYDelta="50%p" />
</set>

strings.xml资源

<string name="paizhaoxuanqu">拍照选取</string>
<string name="xiangcexuanqu">相册选取</string>
<string name="exit">取消</string>

stype.xml资源

 <!-- 选取照片的Activity的样式风格,采取对话框的风格 --><style name="AnimBottom" parent="@android:style/Animation"><item name="android:windowEnterAnimation">@anim/push_bottom_in</item><item name="android:windowExitAnimation">@anim/push_bottom_out</item></style><style name="DialogStyleBottom" parent="android:Theme.Dialog"><item name="android:windowAnimationStyle">@style/AnimBottom</item><item name="android:windowFrame">@null</item><!-- 边框 --><item name="android:windowIsFloating">false</item><!-- 是否浮现在activity之上 --><item name="android:windowIsTranslucent">true</item><!-- 半透明 --><item name="android:windowNoTitle">true</item><!-- 无标题 --><item name="android:windowBackground">@android:color/transparent</item><!-- 背景透明 --><item name="android:backgroundDimEnabled">true</item><!-- 模糊 --></style>

图片资源:

android调取系统相册和照相机选取图片相关推荐

  1. Android 刷新系统相册

    Android 刷新系统相册 最近在做项目时,发现把照片保存到手机指定路径后,有些手机打开系统相册居然看不到,像三星 S3.小米2.sony lt26i和HTC等部分机型!但是中兴N881f.魅族 3 ...

  2. android调取手机相册或打开相机选择图片并显示

    作为一个android小白,自己想尝试写一个小项目,因此写个小博客记录一下自己的开发历程.这一篇记录自己学习调取手机相册以及打开相机选择图片并显示 示例是采用PopupWindow弹出底部菜单,选择相 ...

  3. android 照片多选,Android: 关于系统相册多选图片的问题

    最近在做毕设,想在调用系统相册的时候直接返回多张图片的地址.我本意是想用尽量简单的方法来解决这个问题,不需要剪裁啊什么的功能,只要可以多选就好.可是百度搜出来的方案基本上全部是自己写一个相册或者调用第 ...

  4. android调用系统相册打开图片不显示,【报Bug】打开相册,不显示图片,选中图片后,app会崩溃...

    产品分类: uniapp/App PC开发环境操作系统: Mac PC开发环境操作系统版本号: 10 HBuilderX类型: 正式 HBuilderX版本号: 2.8.8 手机系统: Android ...

  5. android 浏览指定相册,Android -- 采用系统相册浏览指定路径下照片

    //打开系统相册 Intent intent=new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); s ...

  6. Android调用系统相册、拍照以及裁剪最简单的实现(兼容7.0)

    这里我只实现功能,具体Android 7.0 的一些细节参考 http://blog.csdn.net/lmj623565791/article/details/72859156 具体步骤: 一.在清 ...

  7. android+代码调用+相册+小米,Android调用系统相册选择图片,支持小米4云相册

    用小米4调用系统相册选择照片时,如果云相册功能开启的话.云相册中的图片也会显示在选择列表中.经过测试,选择到云相册中的图片的话,uri的scheme是file,而不再试content.本文支持云相册的 ...

  8. Android 调用系统相册选取视频,过滤视频(兼容小米)

    老规矩先上图,注:我这个是其他类型设备的样式图,小米也一样的 由于小米手机可能对很多地方不见让,当然对调用系统相机时也跟其他设备不太一 样,一般情况下他都是底下弹出一个框,选取是否进入相册或者文件夹 ...

  9. Android获取系统相册图片选中地址,获取手机中的所有图片地址自定义相册

    一.获取手机中的值 1.首先在使用读写sd卡权限 2.获取手机中的所有图片: 注意代码中的getGalleryPhotos(getContentResolver()) 方法获取所有地址 获取所有图片地 ...

最新文章

  1. Day71 分页,cookie and Session
  2. 使用 [funcref boost::pfr::get] 按索引访问结构体字段的测试程序
  3. 除了Kaggle,还有哪些高质量的数据科学竞赛平台?
  4. python简单使用
  5. Android 图文混排 通过webview实现并实现点击图片
  6. 考虑用Task.WhenAll
  7. linux was8.5 配db2数据源配置,DataStage v8.5对于db2 v8数据库的支持问题
  8. 黑产肆虐,AI如何扛起反欺诈大旗
  9. BeautifulSoup库用法总结
  10. java中使用MD5进行加密
  11. java list转json
  12. 精简linux (二)背景图片的设置 网络功能的实现
  13. 如何在基于 Intel 的 Mac 上使用机构恢复密钥?
  14. 任务状态段(Task State Segment)
  15. 远程桌面计算机正在使用,远程桌面使用问题
  16. 模拟登陆广工统一认证系统
  17. 轻奢消费人群画像报告
  18. cs231n---RNN、图像字幕、注意力机制Attention、梯度消失、爆炸、LSTM
  19. Alibaba微服务组件Sentinel
  20. linux添加HP网络打印机,在Ubuntu 18.10系统下安装HP网络打印机的方法

热门文章

  1. 日处理200亿+调用,单机QPS高峰达到4万+,喜马拉雅自研 API 网关架构实践
  2. linux安装redis-4.0.2
  3. 前端 html5页面调用手机打电话,发送短信功能
  4. SSM框架的WEB项目
  5. python json模块有什么用_python json模块如何使用
  6. 2022佛山潭洲陶瓷展召开新闻发布会 推出展会十大重点
  7. 区块链分布式商业模式的特征有哪些?
  8. Nowcoder专项练习:网络基础(一)
  9. linux smartgit客户端,跨平台Git客户端SmartGit 18.1.5发布下载
  10. Windows 10 Enterprise 2015 LTSB 2019_Windows超级牛皮软件卸载工具