快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

拍照剪切and获取sd卡中的图片

package com.truekey.alarmclock.user;

成都网络公司-成都网站建设公司创新互联建站10多年经验成就非凡,专业从事成都网站设计、成都网站建设,成都网页设计,成都网页制作,软文发布平台1元广告等。10多年来已成功提供全面的成都网站建设方案,打造行业特色的成都网站建设案例,建站热线:028-86922220,我们期待您的来电!

import java.io.File;

import android.app.Activity;

import android.app.Dialog;

import android.content.Context;

import android.content.Intent;

import android.content.SharedPreferences;

import android.graphics.Bitmap;

import android.graphics.drawable.BitmapDrawable;

import android.graphics.drawable.Drawable;

import android.net.Uri;

import android.os.Bundle;

import android.os.Environment;

import android.provider.MediaStore;

import android.view.Gravity;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.Window;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.RelativeLayout.LayoutParams;

import android.widget.TextView;

import com.truekey.alarmclock.R;

public class UserInfoActivity extends Activity {

ImageView user_header_p_w_picpath;

Dialog pictureDialog;

View pictureView;

Button takePictureBtn, allPicture, cancle;

private static final int SCALE = 15;// 照片缩小比例

String imgBase64;

String fileName = null;

ImageView iv_title_left, iv_title_right;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.user_info_layout);

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

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

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

TextView nicheng_tv = (TextView) findViewById(R.id.nicheng_tv);

pictureDialog();

}

public void onClick(View v) {

switch (v.getId()) {

case R.id.user_header_p_w_picpath:

pictureDialog.show();

pictureDialog.getWindow().setLayout(LayoutParams.MATCH_PARENT,

LayoutParams.WRAP_CONTENT);

break;

case R.id.iv_title_right:

finish();

overridePendingTransition(R.anim.home_main_last_twoactivity_out,

R.anim.ptr_slide_out_to_bottom);

break;

case R.id.iv_title_left:

finish();

overridePendingTransition(R.anim.home_main_last_twoactivity_in,

R.anim.ptr_slide_out_to_bottom);

default:

break;

}

}

// 拍照布局

public void pictureDialog() {

pictureView = LayoutInflater.from(UserInfoActivity.this).inflate(

R.layout.takepicture_dialog, null);

cancle = (Button) pictureView.findViewById(R.id.cancle);

takePictureBtn = (Button) pictureView.findViewById(R.id.take_picture);

takePictureBtn.setOnClickListener(new ButtonOnClickListener());

// 所有的图片选取。。

allPicture = (Button) pictureView.findViewById(R.id.all_picture);

// 所有图片

allPicture.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

Intent intent = new Intent(Intent.ACTION_PICK, null);

intent.setDataAndType(

MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "p_w_picpath/*");

startActivityForResult(intent, 1);

}

});

// 取消

cancle.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

pictureDialog.dismiss();

}

});

if (pictureView != null) {

/* 自定义对话框 */

pictureDialog = new Dialog(this, R.style.mydialog);

pictureDialog.setContentView(pictureView);

pictureDialog.setCancelable(false);

Window window = pictureDialog.getWindow();

window.setWindowAnimations(R.style.AlterDialogAnima);

window.setGravity(Gravity.BOTTOM);

}

}

public final static int CAMERA_RESULT = 8888;

// 拍照获取图片

private class ButtonOnClickListener implements View.OnClickListener {

@Override

public void onClick(View v) {

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

// 下面这句指定调用相机拍照后的照片存储的路径

intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(

Environment.getExternalStorageDirectory(), "xiaoma.jpg")));

startActivityForResult(intent, 2);

}

}

@Override

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

super.onActivityResult(requestCode, resultCode, data);

if (resultCode == RESULT_OK) {

switch (requestCode) {

case 1:

startPhotoZoom(data.getData());

break;

// 如果是调用相机拍照时

case 2:

File temp = new File(Environment.getExternalStorageDirectory()

+ "/xiaoma.jpg");

startPhotoZoom(Uri.fromFile(temp));

break;

// 取得裁剪后的图片

case 3:

/**

* 非空判断大家一定要验证,如果不验证的话, 在剪裁之后如果发现不满意,要重新裁剪,丢弃

* 当前功能时,会报NullException,小马只 在这个地方加下,大家可以根据不同情况在合适的 地方做判断处理类似情况

*

*/

if (data != null) {

setPicToView(data);

}

break;

default:

break;

}

}

}

/**

* 裁剪图片方法实现

*

* @param uri

*/

public void startPhotoZoom(Uri uri) {

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

intent.setDataAndType(uri, "p_w_picpath/*");

// 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪

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

// aspectX aspectY 是宽高的比例

intent.putExtra("aspectX", 1);

intent.putExtra("aspectY", 1);

// outputX outputY 是裁剪图片宽高

intent.putExtra("outputX", 150);

intent.putExtra("outputY", 150);

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

startActivityForResult(intent, 3);

}

/**

* 保存裁剪之后的图片数据

*

* @param picdata

*/

private void setPicToView(Intent picdata) {

Bundle extras = picdata.getExtras();

if (extras != null) {

Bitmap photo = extras.getParcelable("data");

Drawable drawable = new BitmapDrawable(photo);

pictureDialog.dismiss();

// 为防止原始图片过大导致内存溢出,这里先缩小原图显示,然后释放原始Bitmap占用的内存

Bitmap choosepicture = ImageTools.toRoundBitmap2(photo);

// 释放原始图片占用的内存,防止out of memory异常发生

photo.recycle();

user_header_p_w_picpath.setImageBitmap(choosepicture);

}

}

// 用于保存图片

public void putSharedpreference(String takepicture) {

SharedPreferences shared = getSharedPreferences("temp",

Context.MODE_PRIVATE);

SharedPreferences.Editor edit = shared.edit();

edit.putString("picture", takepicture);

edit.commit();

}

public String getSharedpreference() {

SharedPreferences shared = getSharedPreferences("temp",

Context.MODE_PRIVATE);

return shared.getString("picture", "");

}

@Override

protected void onDestroy() {

super.onDestroy();

putSharedpreference(fileName);

}

@Override

protected void onResume() {

super.onResume();

}

}


文章名称:拍照剪切and获取sd卡中的图片
转载来源:http://6mz.cn/article/ppicoi.html

其他资讯