Android相机、图册demo_安卓软件开发教程-查字典教程网
Android相机、图册demo
Android相机、图册demo
发布时间:2016-12-28 来源:查字典编辑
摘要:本文为大家分享了Android相机、图册基本demo,供大家参考,具体内容如下packagecom.example.democamera;i...

本文为大家分享了Android相机、图册基本demo,供大家参考,具体内容如下

package com.example.democamera; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.res.Configuration; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.view.View; import android.widget.ImageView; public class MainActivity extends Activity { private ImageView iv; static final int gallery = 1, camera = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.iv_picture); } /** * 启动图片画廊 * * @param view */ public void startGallery(View view) { Intent intent = new Intent(); intent.setAction(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); startActivityForResult(intent, gallery); } /** * 启动相机 * * @param view */ public void startCamera(View view) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (isSDExsit()) { /* 创建存放图片文件夹 */ File dir = new File(Environment.getExternalStorageDirectory() + "/my"); if (!dir.exists()) dir.mkdirs(); /* 设置图片参数 得到原尺寸的图片 */ File file = new File(dir, "aaa.jpg"); intent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, Configuration.ORIENTATION_UNDEFINED); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); } startActivityForResult(intent, camera); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case gallery: /* 做判断,防止返回报错 */ if (data != null) { /* * 三种方式处理URI */ Uri uri = data.getData(); iv.setImageURI(uri); Bitmap bitmap = BitmapFactory .decodeFile(getPathByUri(uri, this)); iv.setImageBitmap(bitmap); Bitmap bitmap2 = getBitmapByUri(uri, this); iv.setImageBitmap(bitmap2); } break; case camera: if (data != null) {// 这是获得缩略图的方法 Bitmap b = (Bitmap) data.getExtras().get("data"); iv.setImageBitmap(b); } else {// 有sd卡得到图片原图 Bitmap bitmap = BitmapFactory.decodeFile("sdcard/my/aaa.jpg");//直接这样做会有发生OOM的风险,Demo简单这么处理 iv.setImageBitmap(bitmap); } break; default: break; } } /** * Uri-->Bitmap * * @param uri * @param context * @return */ public static Bitmap getBitmapByUri(Uri uri, Context context) { Bitmap bitmap = null; try { bitmap = MediaStore.Images.Media.getBitmap( context.getContentResolver(), uri); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return bitmap; } /** * Uri--->Path * * @param uri * @param context * @return */ public static String getPathByUri(Uri uri, Context context) { String path = null; String[] projection = { MediaStore.Images.Media.DATA }; Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null); if (cursor.moveToFirst()) { int index = cursor .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); path = cursor.getString(index); } return path; } /** * 判断SD卡是否存在 * * @return */ public static boolean isSDExsit() { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { return true; } else { return false; } } }

下面分享具体Android 调用相机、打开相册、裁剪图片的实现代码,内容如下

private ImageView iv_user_photo; private String fileName = ""; private File tempFile; private int crop = 300;// 裁剪大小 private static final int OPEN_CAMERA_CODE = 10; private static final int OPEN_GALLERY_CODE = 11; private static final int CROP_PHOTO_CODE = 12; private OnClickListener PopupWindowItemOnClick = new OnClickListener() { @Override public void onClick(View v) { menuWindow.dismiss(); switch (v.getId()) { // 拍照 case R.id.btn_camera: initFile(); openCamera(); break; // 相册 case R.id.btn_gallery: initFile(); openGallery(); break; default: break; } } }; public void initFile() { if(fileName.equals("")) { if(FileUtil.existSDCard()) { String path = Environment.getExternalStorageDirectory() + File.separator + "JanuBookingOnline" + File.separator; FileUtil.mkdir(path); Logger.i("path:" + path); fileName = path + "user_head_photo.jpg"; tempFile = new File(fileName); } else { CommonUitl.toast(context, "请插入SD卡"); } } } /** * 调用相机 */ public void openCamera() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 打开相机 intent.putExtra("output", Uri.fromFile(tempFile)); startActivityForResult(intent, OPEN_CAMERA_CODE); } /** * 打开相册 */ public void openGallery() { Intent intent = new Intent(Intent.ACTION_PICK);// 打开相册 intent.setDataAndType(MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image/*"); intent.putExtra("output", Uri.fromFile(tempFile)); startActivityForResult(intent, OPEN_GALLERY_CODE); } /** * 裁剪图片 * @param uri */ public void cropPhoto(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); intent.putExtra("output", Uri.fromFile(tempFile)); intent.putExtra("crop", true); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", crop); intent.putExtra("outputY", crop); startActivityForResult(intent, CROP_PHOTO_CODE); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == 1) return; switch (requestCode) { case OPEN_CAMERA_CODE: cropPhoto(Uri.fromFile(tempFile)); break; case OPEN_GALLERY_CODE: cropPhoto(data.getData()); break; case CROP_PHOTO_CODE: try { BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 2; Bitmap bitmap = BitmapFactory.decodeFile(fileName, options); if (bitmap != null) { iv_user_photo.setImageBitmap(bitmap); CommonUitl.sharedPreferences(context, AppConstants.USER_PHOTO, fileName); } } catch (Exception e) { e.printStackTrace(); } break; default: break; } super.onActivityResult(requestCode, resultCode, data); }

以上就是关于Android相机、图册的基本操作内容,希望对大家学习Android软件编程有所帮助。

相关阅读
推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
  • 大家都在看
  • 小编推荐
  • 猜你喜欢
  • 最新安卓软件开发学习
    热门安卓软件开发学习
    编程开发子分类