package com.kpd.test;
import java.io.File;
import java.io.FileNotFoundException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView imageView;
private String[] items = new String[] { "选择本地图片", "拍照" };
/* 头像名称 */
private static final String IMAGE_FILE_NAME = "temp.jpg";
/* 请求码 */
private static final int IMAGE_REQUEST_CODE = 0;
private static final int CAMERA_REQUEST_CODE = 1;
private static final int RESULT_REQUEST_CODE = 2;
private File cutFile;
private File tempFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.iv);
cutFile = new File(getCacheFile(), "head.jpg");
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog();
}
});
}
private void showDialog() {
new AlertDialog.Builder(this).setTitle("设置头像").setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
Intent intentFromGallery = new Intent();
intentFromGallery.setType("image/*"); // 设置文件类型
intentFromGallery.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intentFromGallery, IMAGE_REQUEST_CODE);
break;
case 1:
Intent intentFromCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(getCacheFile(), IMAGE_FILE_NAME)));
startActivityForResult(intentFromCapture, CAMERA_REQUEST_CODE);
break;
}
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// 结果码不等于取消时候
if (resultCode != RESULT_CANCELED) {
switch (requestCode) {
case IMAGE_REQUEST_CODE:
startPhotoZoom(data.getData());
break;
case CAMERA_REQUEST_CODE:
tempFile = new File(getCacheFile(), IMAGE_FILE_NAME);
startPhotoZoom(Uri.fromFile(tempFile));
break;
case RESULT_REQUEST_CODE:
if (tempFile != null && tempFile.exists())
tempFile.delete();
Bitmap bitmap = decodeUriAsBitmap(Uri.fromFile(cutFile));
imageView.setImageBitmap(bitmap);
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
/**
* 获取缓存目录
*
* @return
*/
private File getCacheFile() {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
return Environment.getExternalStorageDirectory();
}
return getCacheDir();
}
/**
* 裁剪图片方法实现
*
* @param uri
*/
public void startPhotoZoom(Uri uri) {
if (uri == null) {
Log.i("tag", "The uri is not exist.");
}
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// 设置裁剪
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", 400);
intent.putExtra("outputY", 400);
// true:返回在intent中,通过Bitmap bitmap=intent.getExtras().getParcelable("data")获取
// false:返回Uri
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cutFile));
startActivityForResult(intent, 2);
}
/**
* uri转换为bitmap
*
* @param uri
* @return
*/
private Bitmap decodeUriAsBitmap(Uri uri) {
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(this.getContentResolver().openInputStream(uri));
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
return bitmap;
}
}
import java.io.File;
import java.io.FileNotFoundException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class MainActivity extends Activity {
private ImageView imageView;
private String[] items = new String[] { "选择本地图片", "拍照" };
/* 头像名称 */
private static final String IMAGE_FILE_NAME = "temp.jpg";
/* 请求码 */
private static final int IMAGE_REQUEST_CODE = 0;
private static final int CAMERA_REQUEST_CODE = 1;
private static final int RESULT_REQUEST_CODE = 2;
private File cutFile;
private File tempFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.iv);
cutFile = new File(getCacheFile(), "head.jpg");
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showDialog();
}
});
}
private void showDialog() {
new AlertDialog.Builder(this).setTitle("设置头像").setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
Intent intentFromGallery = new Intent();
intentFromGallery.setType("image/*"); // 设置文件类型
intentFromGallery.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intentFromGallery, IMAGE_REQUEST_CODE);
break;
case 1:
Intent intentFromCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(getCacheFile(), IMAGE_FILE_NAME)));
startActivityForResult(intentFromCapture, CAMERA_REQUEST_CODE);
break;
}
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// 结果码不等于取消时候
if (resultCode != RESULT_CANCELED) {
switch (requestCode) {
case IMAGE_REQUEST_CODE:
startPhotoZoom(data.getData());
break;
case CAMERA_REQUEST_CODE:
tempFile = new File(getCacheFile(), IMAGE_FILE_NAME);
startPhotoZoom(Uri.fromFile(tempFile));
break;
case RESULT_REQUEST_CODE:
if (tempFile != null && tempFile.exists())
tempFile.delete();
Bitmap bitmap = decodeUriAsBitmap(Uri.fromFile(cutFile));
imageView.setImageBitmap(bitmap);
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
/**
* 获取缓存目录
*
* @return
*/
private File getCacheFile() {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
return Environment.getExternalStorageDirectory();
}
return getCacheDir();
}
/**
* 裁剪图片方法实现
*
* @param uri
*/
public void startPhotoZoom(Uri uri) {
if (uri == null) {
Log.i("tag", "The uri is not exist.");
}
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// 设置裁剪
intent.putExtra("crop", "true");
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪图片宽高
intent.putExtra("outputX", 400);
intent.putExtra("outputY", 400);
// true:返回在intent中,通过Bitmap bitmap=intent.getExtras().getParcelable("data")获取
// false:返回Uri
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cutFile));
startActivityForResult(intent, 2);
}
/**
* uri转换为bitmap
*
* @param uri
* @return
*/
private Bitmap decodeUriAsBitmap(Uri uri) {
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(this.getContentResolver().openInputStream(uri));
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
return bitmap;
}
}