package com.cogent.piccut;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
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.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
public class PicCutActivity extends Activity implements OnClickListener {
private ImageButton img_btn;
private Button btn;
private static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照
private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择
private static final int PHOTO_REQUEST_CUT = 3;// 结果
// 创建一个以当前时间为名称的文件
File tempFile = new File(Environment.getExternalStorageDirectory(),getPhotoFileName());
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
//初始化控件
private void init() {
img_btn = (ImageButton) findViewById(R.id.img_btn);
btn = (Button) findViewById(R.id.btn);
//为ImageButton和Button添加监听事件
img_btn.setOnClickListener(this);
btn.setOnClickListener(this);
}
//点击事件
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.img_btn:
showDialog();
break;
case R.id.btn:
showDialog();
break;
}
}
//提示对话框方法
private void showDialog() {
new AlertDialog.Builder(this)
.setTitle("头像设置")
.setPositiveButton("拍照", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
// 调用系统的拍照功能
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 指定调用相机拍照后照片的储存路径
intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(tempFile));
startActivityForResult(intent, PHOTO_REQUEST_TAKEPHOTO);
}
})
.setNegativeButton("相册", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");
startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
}
}).show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
switch (requestCode) {
case PHOTO_REQUEST_TAKEPHOTO:
startPhotoZoom(Uri.fromFile(tempFile), 150);
break;
case PHOTO_REQUEST_GALLERY:
if (data != null)
startPhotoZoom(data.getData(), 150);
break;
case PHOTO_REQUEST_CUT:
if (data != null)
setPicToView(data);
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
private void startPhotoZoom(Uri uri, int size) {
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", size);
intent.putExtra("outputY", size);
intent.putExtra("return-data", true);
startActivityForResult(intent, PHOTO_REQUEST_CUT);
}
//将进行剪裁后的图片显示到UI界面上
private void setPicToView(Intent picdata) {
Bundle bundle = picdata.getExtras();
if (bundle != null) {
Bitmap photo = bundle.getParcelable("data");
Drawable drawable = new BitmapDrawable(photo);
img_btn.setBackgroundDrawable(drawable);
}
}
// 使用系统当前日期加以调整作为照片的名称
private String getPhotoFileName() {
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat("'IMG'_yyyyMMdd_HHmmss");
return dateFormat.format(date) + ".jpg";
}
}
相关推荐
Android中从SD卡中拍照选择图片并进行剪裁的方法
android gallery 图片轮播 展示SD卡下指定目录的图片 可删除
要开发一个头像上传的模块,头像上传过程分两步。第一步,相机拍照或者从图库选取照片,产生一个照片,第二步,提供头像剪裁,一般是剪裁为...图库选择照片这个可以自己做,访问sd卡,比较简单。问题出在图片剪裁上。
但是每款定制过的android 系统的外置SD卡的路径都不一样,那我们怎么才能去获取这个路径呢,我们可以想其它的办法,我这里提供了一个类可以获取外置SD卡或内置SD卡的 label(名称),path(路径),mount_point(挂载点)...
android开发 点击按钮从sd卡选择图片显示在ImageView
android读取sd卡中MP3文件
android 内部存储 sd卡app私有文件 等
AndroidQ的SD卡读取权限问题解决方法 在学Android中,前面的学习都没有什么问题,一遇到权限问题就蒙了,网上找了好久的,说是Android6.0以上要动态获取权限,我尝试了但是对我项目没有用。 于是我又找又找,终于...
webview可以获取SD卡图片并显示,且完美解决Android4.4以后webview无法访问SD卡图片的问题。
AndroidGallery从SD卡中获取图片,并显示
android 拍照上传 SD卡图片上传 Httppost上传
**Android Gallery:从SD卡获取图片并展示.rar文件** 本项目为一款基于Android平台的Gallery应用,用户可从SD卡中检索并查看图片,同时支持.rar格式文件的预览。项目采用Kotlin语言编写,兼容Android Studio开发...
一款用android读取sd卡中的图片的小程序
android 网络获取图片并在SD卡上保存与读取
简单的android 闹钟选择SD卡音乐铃声 android 闹钟选择SD卡音乐铃声 android 闹钟选择SD卡音乐铃声
android读取sd卡中的图片.使用ViewImage显示
此项目实现Android从SD卡获取图片,进行自动轮播,且支持滑动,欢迎大家下载
Android模拟器中添加SD卡,用于在电脑上模拟android系统添加sd卡
android 读取和存储sd卡一个例子,欢迎下载!
Android上SD卡图片的标准缓存方法