调取系统相册和照相机选取图片

2023-11-29 09:58

本文主要是介绍调取系统相册和照相机选取图片,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

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

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" >  <LinearLayout  android: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" >  <LinearLayout  android: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" >  <Button  android: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" />  <View  android:layout_width="fill_parent"  android:layout_height="0.5px"  android:background="#828282" />  <Button  android: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>  <Button  android: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-----------

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


<span style="white-space:pre">    </span>private ImageView imgtrue;  /** 选择文件 */  public static final int TO_SELECT_PHOTO = 1;  <span style="white-space:pre">        </span>//图片选择事件  imgtrue.setOnClickListener(new OnClickListener() {  Intent intent;  @Override  public void onClick(View v) {  // TODO Auto-generated method stub  intent = new Intent(this, SelectPhotoActivity.class);  startActivityForResult(intent, TO_SELECT_PHOTO);  intent = null;  }  });  


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

@SuppressWarnings("deprecation")  
@Override  
protected 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";  @Override  protected 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);  }  @Override  public 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);  }  @Override  public boolean onTouchEvent(MotionEvent event) {  finish();  return super.onTouchEvent(event);  }  @Override  protected 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();  }  }  }  

资源分享:

动画anim
push_bottom_in.xml
<?xml version="1.0" encoding="utf-8"?>  
<!-- 上下滑入式 -->  
<set xmlns:android="http://schemas.android.com/apk/res/android" >  <translate  android: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" >  <translate  android:duration="200"  android:fromYDelta="0"  android:toYDelta="50%p" />  
</set>  

图片资源:







这篇关于调取系统相册和照相机选取图片的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/432386

相关文章

利用Python脚本实现批量将图片转换为WebP格式

《利用Python脚本实现批量将图片转换为WebP格式》Python语言的简洁语法和库支持使其成为图像处理的理想选择,本文将介绍如何利用Python实现批量将图片转换为WebP格式的脚本,WebP作为... 目录简介1. python在图像处理中的应用2. WebP格式的原理和优势2.1 WebP格式与传统

linux重启命令有哪些? 7个实用的Linux系统重启命令汇总

《linux重启命令有哪些?7个实用的Linux系统重启命令汇总》Linux系统提供了多种重启命令,常用的包括shutdown-r、reboot、init6等,不同命令适用于不同场景,本文将详细... 在管理和维护 linux 服务器时,完成系统更新、故障排查或日常维护后,重启系统往往是必不可少的步骤。本文

基于 HTML5 Canvas 实现图片旋转与下载功能(完整代码展示)

《基于HTML5Canvas实现图片旋转与下载功能(完整代码展示)》本文将深入剖析一段基于HTML5Canvas的代码,该代码实现了图片的旋转(90度和180度)以及旋转后图片的下载... 目录一、引言二、html 结构分析三、css 样式分析四、JavaScript 功能实现一、引言在 Web 开发中,

Mac系统下卸载JAVA和JDK的步骤

《Mac系统下卸载JAVA和JDK的步骤》JDK是Java语言的软件开发工具包,它提供了开发和运行Java应用程序所需的工具、库和资源,:本文主要介绍Mac系统下卸载JAVA和JDK的相关资料,需... 目录1. 卸载系统自带的 Java 版本检查当前 Java 版本通过命令卸载系统 Java2. 卸载自定

Python如何去除图片干扰代码示例

《Python如何去除图片干扰代码示例》图片降噪是一个广泛应用于图像处理的技术,可以提高图像质量和相关应用的效果,:本文主要介绍Python如何去除图片干扰的相关资料,文中通过代码介绍的非常详细,... 目录一、噪声去除1. 高斯噪声(像素值正态分布扰动)2. 椒盐噪声(随机黑白像素点)3. 复杂噪声(如伪

Python中图片与PDF识别文本(OCR)的全面指南

《Python中图片与PDF识别文本(OCR)的全面指南》在数据爆炸时代,80%的企业数据以非结构化形式存在,其中PDF和图像是最主要的载体,本文将深入探索Python中OCR技术如何将这些数字纸张转... 目录一、OCR技术核心原理二、python图像识别四大工具库1. Pytesseract - 经典O

Python实现精准提取 PDF中的文本,表格与图片

《Python实现精准提取PDF中的文本,表格与图片》在实际的系统开发中,处理PDF文件不仅限于读取整页文本,还有提取文档中的表格数据,图片或特定区域的内容,下面我们来看看如何使用Python实... 目录安装 python 库提取 PDF 文本内容:获取整页文本与指定区域内容获取页面上的所有文本内容获取

Python基于微信OCR引擎实现高效图片文字识别

《Python基于微信OCR引擎实现高效图片文字识别》这篇文章主要为大家详细介绍了一款基于微信OCR引擎的图片文字识别桌面应用开发全过程,可以实现从图片拖拽识别到文字提取,感兴趣的小伙伴可以跟随小编一... 目录一、项目概述1.1 开发背景1.2 技术选型1.3 核心优势二、功能详解2.1 核心功能模块2.

基于Python实现一个简单的题库与在线考试系统

《基于Python实现一个简单的题库与在线考试系统》在当今信息化教育时代,在线学习与考试系统已成为教育技术领域的重要组成部分,本文就来介绍一下如何使用Python和PyQt5框架开发一个名为白泽题库系... 目录概述功能特点界面展示系统架构设计类结构图Excel题库填写格式模板题库题目填写格式表核心数据结构

Go语言如何判断两张图片的相似度

《Go语言如何判断两张图片的相似度》这篇文章主要为大家详细介绍了Go语言如何中实现判断两张图片的相似度的两种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 在介绍技术细节前,我们先来看看图片对比在哪些场景下可以用得到:图片去重:自动删除重复图片,为存储空间"瘦身"。想象你是一个