Android中相机和相冊使用分析

时间:2023-02-10 12:27:25

Android中相机和相冊使用分析

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

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

1 使用相机和相冊选择图片。并裁剪较小图片(经常使用于剪裁小图)

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

详细的实现功能清楚了。那么就一一进行说明,详细例如以下(这里不会罗列怎么上传图片到服务端,仅仅介绍怎么使用裁剪和使用相冊和相机)。

另外。有图有真相。我的实现效果图例如以下所看到的:

Android中相机和相冊使用分析

 

Android中相机和相冊使用分析

Android中相机和相冊使用分析

Android中相机和相冊使用分析

上面为我的实现效果图,接下来依照实现次序列出源码文件。详细例如以下:

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

}

@Override

public
boolean onKeyDown(int
keyCode, KeyEvent event) {

switch(keyCode) {

case KeyEvent.KEYCODE_BACK:  {

boolean
flag = ViewUtils.isPopupShowing();

if(flag) {

ViewUtils.dismissPopupDialog();

return
false;

}

}

break;

default:

break;

}

return
super.onKeyDown(keyCode,
event);

}

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

public
boolean onKeyDown(int
keyCode, KeyEvent event) {

switch(keyCode) {

case KeyEvent.KEYCODE_BACK:  {

boolean
flag = ViewUtils.isPopupShowing();

if(flag) {

ViewUtils.dismissPopupDialog();

return
false;

}

}

break;

default:

break;

}

return
super.onKeyDown(keyCode,
event);

}

@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();

}

}

});

}

@Override

public
boolean onKeyDown(int
keyCode, KeyEvent event) {

switch(keyCode) {

case KeyEvent.KEYCODE_BACK:  {

boolean
flag = ViewUtils.isPopupShowing();

if(flag) {

ViewUtils.dismissPopupDialog();

return
false;

}

}

break;

default:

break;

}

return
super.onKeyDown(keyCode,
event);

}

// 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)中进行讨论,谢谢。

代码下载地址:点这里