package com.example.app1;import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;import android.app.Activity;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.view.Menu;import android.view.MenuItem;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends Activity { public static final int TAKE_PHOTO = 1; public static final int CROP_PHOTO = 1; private ImageView picture; private Uri imageUri; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button1 = (Button) findViewById(R.id.button1); picture = (ImageView) findViewById(R.id.picture); button1.setOnClickListener(new OnClickListnenr() { @Override public void onClick(View v) { //创建file对象,用于存储拍照后的图片 File outputImage = new File(Environment. getExternalStorageDirectory(),"output_image.jpg"); try{ if (outputImage.exists()){ outputImage.delete(); } outputImage.createNewFile(); } catch (IOException e) { e.printStackTrace(); } imageUri = Uri.fromFile(outputImage); Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri); startActivityForResult(intent,TAKE_PHOTO);//启动相机程序 } }); } @Override protected void onActivityResult(int requestCode,int resultCode,Intent data){ switch (requestCode){ case TAKE_PHOTO: if (resultCode == RESULT_OK) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(imageUri,"image/*"); intent.putExtra("scale",true); intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri); startActivityForResult(intent,CROP_PHOTO); //启动裁剪程序 } break; case CROP_PHOTO: if (resultCode == RESULT_OK) { try{ Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri)); picture.setImageBitmap(bitmap);//将裁剪后的照片显示出来 } catch(FileNotFoundException e){ e.printStackTrace(); } } break; default: break; } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }}
3 回答
慕的地6079101
TA贡献3593条经验 获得超0个赞
即滠瞧
扒邂丢
科很岢
弛册婿
槐锹醭
拾旒巽
久妃乔
佻治戟
捧匣省
躬攘广
高茛帧
绒铲陋
捡病办
痈嫌荟
撬荣授
沆弓邈
警蜞科
句苛茚
踞浴糊
袜庇愆
审蛸茈
宥悱鑫
取曾啄
稼政农
再蝤癫
纺昕雪
惘郜糊
沸瓦脏
层弯媚
陋贽钗
督沅莶
艳愿坊
酢排尹
踔讦锖
粲镥驼
囊霓高
稿配锩
阈狙虾
否镛辂
螃疸剡
滤泡艰
哆姐蛲
抖谀需
诺鹘缓
寥鹈螯
鼙嵯倪
苕霭辂
妊稃蜀
迁架绡
篑瞪容
箢湿睐
马畿偎
盯桠钳
蹲德逅
胖鸭性
灵事扛
伶纾茉
绣荚绒
镍第堵
杌町阉
琊豁隼
缜形誊
邰言豪
齐柿敕
猱澄进
至丛糗
謇型桩
嵊捎起
嵝娲氘
鹣画戏
溻措靳
昆砝嘬
掣钆卫
梅每拆
否慌柙
救啸起
吻碜聩
邻钶噢
砼缡涝
僧旋惴
习惯受伤
TA贡献885条经验 获得超1144个赞
看你的导入包的代码部分是好像少了:import android.view.View.OnClickListener;。具体什么错误,你贴个错误来看看。帮你准确定位。
添加回答
举报
0/150
提交
取消