您的位置:首页 > 移动开发 > Android开发

Android从相机或相册获取图片裁剪

2013-08-19 14:10 381 查看
package
com.only.android.app;
02
03
import
java.io.File;
04
05
import
android.app.Activity;
06
import
android.app.AlertDialog;
07
import
android.content.DialogInterface;
08
import
android.content.Intent;
09
import
android.graphics.Bitmap;
10
import
android.graphics.BitmapFactory;
11
import
android.net.Uri;
12
import
android.os.Bundle;
13
import
android.os.SystemClock;
14
import
android.provider.MediaStore;
15
import
android.view.View;
16
import
android.widget.Button;
17
import
android.widget.ImageView;
18
19
import
com.only.android.R;
20
21
public
class
CopyOfImageScaleActivity
extends
Activity
implements
View.OnClickListener
{
22
/**
Called when the activity is first created. */
23
private
Button
selectImageBtn;
24
private
ImageView
imageView;
25
 
26
private
File
sdcardTempFile;
27
private
AlertDialog
dialog;
28
private
int
crop
=
180
;
29
30
@Override
31
public
void
onCreate(Bundle
savedInstanceState) {
32
super
.onCreate(savedInstanceState);
33
setContentView(R.layout.imagescale);
34
35
selectImageBtn
= (Button) findViewById(R.id.selectImageBtn);
36
imageView
= (ImageView) findViewById(R.id.imageView);
37
38
selectImageBtn.setOnClickListener(
this
);
39
sdcardTempFile
=
new
File(
"/mnt/sdcard/"
,
"tmp_pic_"
+
SystemClock.currentThreadTimeMillis() +
".jpg"
);
40
41
}
42
43
@Override
44
public
void
onClick(View
v) {
45
if
(v
== selectImageBtn) {
46
if
(dialog
==
null
)
{
47
dialog
=
new
AlertDialog.Builder(
this
).setItems(
new
String[]
{
"相机"
,
"相册"
},
new
DialogInterface.OnClickListener()
{
48
@Override
49
public
void
onClick(DialogInterface
dialog,
int
which)
{
50
if
(which
==
0
)
{
51
Intent
intent =
new
Intent(
"android.media.action.IMAGE_CAPTURE"
);
52
intent.putExtra(
"output"
,
Uri.fromFile(sdcardTempFile));
53
intent.putExtra(
"crop"
,
"true"
);
54
intent.putExtra(
"aspectX"
,
1
);
//
裁剪框比例
55
intent.putExtra(
"aspectY"
,
1
);
56
intent.putExtra(
"outputX"
,
crop);
//
输出图片大小
57
intent.putExtra(
"outputY"
,
crop);
58
startActivityForResult(intent,
101
);
59
}
else
{
60
Intent
intent =
new
Intent(
"android.intent.action.PICK"
);
61
intent.setDataAndType(MediaStore.Images.Media.INTERNAL_CONTENT_URI,
"image/*"
);
62
intent.putExtra(
"output"
,
Uri.fromFile(sdcardTempFile));
63
intent.putExtra(
"crop"
,
"true"
);
64
intent.putExtra(
"aspectX"
,
1
);
//
裁剪框比例
65
intent.putExtra(
"aspectY"
,
1
);
66
intent.putExtra(
"outputX"
,
crop);
//
输出图片大小
67
intent.putExtra(
"outputY"
,
crop);
68
startActivityForResult(intent,
100
);
69
}
70
}
71
}).create();
72
}
73
if
(!dialog.isShowing())
{
74
dialog.show();
75
}
76
}
77
}
78
79
@Override
80
protected
void
onActivityResult(
int
requestCode,
int
resultCode,
Intent intent) {
81
if
(resultCode
== RESULT_OK) {
82
Bitmap
bmp = BitmapFactory.decodeFile(sdcardTempFile.getAbsolutePath());
83
imageView.setImageBitmap(bmp);
84
}
85
}
86
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息