动态加载拍照和本地图片并移除的方法
2017-03-31 19:51
183 查看
这是一个类似发朋友圈的时候添加的照片,选错了可以删除的功能,现在很多应用都有这样的功能,废话不多直接上代码
布局文件没有过多废话,一目了然
逻辑清晰明了,不懂留言
布局文件没有过多废话,一目了然
<Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="照相机" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="图库" /> <ImageView android:id="@+id/imageview1" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="删除" />
逻辑清晰明了,不懂留言
private Button button1, button2, button3; private ImageView imageView; private final int PICK = 1;// 选择图片库 private final int IMAGE_RESULT_CODE = 2;// 表示打开照相机 private Bitmap bitmap; // 使用意图回传值的结果码 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1 = (Button) findViewById(R.id.button1); button2 = (Button) findViewById(R.id.button2); button3 = (Button) findViewById(R.id.button3); imageView = (ImageView) findViewById(R.id.imageview1); button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // 使用意图 直接调用安装在手机上的照相机 // 直接开发Camera硬件 Intent intent = new Intent( android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, IMAGE_RESULT_CODE);// 打开照相机 } }); button2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { Intent intent = new Intent( Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, PICK);// 打开照相机 } }); button3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { imageView.setImageDrawable(null); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { // 表示 调用照相机拍照 case IMAGE_RESULT_CODE: if (resultCode == RESULT_OK) { Bundle bundle = data.getExtras(); bitmap = (Bitmap) bundle.get("data"); imageView.setImageBitmap(bitmap); } break; // 选择图片库的图片 case PICK: if (resultCode == RESULT_OK) { Uri uri = data.getData(); imageView.setImageURI(uri); } break; } }
相关文章推荐
- VC中加载GIF格式动态图片的详细方法!
- 解决IE6下JS动态设置图片IMG的SRC时图片无法加载错误的方法
- [WinForm]dataGridView动态加载以本地图片显示列
- C#实现动态显示及动态移除图片方法
- FileReference的load()方法加载本地图片
- CBitmap程序中动态加载 本地文件上的BMP图片资源
- android异步加载图片并缓存到本地实现方法
- JQ获取动态加载的图片大小的正确方法分享
- [unity基础教程]关于Unity3D中Resources动态加载NGUI图片的方法!
- 关于Unity3D中Resources动态加载NGUI图片的方法!!!
- RDLC加载动态图片和文本的方法
- 关于Unity3D中Resources动态加载NGUI图片的方法
- FileReference的load()方法加载本地图片
- JQ获取动态加载的图片大小的正确方法分享
- 解决IE6下JS动态设置图片IMG的SRC时图片无法加载错误的方法
- VC中加载GIF格式动态图片的详细方法!
- CBitMap动态加载本地文件夹中的BMP图片
- Delphi中动态加载Image控件图片的方法
- 用户控件动态加载图片问题解决方法!
- 关于Unity3D中Resources动态加载NGUI图片的方法!!!