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);
}
@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中相机和相冊使用分析的更多相关文章
- Android 中View的绘制机制源代码分析 三
到眼下为止,measure过程已经解说完了,今天開始我们就来学习layout过程.只是在学习layout过程之前.大家有没有发现我换了编辑器,哈哈.最终下定决心从Html编辑器切换为markdown编 ...
- iOS开发中訪问相冊摄像像头
iOS开发中訪问相冊摄像像头 源代码下载地址http://download.csdn.net/download/jingjingxujiayou/7270479 在AppDelegate.m文件里 - ...
- Android 中View的绘制机制源代码分析 一
尊重原创: http://blog.csdn.net/yuanzeyao/article/details/46765113 差点儿相同半年没有写博客了,一是由于工作比較忙,二是认为没有什么内容值得写, ...
- Android 中View的绘制机制源代码分析 二
尊重原创:http://blog.csdn.net/yuanzeyao/article/details/46842891 本篇文章接着上篇文章的内容来继续讨论View的绘制机制,上篇文章中我们主要解说 ...
- android之照相、相冊裁剪功能的实现过程
今天无聊做了一些照相.相冊裁剪功能,希望能够帮到大家! 不多说了,贴代码实际一点: 首先是XML: <ImageButton android:id="@+id/imageButton1 ...
- Android中Input型输入设备驱动原理分析(一)
转自:http://blog.csdn.net/eilianlau/article/details/6969361 话说Android中Event输入设备驱动原理分析还不如说Linux输入子系统呢,反 ...
- Android中对Log日志文件的分析[转]
一,Bug出现了, 需要“干掉”它 bug一听挺吓人的,但是只要你懂了,android里的bug是很好解决的,因为android里提供了LOG机制,具体的底层代码,以后在来分析,只要你会看bug, a ...
- Android中Input型输入设备驱动原理分析<一>
话说Android中Event输入设备驱动原理分析还不如说Linux输入子系统呢,反正这个是没变的,在android的底层开发中对于Linux的基本驱动程序设计还是没变的,当然Android底层机制也 ...
- Android中WebView的跨域漏洞分析和应用被克隆问题情景还原(免Root获取应用沙盒数据)
一.前言 去年年底支付宝的被克隆漏洞被爆出,无独有偶就是腾讯干的,其实真正了解这个事件之后会发现,感觉是针对支付宝.因为这个漏洞找出肯定花费了很大劲,主要是因为支付宝的特殊业务需要开启了WebView ...
随机推荐
- Cache 和 Buffer 都是缓存,主要区别是什么?
存储器的高速缓冲存储器存储了频繁访问的RAM位置的内容及这些数据项的存储地址.当处理器引用存储器中的某地址时,高速缓冲存储器便检查是否存有该地址.如果存有该地址,则将数据返回处理器;如果没有保存该地址 ...
- 分页查询时,使用cookie保存上次的查询条件。jQuery实现方法以及中间遇到的坑
今天做分页查询时需要在跳转页面时保存上次查询的条件,如下: 实现的大致思路就是用cookie本地保存. 其中需要用到jQuery.Cookie插件. 使用方法很简单: 存数据:$.cookie(“ke ...
- C#算法面试题
1.产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复. static void GetArray() { ]; ]; ; i < ; i++) { intArr[i] ...
- [S]SQL SERVER数据库维护与重建索引
第一步:查看是否需要维护,查看扫描密度/Scan Density是否为100% declare @table_id int set @table_id=object_id('表名') dbcc sho ...
- 最全的iOS数据存储方法
目的 项目准备运用的Core Data进行本地数据存储,本来打算只写一下Core Data的,不过既然说到了数据存储,干脆来个数据存储基础大总结!本文将对以下几个模块进行叙述. 沙盒 Plist Pr ...
- 转贴---Performance Counter(包含最全的Windows计数器解释)
http://support.smartbear.com/viewarticle/55773/ 这个Article中介绍了一个新工具,TestComplete,把其中涉及到性能计数器的部分摘抄出来了. ...
- 使用dropwizard(6)-国际化-easy-i18n
前言 Dropwizard官方文档并没有提供国际化的模块,所以只能自己加.Spring的MessageResource用的很顺手,所以copy过来. Easy i18n 在整合Dropwizard的时 ...
- webapp填坑记录[更新中]
网上也有许多的 webapp 填坑记录了,这几个月,我在公司正好也做了2个,碰到了一些问题,所以我在这里记录一下我所碰到的问题: meta 头部声明在开发的时候,刚刚创建 HTML 文件,再使用浏览器 ...
- Fedora 23建立wifi热点(Android手机可用)
在ubuntu14.04下使用ap-hotspot,速度还不错.但是在15.04下就用不了了,不知为啥.现在使用fedora23,在学校还是挺需要给手机连wifi的,于是google看看ap-hots ...
- shopxx------list列表回显修改尝试
需求:在商品列表展示页面增加一列 一.修改模板 1.列表页面对应的freemarker模板位置 /shopxx/WebContent/WEB-INF/template/admin/product/li ...