android从相机和相册中获取图片
2016-11-17 17:57
281 查看
之前做项目时有用到拍照获取图片这个功能,针对项目抽取出来了主要的代码,其中一些是来自于网上其他朋友的,在此说明!好废话不多说了。。。
activity_main.xml
MainActivity
ImageTools
PhotoInfo
如果对你有用途别忘了点赞哦!没用途也别喷,小弟刚刚开始写博客回馈社会~
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.simpletakepicture.MainActivity" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拍照" android:id="@+id/take_picture_btn" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="相册" android:id="@+id/album_choice_btn" android:layout_toRightOf="@+id/take_picture_btn" /> <ImageView android:layout_width="200dp" android:layout_height="400dp" android:layout_centerInParent="true" android:id="@+id/image_preview" android:src="@drawable/ic_launcher" /> </RelativeLayout>
MainActivity
package com.example.simpletakepicture; import java.io.File; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; /** * * @author wangyangzi * 拍照获取图片和从相册选择获取图片。不管怎样获取都会封装成一个PhotoInfo对象,包含了图片的sdcard路径,缩略图 * 为的就是上传到服务器时方便查找,通过File转Base64格式上传。本例子不包含上传功能,部分代码来自网上 * */ public class MainActivity extends ActionBarActivity { private Button takePictureBtn; private Button albumChoiceBtn; private ImageView previewImg; private static final int TAKE_PICTURE = 0; private static final int CHOOSE_PICTURE = 1; private static final int SCALE = 5; private static final String tempPhotoDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/wyz/"; private String photoName; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); takePictureBtn = (Button) this.findViewById(R.id.take_picture_btn); albumChoiceBtn = (Button) this.findViewById(R.id.album_choice_btn); previewImg = (ImageView) this.findViewById(R.id.image_preview); //拍照 takePictureBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { Intent tpIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File dir = new File(tempPhotoDir); if (!dir.exists()) { dir.mkdirs(); } //将图片保存进原始目录 photoName = String.valueOf(System.currentTimeMillis()); tpIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(tempPhotoDir + photoName + ".png"))); startActivityForResult(tpIntent, TAKE_PICTURE); } else { Toast.makeText(MainActivity.this, "请确认已经插入SD卡", Toast.LENGTH_LONG).show(); } } }); //相册选择 albumChoiceBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent intent = new Intent(Intent.ACTION_PICK); intent.setType("image/*");//相片类型 startActivityForResult(intent, CHOOSE_PICTURE); } }); } //获取相机或相册的照片 @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { PhotoInfo photoInfo = new PhotoInfo(); Bitmap bitmap = null; String photoPath = ""; try{ if(resultCode != RESULT_CANCELED){ if (requestCode == CHOOSE_PICTURE) { //照片的原始资源地址 Uri originalUri = data.getData(); //使用ContentProvider通过URI获取原始图片 Bitmap photo = MediaStore.Images.Media.getBitmap(getContentResolver(), originalUri); if (photo != null) { //为防止原始图片过大导致内存溢出,这里先缩小原图显示,然后释放原始Bitmap占用的内存 bitmap = ImageTools.zoomBitmap(photo, photo.getWidth() / SCALE, photo.getHeight() / SCALE); //释放原始图片占用的内存,防止out of memory异常发生 photo.recycle(); //获取相册图片的SDCard路径 photoPath = ImageTools.getRealFilePath(this,originalUri); } } else if (requestCode == TAKE_PICTURE ) { BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); bitmapOptions.inSampleSize = 8; int degree = ImageTools.readPictureDegree(tempPhotoDir + photoName + ".png"); bitmap = BitmapFactory.decodeFile(tempPhotoDir + photoName + ".png",bitmapOptions); bitmap = ImageTools.rotaingImageView(degree, bitmap); photoPath = tempPhotoDir + photoName + ".png"; } //构建PhotoInfo对象,此对象可以保存进adapter,用于后续业务处理... photoInfo.setThumbnail(bitmap); photoInfo.setPhotoSDCardPath(photoPath); //预览图片 previewPhoto(photoInfo); } } catch (Exception e) { e.printStackTrace(); } } /** * 预览图片 * @param photoInfo */ private void previewPhoto(PhotoInfo photoInfo){ Toast.makeText(this, "获得图片>>" + photoInfo.getPhotoSDCardPath(), Toast.LENGTH_SHORT).show(); previewImg.setImageBitmap(photoInfo.getThumbnail()); } }
ImageTools
package com.example.simpletakepicture; import java.io.IOException; import android.content.ContentResolver; import android.content.Context; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.Matrix; import android.media.ExifInterface; import android.net.Uri; import android.provider.MediaStore; public class ImageTools { public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) { int w = bitmap.getWidth(); int h = bitmap.getHeight(); Matrix matrix = new Matrix(); float scaleWidth = ((float) width / w); float scaleHeight = ((float) height / h); matrix.postScale(scaleWidth, scaleHeight); Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true); return newbmp; } public static int readPictureDegree(String path) { int degree = 0; try { ExifInterface exifInterface = new ExifInterface(path); int orientation = exifInterface.getAttributeInt( ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); switch (orientation) { case ExifInterface.ORIENTATION_ROTATE_90: degree = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: degree = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: degree = 270; break; } } catch (IOException e) { e.printStackTrace(); } return degree; } public static Bitmap rotaingImageView(int angle, Bitmap bitmap) { // 旋转图片 动作 Matrix matrix = new Matrix(); matrix.postRotate(angle); // 创建新的图片 Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); return resizedBitmap; } public static String getRealFilePath(final Context context, final Uri uri) { if (null == uri) return null; final String scheme = uri.getScheme(); String data = null; if (scheme == null) data = uri.getPath(); else if (ContentResolver.SCHEME_FILE.equals(scheme)) { data = uri.getPath(); } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) { Cursor cursor = context.getContentResolver().query(uri, new String[] { MediaStore.Images.ImageColumns.DATA }, null, null, null); if (null != cursor) { if (cursor.moveToFirst()) { int index = cursor .getColumnIndex(MediaStore.Images.ImageColumns.DATA); if (index > -1) { data = cursor.getString(index); } } cursor.close(); } } return data; } }
PhotoInfo
package com.example.simpletakepicture; import android.graphics.Bitmap; public class PhotoInfo { //图片的真实路径 private String photoSDCardPath; //显示在页面中的缩略图 private Bitmap thumbnail; public Bitmap getThumbnail() { return thumbnail; } public void setThumbnail(Bitmap thumbnail) { this.thumbnail = thumbnail; } public String getPhotoSDCardPath() { return photoSDCardPath; } public void setPhotoSDCardPath(String photoSDCardPath) { this.photoSDCardPath = photoSDCardPath; } }
如果对你有用途别忘了点赞哦!没用途也别喷,小弟刚刚开始写博客回馈社会~
相关文章推荐
- Android图片系列(1)-------调用系统相册与相机获取图片
- Android相机、相册获取图片显示并保存到SD卡 - Ryan's zone 的案例
- android 调用系统相册选取照片或者打开相机拍照获取图片,返回时为什么会回到home界面或者到前一个界面,然后才会跳转到当前activity
- Android开发,调用相册图片或者调用相机进行拍照获取图片
- Android从相册和相机获取图片介绍
- Android相机、相册获取图片显示并保存到SD卡
- Android 从相机或相册获取图片并裁剪
- Android调用相机和相册获取图片并存入数据库(反思)
- Android从相机或相册获取图片裁剪
- Android 从手机相册获取图片 uri 路径 从相机获取照片
- 【Android】使用系统相机或者从相册中获取图片资源并进行适当裁剪
- Android相机、相册获取图片,解决相机拍照图片被压缩模糊的情况
- Android从相机或相册获取图片裁剪
- android中获取手机相机和相册可以传多张图片
- 【移动开发】Android相机、相册获取图片显示并保存到SD卡
- Android相机、相册获取图片显示并保存到SD卡
- Android相机、相册获取图片
- 【移动开发】Android相机、相册获取图片显示并保存到SD卡
- Android 代码片段---从相册或相机获取图片保存并处理
- android 获取相册图片和使用相机照照片 并使用Android自带图片剪裁界面剪裁