Android中相机和相册使用分析

Android中相机和相册使用分析

欢迎转载,但请尊重原创(文章来自不易,转载请标明转载出处,谢谢)

在手机应用程序中,使用自带的相机拍照以及相册选择喜欢的图片是最常见不过的用户需求,那么怎么合理使用相机和相册来选择照片是重要的,下面就以项目中实际需求为例进行说明,这里实现的功能如下:

1 使用相机和相册选择图片,并裁剪较小图片(常用于剪裁小图)

2 使用相机和相册选择图片,并裁剪较大图片(常用于裁剪大图)

具体的实现功能清楚了,那么就一一进行说明,具体如下(这里不会罗列怎么上传图片到服务端,只介绍怎么使用裁剪和使用相册和相机)。另外,有图有真相,我的实现效果图如下所示:

 

上面为我的实现效果图,接下来按照实现次序列出源代码文件,具体如下:

Popup.java( 弹窗属性设置对象类):

public class Popup {

private int xPos;// 弹出窗口的x方向位置

private int yPos;// 弹出窗口的y方向位置

private int vWidth;// 窗口显示内容的视图宽度

private int vHeight;// 窗口显示内容的视图高度

private int animFadeInOut;// 窗口显示动画

private int contentView;// 潜入在窗口的视图

private View customView;// 潜入的窗口视图view

private boolean isClickable;// 视图外部是否可以点击

private OnDismissListener listener;// 监听弹窗是否dismiss

private OnTouchListener touchListener;// 监听触摸位置

private float bgAlpha;// 背景遮罩的透明度

public int getxPos() {

return xPos;

}

public void setxPos(int xPos) {

this.xPos = xPos;

}

public int getyPos() {

return yPos;

}

public void setyPos(int ypos) {

this.yPos = ypos;

}

public int getvWidth() {

return vWidth;

}

public void setvWidth(int vWidth) {

this.vWidth = vWidth;

}

public int getvHeight() {

return vHeight;

}

public void setvHeight(int vHeight) {

this.vHeight = vHeight;

}

public int getAnimFadeInOut() {

return animFadeInOut;

}

public void setAnimFadeInOut(int animFadeInOut) {

this.animFadeInOut = animFadeInOut;

}

public int getContentView() {

return contentView;

}

public void setContentView(int contentView) {

this.contentView = contentView;

}

public boolean isClickable() {

return isClickable;

}

public void setClickable(boolean isClickable) {

this.isClickable = isClickable;

}

public View getCustomView() {

return customView;

}

public void setCustomView(View customView) {

this.customView = customView;

}

public OnDismissListener getListener() {

return listener;

}

public void setListener(OnDismissListener listener) {

this.listener = listener;

}

public float getBgAlpha() {

return bgAlpha;

}

public void setBgAlpha(float bgAlpha) {

this.bgAlpha = bgAlpha;

}

public OnTouchListener getTouchListener() {

return touchListener;

}

public void setTouchListener(OnTouchListener touchListener) {

this.touchListener = touchListener;

}

PopupDialog.java(弹窗实体类):

public class PopupDialog extends PopupWindow {

public PopupDialog(View view,int width,int height) {

super(view,width,height);

}

}

ViewUtils.java(自定义弹窗工具类):

public class ViewUtils {

private static PopupDialog popupDialog = null;

@SuppressLint("NewApi")

public static PopupDialog createPopupDialog(Context context,Popup dialog) {

dismissPopupDialog();

View view = null;

if(null == dialog.getCustomView()) {

LayoutInflater inflater = LayoutInflater.from(context);

view = inflater.inflate(dialog.getContentView(), null);

} else {

view = dialog.getCustomView();

}

view.setOnTouchListener(dialog.getTouchListener());

if(0 != dialog.getBgAlpha()) {

view.setAlpha(dialog.getBgAlpha());

}

popupDialog = new PopupDialog(view,dialog.getvWidth(),dialog.getvHeight());

ColorDrawable dw = new ColorDrawable(Color.TRANSPARENT);// follow two
lines is used for back key -00000

popupDialog.setBackgroundDrawable(dw);

popupDialog.setAnimationStyle(dialog.getAnimFadeInOut());

popupDialog.setOutsideTouchable(dialog.isClickable());

popupDialog.setFocusable(true);// not allow user click popupwindow background
event or not permit

popupDialog.setOnDismissListener(dialog.getListener());

popupDialog.update();

return popupDialog;

}

public static void dismissPopupDialog() {

if(null != popupDialog &&

popupDialog.isShowing()) {

popupDialog.dismiss();

popupDialog = null;

}

}

public static boolean isPopupShowing() {

if(null != popupDialog &&

popupDialog.isShowing()) {

return true;

} else {

return false;

}

}

view_cameraalbum_popup_menus.xml(导航大小图片裁剪弹窗布局):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent">

<FrameLayout

android:id="@+id/flMaskLayer"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#000000"/>

<LinearLayout

android:id="@+id/llHeader"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginLeft="10dp"

android:layout_marginRight="10dp"

android:orientation="vertical"

android:layout_alignParentBottom="true">

<RelativeLayout

android:layout_width="match_parent"

android:layout_height="101.0dp"

android:background="@drawable/corners_bk_white"

>

<TextView

android:id="@+id/tvSmallImage"

android:layout_width="match_parent"

android:layout_height="50dp"

android:text="裁剪小图"

android:textSize="14sp"

android:textColor="#5084FE"

android:gravity="center"

/>

<View

android:layout_width="match_parent"

android:layout_height="1dp"

android:background="#cccccc"

android:layout_centerVertical="true"

/>

<TextView

android:id="@+id/tvBigImage"

android:layout_width="match_parent"

android:layout_height="50dp"

android:text="裁剪大图"

android:textSize="14sp"

android:textColor="#5084FE"

android:gravity="center"

android:layout_below="@id/tvSmallImage"

/>

</RelativeLayout>

<TextView

android:id="@+id/tvCancel"

android:layout_width="match_parent"

android:layout_height="50dp"

android:text="取消"

android:textColor="#5084FE"

android:background="@drawable/corners_bk_white"

android:gravity="center"

android:layout_marginTop="20dp"

android:textSize="14sp"

android:layout_marginBottom="15dp"

/>

</LinearLayout>

</RelativeLayout>

view_cameraalbum_popup_smallimage.xml(裁剪小图页面弹窗布局):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent">

<FrameLayout

android:id="@+id/flMaskLayer"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#000000"/>

<LinearLayout

android:id="@+id/llHeader"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginLeft="10dp"

android:layout_marginRight="10dp"

android:orientation="vertical"

android:layout_alignParentBottom="true">

<RelativeLayout

android:layout_width="match_parent"

android:layout_height="101.0dp"

android:background="@drawable/corners_bk_white"

>

<TextView

android:id="@+id/tvAlbumSmallImage"

android:layout_width="match_parent"

android:layout_height="50dp"

android:text="相册"

android:textSize="14sp"

android:textColor="#5084FE"

android:gravity="center"

/>

<View

android:layout_width="match_parent"

android:layout_height="1dp"

android:background="#cccccc"

android:layout_centerVertical="true"

/>

<TextView

android:id="@+id/tvCameraSmallImage"

android:layout_width="match_parent"

android:layout_height="50dp"

android:text="拍照"

android:textSize="14sp"

android:textColor="#5084FE"

android:gravity="center"

android:layout_below="@id/tvAlbumSmallImage"

/>

</RelativeLayout>

<TextView

android:id="@+id/tvCancel"

android:layout_width="match_parent"

android:layout_height="50dp"

android:text="取消"

android:textColor="#5084FE"

android:background="@drawable/corners_bk_white"

android:gravity="center"

android:layout_marginTop="20dp"

android:textSize="14sp"

android:layout_marginBottom="15dp"

/>

</LinearLayout>

</RelativeLayout>

view_cameraalbum_popup_bigimage.xml(裁剪大图页面弹窗布局):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent">

<FrameLayout

android:id="@+id/flMaskLayer"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#000000"/>

<LinearLayout

android:id="@+id/llHeader"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginLeft="10dp"

android:layout_marginRight="10dp"

android:orientation="vertical"

android:layout_alignParentBottom="true">

<RelativeLayout

android:layout_width="match_parent"

android:layout_height="101.0dp"

android:background="@drawable/corners_bk_white"

>

<TextView

android:id="@+id/tvAlbumBigImage"

android:layout_width="match_parent"

android:layout_height="50dp"

android:text="相册"

android:textSize="14sp"

android:textColor="#5084FE"

android:gravity="center"

/>

<View

android:layout_width="match_parent"

android:layout_height="1dp"

android:background="#cccccc"

android:layout_centerVertical="true"

/>

<TextView

android:id="@+id/tvCameraBigImage"

android:layout_width="match_parent"

android:layout_height="50dp"

android:text="拍照"

android:textSize="14sp"

android:textColor="#5084FE"

android:gravity="center"

android:layout_below="@id/tvAlbumBigImage"

/>

</RelativeLayout>

<TextView

android:id="@+id/tvCancel"

android:layout_width="match_parent"

android:layout_height="50dp"

android:text="取消"

android:textColor="#5084FE"

android:background="@drawable/corners_bk_white"

android:gravity="center"

android:layout_marginTop="20dp"

android:textSize="14sp"

android:layout_marginBottom="15dp"

/>

</LinearLayout>

</RelativeLayout>

activity_main.xml(首页面布局):

<RelativeLayoutxmlns: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:id="@+id/main"

android:background="#F2F3F4">

<TextView

android:id="@+id/tvCameraAlbumFuntip"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:padding="10dp"

android:textColor="#676767"

android:text="@string/text_camera_album_tip"

/>

<Button

android:id="@+id/btnMenu"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="#343434"

android:padding="10dp"

android:layout_marginTop="20dp"

android:layout_marginLeft="10dp"

android:layout_marginRight="10dp"

android:layout_marginBottom="20dp"

android:layout_below="@id/tvCameraAlbumFuntip"

android:text="开始"

android:textSize="14sp"

android:textColor="#ffffff"

/>

</RelativeLayout>

MainActivity.java(首页代码文件):

public class MainActivity extends Activity {

protected Button btnMenu = null;

private PopupWindow popupDialog = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btnMenu = (Button) findViewById(R.id.btnMenu);

btnMenu.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if(v.getId() == R.id.btnMenu) {

switchToPictureMakeMenusView();

}

}

});

}

@SuppressLint({ "NewApi", "ClickableViewAccessibility" })

private void switchToPictureMakeMenusView() {

Popup popup = new Popup();

popup.setvWidth(LayoutParams.MATCH_PARENT);

popup.setvHeight(LayoutParams.MATCH_PARENT);

popup.setClickable(true);

popup.setContentView(R.layout.view_cameraalbum_popup_menus);

OnTouchListener listener = new OnTouchListener() {

@Override

public boolean onTouch(View view, MotionEvent event) {

int height = view.findViewById(R.id.llHeader).getTop();

int y = (int) event.getY();

if(event.getAction()==MotionEvent.ACTION_UP){

if(y<height){

ViewUtils.dismissPopupDialog();

}

}

return true;

}

};

popup.setTouchListener(listener);

popupDialog = ViewUtils.createPopupDialog(this, popup);

popupDialog.showAtLocation(findViewById(R.id.main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, popup.getxPos(),
popup.getyPos());

View view = popupDialog.getContentView();

view.findViewById(R.id.flMaskLayer).setAlpha(0.75f);

View.OnClickListener l = new View.OnClickListener() {

@Override

public void onClick(View v) {

if(v.getId() == R.id.tvCancel) {

ViewUtils.dismissPopupDialog();

}

else if(v.getId() == R.id.tvSmallImage) {

switchToPictureMakeSmallActivity();

}

else if(v.getId() == R.id.tvBigImage) {

switchToPictureMakeBigActivity();

}

}

};

view.findViewById(R.id.tvCancel).setOnClickListener(l);

view.findViewById(R.id.tvSmallImage).setOnClickListener(l);

view.findViewById(R.id.tvBigImage).setOnClickListener(l);

}

private void switchToPictureMakeSmallActivity() {

Intent intent = new Intent();

intent.setClass(this, PictureMakeSmallActivity.class);

startActivity(intent);

}

private void switchToPictureMakeBigActivity() {

Intent intent = new Intent();

intent.setClass(this, PictureMakeBigActivity.class);

startActivity(intent);

}

activity_smallimage.xml(裁剪小图页面):

<RelativeLayoutxmlns: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:id="@+id/main"

android:background="#F2F3F4">

<TextView

android:id="@+id/tvCameraAlbumFuntip"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:padding="10dp"

android:textColor="#030303"

android:text="裁剪后的小图"

/>

<ImageView

android:id="@+id/ivSmallImageView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/ic_launcher"

android:contentDescription="@string/app_name"

android:layout_below="@id/tvCameraAlbumFuntip"

android:layout_margin="10dp"

/>

<Button

android:id="@+id/btnMenu"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="#343434"

android:padding="10dp"

android:layout_marginTop="20dp"

android:layout_marginLeft="10dp"

android:layout_marginRight="10dp"

android:layout_marginBottom="20dp"

android:layout_below="@id/ivSmallImageView"

android:text="裁剪头像"

android:textSize="14sp"

android:textColor="#ffffff"

/>

</RelativeLayout>

PictureMakeSmallActivity.java(裁剪小图代码文件):

public class PictureMakeSmallActivity extends Activity {

private PopupWindow popupDialog = null;

private ImageView ivSmallImageView = null;

private static Bitmap smallImage = null;

private static final int PHOTO_CAMERA_ACTION = 1;

private static final int PHOTO_ZOOM_ACTION = 2;

private static final int PHOTO_ALBUM_ACTION = 3;

private static final String FILE_IAMGE = "image/*";

private Button btnMenu = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_smallimage);

ivSmallImageView = (ImageView) findViewById(R.id.ivSmallImageView);

btnMenu = (Button) findViewById(R.id.btnMenu);

btnMenu.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if(v.getId() == R.id.btnMenu) {

switchToPictureMakeSmallImageView();

}

}

});

}

// call the album to filter photo

private void switchToAlbumFilterPhoto() {

Intent intent = new Intent(Intent.ACTION_PICK);

intent.setDataAndType(MediaStore.Images.Media.INTERNAL_CONTENT_URI, FILE_IAMGE);

startActivityForResult(intent, PHOTO_ZOOM_ACTION);

}

// call the camera to take photo

private void switchToCameraCapturePhoto() {

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

String fileName = Environment.getExternalStorageDirectory().toString() + File.separator + "test.png";

File file = new File(fileName);

if(!file.exists()) {

file.mkdir();

}

Uri uri = Uri.fromFile(file);

intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);

startActivityForResult(intent, PHOTO_CAMERA_ACTION);

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

switch(requestCode) {

case PHOTO_CAMERA_ACTION: {

if(resultCode != RESULT_OK) {

return;

}

String fileName = Environment.getExternalStorageDirectory().toString() + File.separator + "test.png";

File file = new File(fileName);

if(!file.exists()) {

file.mkdir();

}

Uri uri = Uri.fromFile(file);

startPhotoZoom(uri);

}

break;

case PHOTO_ZOOM_ACTION: {

if(resultCode != RESULT_OK) {

return;

}

startPhotoZoom(data.getData());// data.getData()

}

break;

case PHOTO_ALBUM_ACTION: {

if(resultCode != RESULT_OK) {

return;

}

Bundle extras = data.getExtras();

if (null !=extras) {

smallImage = extras.getParcelable("data");

ByteArrayOutputStream stream = new ByteArrayOutputStream();

// (0 - 100)压缩文件

smallImage.compress(Bitmap.CompressFormat.PNG, 85, stream);

// show and cache local header image

ivSmallImageView.setImageBitmap(smallImage);

// here,you can transfer bitmap to bytes and send to server

// or you also can save bitmap to local sd card and so.

}

}

break;

default:

break;

}

}

public void startPhotoZoom(Uri uri) {

Intent intent = new Intent("com.android.camera.action.CROP");

intent.setDataAndType(uri, FILE_IAMGE);

intent.putExtra("crop", "true");

intent.putExtra("aspectX", 1);

intent.putExtra("aspectY", 1);

intent.putExtra("outputX", 60);

intent.putExtra("outputY", 60);

intent.putExtra("scale", true);

intent.putExtra("return-data", true);

startActivityForResult(intent, PHOTO_ALBUM_ACTION);

}

@SuppressLint({ "ClickableViewAccessibility", "NewApi" })

private void switchToPictureMakeSmallImageView() {

Popup popup = new Popup();

popup.setvWidth(LayoutParams.MATCH_PARENT);

popup.setvHeight(LayoutParams.MATCH_PARENT);

popup.setClickable(true);

popup.setContentView(R.layout.view_cameraalbum_popup_smallimage);

OnTouchListener listener = new OnTouchListener() {

@Override

public boolean onTouch(View view, MotionEvent event) {

int height = view.findViewById(R.id.llHeader).getTop();

int y = (int) event.getY();

if(event.getAction()==MotionEvent.ACTION_UP){

if(y<height){

ViewUtils.dismissPopupDialog();

}

}

return true;

}

};

popup.setTouchListener(listener);

popupDialog = ViewUtils.createPopupDialog(this, popup);

popupDialog.showAtLocation(findViewById(R.id.main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, popup.getxPos(),
popup.getyPos());

View view = popupDialog.getContentView();

view.findViewById(R.id.flMaskLayer).setAlpha(0.75f);

View.OnClickListener l = new View.OnClickListener() {

@Override

public void onClick(View v) {

if(v.getId() == R.id.tvCancel) {

ViewUtils.dismissPopupDialog();

}

else if(v.getId() == R.id.tvAlbumSmallImage) {

switchToAlbumFilterPhoto();

ViewUtils.dismissPopupDialog();

}

else if(v.getId() == R.id.tvCameraSmallImage) {

switchToCameraCapturePhoto();

ViewUtils.dismissPopupDialog();

}

}

};

view.findViewById(R.id.tvCancel).setOnClickListener(l);

view.findViewById(R.id.tvAlbumSmallImage).setOnClickListener(l);

view.findViewById(R.id.tvCameraSmallImage).setOnClickListener(l);

}

activity_bigimage.xml(裁剪大图页面):

<RelativeLayoutxmlns: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:id="@+id/main"

android:background="#F2F3F4">

<TextView

android:id="@+id/tvCameraAlbumFuntip2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:padding="10dp"

android:textColor="#030303"

android:text="裁剪后的大图"

/>

<ImageView

android:id="@+id/ivBigImageView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/ic_launcher"

android:contentDescription="@string/app_name"

android:layout_below="@id/tvCameraAlbumFuntip2"

android:layout_margin="10dp"

/>

<Button

android:id="@+id/btnMenu"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="#343434"

android:padding="10dp"

android:layout_marginTop="20dp"

android:layout_marginLeft="10dp"

android:layout_marginRight="10dp"

android:layout_marginBottom="20dp"

android:layout_below="@id/ivBigImageView"

android:text="裁剪头像"

android:textSize="14sp"

android:textColor="#ffffff"

/>

</RelativeLayout>

PictureMakeBigActivity.java(裁剪大图代码文件):

public class PictureMakeBigActivity extends Activity {

private PopupWindow popupDialog = null;

private ImageView ivBigImageView = null;

private static Bitmap bigImage = null;

private static final int PHOTO_CAMERA_ACTION = 1;

private static final int PHOTO_ZOOM_ACTION = 2;

private static final int PHOTO_ALBUM_ACTION = 3;

private static final String FILE_IAMGE = "image/*";

private Button btnMenu = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_bigimage);

ivBigImageView = (ImageView) findViewById(R.id.ivBigImageView);

btnMenu = (Button) findViewById(R.id.btnMenu);

btnMenu.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if(v.getId() == R.id.btnMenu) {

switchToPictureMakeBigImageView();

}

}

});

}

// call the album to filter photo

private void switchToAlbumFilterPhoto() {

Intent intent = new Intent(Intent.ACTION_PICK);

intent.setDataAndType(MediaStore.Images.Media.INTERNAL_CONTENT_URI, FILE_IAMGE);

startActivityForResult(intent, PHOTO_ZOOM_ACTION);

}

// call the camera to take photo

private void switchToCameraCapturePhoto() {

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

String fileName = Environment.getExternalStorageDirectory().toString() + File.separator + "test.png";

File file = new File(fileName);

if(!file.exists()) {

file.mkdir();

}

Uri uri = Uri.fromFile(file);

intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);

startActivityForResult(intent, PHOTO_CAMERA_ACTION);

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

switch(requestCode) {

case PHOTO_CAMERA_ACTION: {

if(resultCode != RESULT_OK) {

return;

}

String fileName = Environment.getExternalStorageDirectory().toString() + File.separator + "test.png";

File file = new File(fileName);

if(!file.exists()) {

file.mkdir();

}

Uri uri = Uri.fromFile(file);

startPhotoZoom(uri);

}

break;

case PHOTO_ZOOM_ACTION: {

if(resultCode != RESULT_OK) {

return;

}

startPhotoZoom(data.getData());// data.getData()

}

break;

case PHOTO_ALBUM_ACTION: {

if(resultCode != RESULT_OK) {

return;

}

Uri originalUri = data.getData();

if(null != originalUri) {

decodeUriAsBitmap(originalUri);

}

}

break;

default:

break;

}

}

private void decodeUriAsBitmap(Uri uri) {

try {

//result = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));

ContentResolver resolver = getContentResolver();

//Bitmap result = MediaStore.Images.Media.getBitmap(resolver, uri);

Bitmap result = BitmapFactory.decodeStream(resolver.openInputStream(uri));

if(null == result) {

return;

}

ByteArrayOutputStream stream = new ByteArrayOutputStream();

result.compress(Bitmap.CompressFormat.PNG, 85, stream); // (0 - 100)压缩文件

float scale = 0.85f;

bigImage = zoomBitmap(result,(int)(result.getWidth() * scale),(int)(result.getHeight() * scale));

result.recycle();

// show and cache local header image

ivBigImageView.setImageBitmap(bigImage);

// here,you can transfer bitmap to bytes and send to server

// or you also can save bitmap to local sd card and so.

} catch (FileNotFoundException e) {

}

}

private void startPhotoZoom(Uri uri) {

Intent intent = new Intent("com.android.camera.action.CROP");

intent.setDataAndType(uri, FILE_IAMGE);

intent.putExtra("crop", "true");

intent.putExtra("aspectX", 1);

intent.putExtra("aspectY", 1);

intent.putExtra("outputX", 800);

intent.putExtra("outputY", 800);

intent.putExtra("scale", true);

intent.putExtra("return-data", false);

intent.putExtra("scaleUpIfNeeded", true); // limit the black border of image

intent.putExtra("noFaceDetection", true); // no face detection

startActivityForResult(intent, PHOTO_ALBUM_ACTION);

}

private 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;

}

@SuppressLint({ "ClickableViewAccessibility", "NewApi" })

private void switchToPictureMakeBigImageView() {

Popup popup = new Popup();

popup.setvWidth(LayoutParams.MATCH_PARENT);

popup.setvHeight(LayoutParams.MATCH_PARENT);

popup.setClickable(true);

popup.setContentView(R.layout.view_cameraalbum_popup_bigimage);

OnTouchListener listener = new OnTouchListener() {

@Override

public boolean onTouch(View view, MotionEvent event) {

int height = view.findViewById(R.id.llHeader).getTop();

int y = (int) event.getY();

if(event.getAction()==MotionEvent.ACTION_UP){

if(y<height){

ViewUtils.dismissPopupDialog();

}

}

return true;

}

};

popup.setTouchListener(listener);

popupDialog = ViewUtils.createPopupDialog(this, popup);

popupDialog.showAtLocation(findViewById(R.id.main), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, popup.getxPos(),
popup.getyPos());

View view = popupDialog.getContentView();

view.findViewById(R.id.flMaskLayer).setAlpha(0.75f);

View.OnClickListener l = new View.OnClickListener() {

@Override

public void onClick(View v) {

if(v.getId() == R.id.tvCancel) {

ViewUtils.dismissPopupDialog();

}

else if(v.getId() == R.id.tvAlbumBigImage) {

switchToAlbumFilterPhoto();

ViewUtils.dismissPopupDialog();

}

else if(v.getId() == R.id.tvCameraBigImage) {

switchToCameraCapturePhoto();

ViewUtils.dismissPopupDialog();

}

}

};

view.findViewById(R.id.tvCancel).setOnClickListener(l);

view.findViewById(R.id.tvAlbumBigImage).setOnClickListener(l);

view.findViewById(R.id.tvCameraBigImage).setOnClickListener(l);

}

好了,到这里我已经罗列出了所有主要文件实现,如果有问题可以在评论或是群(179914858)中进行讨论,谢谢。

代码下载地址:点这里

时间: 2024-12-21 03:02:14

Android中相机和相册使用分析的相关文章

Android中的消息处理实例与分析

Android中的消息处理实例与分析 摘要 本文介绍了Android中的消息处理机制,给出了Android消息处理中的几个重点类Handler.Message.MessageQueue.Looper.Runnable.Thread的详细介绍,提供了两个消息处理的实例代码,并深入分析了使用Android消息机制应该遵循的几个原则. 阅读本文的收获 在具有java基础的情况下,Android的学习比较轻松,很多人在没有深刻了解Android消息处理机制的背景下,已经能够开发出可用的app,很多人开始

Android中app卡顿原因分析示例

在知乎回答了一个“为什么微博的app在iPhone比Android上流畅”的问题.后面部分是一个典型的动画卡顿的性能分析过程,因此帖在这里.有编程问题可以在这里交流.知乎链接. ========================================================= 我来说下我所知道的事情.我不知道iOS为什么流畅,但我知道一些Android为什么不流畅的原因. 首先,就题主所说的问题,我用iPad和小米Pad对比了一下微博滑动滚屏这件事情(2014年8月10日目前微博

Android中的软件安全和逆向分析[二]—apk反破解技术与安全保护机制

在Android应用开发中,当我们开发完软件之后,我们不希望别人能够反编译破解我们的应用程序,不能修改我们的代码逻辑.实际上,在应用程序的安全机制考虑中,我们希望自己的应用程序安全性高,通过各种加密操作等来增大竞争对手的反编译破解成本.设想,竞争对手开发一个同样的应用程序需要10天,而破解我们的软件程序需要100天,那么势必会打消黑客程序员破解我们应用程序的念头.如何增加对手的破解成本,就需要考验我们应用程序的安全性有多高,加密技术有多强.一个优秀的应用程序,不仅能为用户带来利益,同时也能保护自

android中倒计时控件CountDownTimer分析

android中倒计时控件CountDownTimer分析 1 示例代码 new CountDownTimer(10000, 1000) { public void onTick(long millisUntilFinished) { LogUtil.i(TAG, "seconds remaining: " + millisUntilFinished / 1000); } public void onFinish() { LogUtil.i(TAG, "done!"

Android中相机拍照

Android中调用系统相机的api接口在android.hardware包中.包里面类主要用到了Camera类.该类里面包含几个内部类:Camera.Parameters.Camera.CameraInfo.Camera.PictureCallback. Camera.Parameters是通过Camera.Parameters parameters = mCamera.getParameters()获取,主要用来设置相机的一些參数,包含图片的格式.是否开启闪光灯等. Camera.Camer

Android中线程间通信原理分析:Looper,MessageQueue,Handler

自问自答的两个问题 在我们去讨论Handler,Looper,MessageQueue的关系之前,我们需要先问两个问题: 1.这一套东西搞出来是为了解决什么问题呢? 2.如果让我们来解决这个问题该怎么做? 以上者两个问题,是我最近总结出来的,在我们学习了解一个新的技术之前,最好是先能回答这两个问题,这样你才能对你正在学习的东西有更深刻的认识. 第一个问题:google的程序员们搞出这一套东西是为了解决什么问题的?这个问题很显而易见,为了解决线程间通信的问题.我们都知道,Android的UI/Vi

Android中RelativeLayout和LinearLayout性能分析

先看一些现象吧:用eclipse或者Android studio,新建一个Activity自动生成的布局文件都是RelativeLayout,或许你会认为这是IDE的默认设置问题,其实不然,这是由 android-sdk\tools\templates\activities\BlankActivity\root\res\layout\activity_simple.xml.ftl 这个文件事先就定好了的,也就是说这是Google的选择,而非IDE的选择.那SDK为什么会默认给开发者新建一个默认的

Android选取相机、相册图片进行裁剪,并更新UI

demo源码:http://download.csdn.net/detail/u010778159/8648701 效果图: 界面非常的简单,只有一个imageView,通过点击该ImageView,从相册中选取照片,或拍照,将得到的照片按要求进行裁剪,然后将裁剪后的照片更新到ImageView中. 现在,来看一下工程的xml,和.java文件: 有两个xml文件,main.xml是进入app时的主页面,有一个ImageView select_pic_layout.xml是点击ImageView

Android中的NestedScrollingParent和NestedScrollingChild分析

在分析SwipeRefreshLayout源码的时候发现该类实现了NestedScrollingParent和NestedScrollingChild两个接口,甚是好奇,于是结合了网上的资料,然后根据我个人的理解写下本章. 这个两个接口是为了更好解决事件冲突的. 在这里 nested scrolling 就翻译为嵌套滚动吧. 但是这和以前用过的dispatchTouchEvent,onInterceptTouchEvent,onTouchEvent和requestDisallowIntercep