Android拍照Camera(二)
2016-02-17 14:07
375 查看
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:android_custom="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="btn" > </Button> <ImageView android:id="@+id/img" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/btn" android:scaleType="center" > </ImageView> </RelativeLayout>
public class MainActivity extends Activity implements OnClickListener {
private static final int REQUEST_CODE_IMAGE_CATURE = 0x11;
private static final int REQUEST_CODE_IMAGE_CATURE_CROP = 0x12;
ImageView img;
Uri imgUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.paomadeng);
img = (ImageView) findViewById(R.id.img);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn) {
takePicture();
}
}
/**
* 拍摄照片并保存
*/
public void takePicture() {
String imgPath = Environment.getExternalStorageDirectory() + File.separator + "tmp.png";
File file = new File(imgPath);
if (file.exists()) {
file.delete();
}
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
imgUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
startActivityForResult(intent, REQUEST_CODE_IMAGE_CATURE);
}
/**
* 裁剪图片
*/
public void cropPicture() {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(imgUri, "image/*");
intent.putExtra("scale", true);
intent.putExtra("outputX", 300);
intent.putExtra("outputY", 300);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
startActivityForResult(intent, REQUEST_CODE_IMAGE_CATURE_CROP);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK) {
return;
}
if (requestCode == REQUEST_CODE_IMAGE_CATURE) {
cropPicture();
} else if (requestCode == REQUEST_CODE_IMAGE_CATURE_CROP) {
InputStream is = null;
try { // 根据文件Uri,读取输入流
is = getContentResolver().openInputStream(imgUri);
Bitmap bitmap = BitmapFactory.decodeStream(is);
img.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
相关文章推荐
- android事件分发的梗?
- Notification bar icon turns white in Android 5 Lollipop
- Android之.9图片——Drawable、BitmapDrawable、NinePatchDrawable之间转换
- 快速Android开发系列通信篇之EventBus(1)
- 干货分享:分析Android应用使用的技术框架和开源库
- 2015年十大热门Android开源新项目
- 通过android studio 支持各版本使用矢量图 (SVG)
- 【Android测试】【随笔】模拟长按电源键
- 【高德地图Android SDK】视频教学
- android sdk的下载以及安装
- 2.7-3 Android Studio 的Gradle一点理解, 查看gradle 版本和android 插件的版本
- Activity的四种LaunchMode和Intent 的flag的使用
- 这些年正Android - 身在他乡
- Android如何分析开源项目
- 2.7-2 Android studio gradle 文件 配置完整版
- Android开发:Translucent System Bar 的最佳实践
- 几行代码,让你的app动感起来--Android Design Support Library使用
- android studio SDK与版本更新
- 开发阶段总结
- Android蓝牙的开启-搜索-关闭演示