在安卓应用开发中经常会用到调用系统相机拍照跟获取本地图片功能,下面就是对这一常用功能的简单实现Demo!
在获取拍照图片功能中要加上这两权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
布局文件
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="com.hhkb.obtainmobilephoto.MainActivity" > 10 11 <Button 12 android:id="@+id/btn_album" 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:text="@string/album" /> 16 17 <Button 18 android:id="@+id/btn_camera" 19 android:layout_width="wrap_content" 20 android:layout_height="wrap_content" 21 android:layout_toRightOf="@id/btn_album" 22 android:text="@string/camera" /> 23 24 <ImageView 25 android:id="@+id/iv_photo" 26 android:layout_width="wrap_content" 27 android:layout_height="wrap_content" 28 android:layout_below="@id/btn_camera" 29 android:src="@drawable/pretty" /> 30 31 </RelativeLayout>
代码
1 public class MainActivity extends Activity implements OnClickListener { 2 3 private static final int CAMERA = 1025; 4 private static final int ALBUM = 1026; 5 6 TextView mBtnAlbum, mBtnCamera; 7 ImageView mIvPhoto; 8 9 private File mFolder; 10 private String mImgName; 11 12 @Override 13 protected void onCreate(Bundle savedInstanceState) { 14 super.onCreate(savedInstanceState); 15 initView(); 16 initData(); 17 } 18 19 private void initView() { 20 setContentView(R.layout.activity_main); 21 mBtnAlbum = (TextView) findViewById(R.id.btn_album); 22 mBtnCamera = (TextView) findViewById(R.id.btn_camera); 23 mIvPhoto = (ImageView) findViewById(R.id.iv_photo); 24 25 } 26 27 private void initData() { 28 mBtnAlbum.setOnClickListener(this); 29 mBtnCamera.setOnClickListener(this); 30 } 31 32 /* 33 * 设置从相机获取图片 34 */ 35 private void getImgFromCamra() { 36 String state = Environment.getExternalStorageState(); 37 // 先检测是不是有内存卡。 38 if (state.equals(Environment.MEDIA_MOUNTED)) { 39 mFolder = new File(Environment.getExternalStorageDirectory(), "bCache"); 40 // 判断手机中有没有这个文件夹,没有就新建。 41 if (!mFolder.exists()) { 42 mFolder.mkdirs(); 43 } 44 // 自定义图片名字,这里是以毫秒数作为图片名。 45 mImgName = System.currentTimeMillis() + ".jpg"; 46 Uri uri = Uri.fromFile(new File(mFolder, mImgName)); 47 // 调用系统拍照功能。 48 Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 49 intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); 50 startActivityForResult(intent, CAMERA); 51 } else { 52 Toast.makeText(this, "未检测到SD卡", Toast.LENGTH_SHORT).show(); 53 } 54 } 55 56 /* 57 * 设置从本地相册获取图片 58 */ 59 private void getImgFromAlbum() { 60 // 调用本地图库。 61 Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 62 intent.setType("image/*"); 63 startActivityForResult(intent, ALBUM); 64 } 65 66 public void onActivityResult(int requestCode, int resultCode, Intent data) { 67 Bitmap bm; 68 if (resultCode == RESULT_OK && requestCode == CAMERA) { 69 // 调用系统方法获取到的是被压缩过的图片,通过自定义路径轻松获取原始图片。 70 bm = BitmapFactory.decodeFile(mFolder.getAbsolutePath() 71 + File.separator + mImgName); 72 mIvPhoto.setImageBitmap(bm); 73 } 74 75 if (resultCode == RESULT_OK && requestCode == ALBUM) { 76 try { 77 if (data != null) { 78 // 获取本地相册图片。 79 Uri uri = data.getData(); 80 ContentResolver cr = getContentResolver(); 81 bm = BitmapFactory.decodeStream(cr.openInputStream(uri)); 82 mIvPhoto.setImageBitmap(bm); 83 } 84 } catch (FileNotFoundException e) { 85 e.printStackTrace(); 86 } 87 } 88 } 89 90 @Override 91 public void onClick(View v) { 92 switch (v.getId()) { 93 case R.id.btn_album: 94 getImgFromAlbum(); 95 break; 96 case R.id.btn_camera: 97 getImgFromCamra(); 98 break; 99 default: 100 break; 101 } 102 103 }; 104 }
时间: 2024-10-07 04:27:04