博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android实现拍照与打开本地图片
阅读量:6567 次
发布时间:2019-06-24

本文共 1794 字,大约阅读时间需要 5 分钟。

代码如下:

publicclass MainActivity extends Activity {

    private Button btnCamera;

    private Button btnLocalPic;

    private ImageView imageView;

 

    @Override

    protectedvoid onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);

        setContentView(R.layout.mainactivity);

        btnCamera = (Button) this.findViewById(R.id.btnCamera);

        btnLocalPic = (Button) this.findViewById(R.id.btnlocalPic);

        imageView = (ImageView) this.findViewById(R.id.imageView1);

 

        btnCamera.setOnClickListener(new OnClickListener() {

 

            @Override

            publicvoid onClick(View arg0) {

                // TODO Auto-generated method stub

                Intent intent = new Intent(

                        android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                startActivityForResult(intent, 1000);

            }

        });

 

        btnLocalPic.setOnClickListener(new OnClickListener() {

 

            @Override

            publicvoid onClick(View arg0) {

                // TODO Auto-generated method stub

                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

                intent.setType("image/*");

                intent.putExtra("crop", true);

                intent.putExtra("return-data", true);

                startActivityForResult(intent, 1001);

            }

        });

    }

 

    @Override

    protectedvoid onActivityResult(int requestCode, int resultCode, Intent data) {

        // TODO Auto-generated method stub

        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1000 && resultCode == RESULT_OK) {

            Bundle bundle = data.getExtras();

            Bitmap bm = (Bitmap) bundle.get("data");

            imageView.setImageBitmap(bm);

        } elseif (requestCode == 1001 && resultCode == RESULT_OK) {

            Uri uri = data.getData();

            ContentResolver contentResolver = getContentResolver();

            try {

                Bitmap bm = BitmapFactory.decodeStream(contentResolver

                        .openInputStream(uri));

                imageView.setImageBitmap(bm);

            } catch (Exception e) {

                // TODO: handle exception

                e.printStackTrace();

            }

        }

    }

}

转载于:https://www.cnblogs.com/Yellow0-0River/p/4240617.html

你可能感兴趣的文章
git修改远程仓库地址
查看>>
Guess the number
查看>>
iscsi网络存储
查看>>
团队随笔
查看>>
Java内存块说明
查看>>
List集合具体对象的特点
查看>>
网络信息安全之防火墙***检测方法 (五)
查看>>
怎样为用户写“招标书”
查看>>
1.7 文件目录管理及相关的命令使用方法
查看>>
实际案例告诉你大数据在农业中如何应用
查看>>
LAMP优化策略
查看>>
PDF中添加页面/合并 PDF 内容
查看>>
JS仿FLASH特效可跳转回首页的CSS二级联动菜单
查看>>
页面导入样式时,使用link和@import有什么区别?
查看>>
类成员与类的实例成员
查看>>
Spark源码编译并在YARN上运行WordCount实例
查看>>
Spring AOP + AspectJ annotation example
查看>>
Spring VS EJB 3 的若干认识误区(转)
查看>>
React.js初探(一)
查看>>
Neo4j CQL -(17)- NULL值
查看>>