动态加载拍照和本地图片并移除的方法

来源:互联网 发布:js控制元素隐藏 编辑:程序博客网 时间:2024/05/19 23:59

这是一个类似发朋友圈的时候添加的照片,选错了可以删除的功能,现在很多应用都有这样的功能,废话不多直接上代码

布局文件没有过多废话,一目了然

  <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;        }    }
2 0
原创粉丝点击