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

}

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

代码下载地址:点这里

Android中相机和相冊使用分析的更多相关文章

  1. Android 中View的绘制机制源代码分析 三

    到眼下为止,measure过程已经解说完了,今天開始我们就来学习layout过程.只是在学习layout过程之前.大家有没有发现我换了编辑器,哈哈.最终下定决心从Html编辑器切换为markdown编 ...

  2. iOS开发中訪问相冊摄像像头

    iOS开发中訪问相冊摄像像头 源代码下载地址http://download.csdn.net/download/jingjingxujiayou/7270479 在AppDelegate.m文件里 - ...

  3. Android 中View的绘制机制源代码分析 一

    尊重原创: http://blog.csdn.net/yuanzeyao/article/details/46765113 差点儿相同半年没有写博客了,一是由于工作比較忙,二是认为没有什么内容值得写, ...

  4. Android 中View的绘制机制源代码分析 二

    尊重原创:http://blog.csdn.net/yuanzeyao/article/details/46842891 本篇文章接着上篇文章的内容来继续讨论View的绘制机制,上篇文章中我们主要解说 ...

  5. android之照相、相冊裁剪功能的实现过程

    今天无聊做了一些照相.相冊裁剪功能,希望能够帮到大家! 不多说了,贴代码实际一点: 首先是XML: <ImageButton android:id="@+id/imageButton1 ...

  6. Android中Input型输入设备驱动原理分析(一)

    转自:http://blog.csdn.net/eilianlau/article/details/6969361 话说Android中Event输入设备驱动原理分析还不如说Linux输入子系统呢,反 ...

  7. Android中对Log日志文件的分析[转]

    一,Bug出现了, 需要“干掉”它 bug一听挺吓人的,但是只要你懂了,android里的bug是很好解决的,因为android里提供了LOG机制,具体的底层代码,以后在来分析,只要你会看bug, a ...

  8. Android中Input型输入设备驱动原理分析<一>

    话说Android中Event输入设备驱动原理分析还不如说Linux输入子系统呢,反正这个是没变的,在android的底层开发中对于Linux的基本驱动程序设计还是没变的,当然Android底层机制也 ...

  9. Android中WebView的跨域漏洞分析和应用被克隆问题情景还原(免Root获取应用沙盒数据)

    一.前言 去年年底支付宝的被克隆漏洞被爆出,无独有偶就是腾讯干的,其实真正了解这个事件之后会发现,感觉是针对支付宝.因为这个漏洞找出肯定花费了很大劲,主要是因为支付宝的特殊业务需要开启了WebView ...

随机推荐

  1. javascript中的变量、作用域和内存问题

    1.变量 变量的值的类型:基本类型值和引用类型值两种. 基本类型:Undefined.Null.Boolean.String.Number,这五类基本数据类型的值在内存中占有固定大小的空间,因此保存在 ...

  2. 淘宝联盟api调用笔记

    一.流程及主要请求接口 每日凌晨1点开始,服务器定时自动请求淘宝联盟数据,请求完毕之后,执行一个存储过程对数据进行整理,删除过期...购买数量<...的商品......,请求接口分别有(tbk_ ...

  3. php 例子 如何转换ISO8601为 utc时间

    //firstpowertime "2017-01-02T13:22:22" 获取时间$firstpowertime=$list[$i]['firstpowertime'];//判 ...

  4. C#图片水印代码整理

    这一段公司有个项目,客户要求上传的图片要带上自定义的水印.以前也经常和朋友讨论C#图片水印方面的问题,但是从来没有实际操作过.所以,借这次项目的机会也研究了一下C#图片水印的功能!本人参考的是disc ...

  5. 设计模式的征途—15.观察者(Observer)模式

    在日常生活中,交通信号灯指挥者日益拥挤的城市交通.红灯亮,汽车停止:绿灯亮,汽车继续前行:在这个过程中,交通信号灯是汽车的观察目标,而汽车则是观察者.随着交通信号灯的变化,汽车的行为也会随之变化,一盏 ...

  6. javaweb-1-B/S初论

    一.B/S结构的基本概念 1.什么是动态网页 2.为什么需要动态网页 3.如何实现动态网页 4.为什么学习B/S技术 4.1C/S结构 优点: 1.C/S架构的界面和操作可以很丰富. 2.安全性能可以 ...

  7. 创建简单的Python列表

    比如给出电影列表: The Holy Grail The Life of Brian The Meaning of Life 转换为Python可读的列表,需要遵循以下4个步骤: 1.在数据两边加引号 ...

  8. Ansible(一) 配置安装

    puppet ruby开发 salt python开发,有客户端,使用Rabbitmq消息队列,支持并发,在机器数量很多时效果比ansible好. ansible python开发, 没有客户端,基于 ...

  9. laravel查看执行sql的

    加个监听就好了~~~~而且很简单 1.在routes.php(api.php\web.php)中添加如下语句 Event::listen('illuminate.query', function($s ...

  10. 后台工作者HangFire与ABP框架Abp.Hangfire及扩展

    HangFire与Quartz.NET相比主要是HangFire的内置提供集成化的控制台,方便后台查看及监控,对于大家来说,比较方便. HangFire是什么 Hangfire是一个开源框架(.NET ...