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

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

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. 【一步步学小程序】3. 使用自定义组件(component)
  2. 《Oracle系列》:oracle job详解
  3. golang第三方日志包seelog配置文件详解
  4. 做正确的事,正确的做事
  5. SAP Spartacus能够使用的theme
  6. 详细介绍Qt,ffmpeg 和SDl 教程之间的联系
  7. 当不同公司的产品经理在一块聊天,会聊什么?
  8. STM32之FSMC-SRAM/NOR原理
  9. leetcode 121 python(动态规划)
  10. hibernate oracle 读写分离_ASP.NET CORE 国产最火前后端完全分离框架BCVP
  11. 在Vue+springBoot环境中如何实现单点登录(SSO)
  12. f分布表完整图a=0.01_Matlab中的数据分析之概率分布与检验实例讲解
  13. WinRAR 32位解压缩软件 v5.21 汉化免费版
  14. Legacy(传统)BIOS的历史和不足
  15. matlab取平均值不含nan,在Matlab计算中忽略包含NaN条目的向量
  16. 嵌入式三级知识点整理
  17. 应急响应之windows进程排查
  18. QQ邮箱疯狂的附件:别人笑我太疯癫 我笑别人看不穿
  19. 文件存取服务器是用的什么,什么是文件存储?
  20. 【调剂】福建师范大学海峡创新实验室覃弦接收调剂研究生

热门文章

  1. 谷歌眼镜商业模式值得学习:风险转嫁给开发者
  2. pfSense流量限速
  3. nowcoder contest#115 江西财经大学第一届程序设计竞赛 C 今晚吃鸡
  4. 超详细!文献管理软件对比——Endnote、Noteexpress、Zotero、Citavi
  5. 如何激活Windows 10 LTSB
  6. 论文学习笔记 Titanium: A Metadata-Hiding File-Sharing System with Malicious Security
  7. 惠普台式机400-321cn win8换win7操作步骤
  8. (附源码)基于nodejs购物系统app-计算机毕设90766
  9. xgboost / lightgbm for NLP 添加一些 写死的/hardcode 的比如同义词 “特征”/规则
  10. 如何解决航空企业数字化转型中的痛点?