标签:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="300dp" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:id="@+id/iv3" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@mipmap/run"/> </LinearLayout>
package lebang.bwie.com.lebang.activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Environment; import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import com.google.android.gms.appindexing.Action; import com.google.android.gms.appindexing.AppIndex; import com.google.android.gms.common.api.GoogleApiClient; import com.lidroid.xutils.BitmapUtils; import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import lebang.bwie.com.lebang.R; public class PhotoActivity extends AppCompatActivity { private static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照 private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择 private static final int PHOTO_REQUEST_CUT = 3;// 结果 private File tempFile = new File(Environment.getExternalStorageDirectory(), getPhotoFileName()); private ImageView iv; /** * ATTENTION: This was auto-generated to implement the App Indexing API. * See https://g.co/AppIndexing/AndroidStudio for more information. */ private Bitmap photo; protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case PHOTO_REQUEST_TAKEPHOTO:// 当选择拍照时调用 startPhotoZoom(Uri.fromFile(tempFile)); break; case PHOTO_REQUEST_GALLERY:// 当选择从本地获取图片时 // 做非空判断,当我们觉得不满意想重新剪裁的时候便不会报异常,下同 if (data != null) { System.out.println("11================"); startPhotoZoom(data.getData()); } else { System.out.println("================"); } break; case PHOTO_REQUEST_CUT:// 返回的结果 if (data != null) // setPicToView(data); setPicToView(data); break; } super.onActivityResult(requestCode, resultCode, data); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.activity_photo); setContentView(R.layout.layout_p); iv = (ImageView) findViewById(R.id.iv3); iv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // AlertDialog mDialog = new AlertDialog.Builder(PhotoActivity.this, R.style.FullScreenDialog).create(); AlertDialog mDialog = new AlertDialog.Builder(PhotoActivity.this) // .setIcon(R.drawable.xiangji) .setTitle("选择上传头像方式") // .setIcon(android.R.drawable.ic_dialog_info) .setPositiveButton("照相", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //调用系统拍照功能: Intent cameraintent = new Intent( MediaStore.ACTION_IMAGE_CAPTURE); // 指定调用相机拍照后照片的储存路径 cameraintent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tempFile)); startActivityForResult(cameraintent, PHOTO_REQUEST_TAKEPHOTO); } }) .setNegativeButton("相册", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //调用系统相册功能: Intent getAlbum = new Intent(Intent.ACTION_GET_CONTENT); getAlbum.setType("image/*"); startActivityForResult(getAlbum, PHOTO_REQUEST_GALLERY); } }).create(); if (mDialog != null && !mDialog.isShowing()) { mDialog.show(); // mDialog.setContentView(R.layout.layout_p); mDialog.setCanceledOnTouchOutside(false); } } }); } // 将进行剪裁后的图片显示到UI界面上 private void setPicToView(Intent picdata) { Bundle bundle = picdata.getExtras(); if (bundle != null) { photo = bundle.getParcelable("data"); iv.setImageBitmap(photo); } } // 使用系统当前日期加以调整作为照片的名称 private String getPhotoFileName() { Date date = new Date(System.currentTimeMillis()); SimpleDateFormat dateFormat = new SimpleDateFormat( "‘IMG‘_yyyyMMdd_HHmmss"); return dateFormat.format(date) + ".jpg"; } private void startPhotoZoom(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // crop为true是设置在开启的intent中设置显示的view可以剪裁 intent.putExtra("crop", "true"); // aspectX aspectY 是宽高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX,outputY 是剪裁图片的宽高 intent.putExtra("outputX", 300); intent.putExtra("outputY", 300); intent.putExtra("return-data", true); intent.putExtra("noFaceDetection", true); System.out.println("22================"); startActivityForResult(intent, PHOTO_REQUEST_CUT); } }
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
标签:
原文地址:http://www.cnblogs.com/gaoliangjie/p/5701447.html