• 相机的简单使用


    public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    
        private Button button;
        private Button button2;
        private ImageView imageView;
        private String sdpath;
        private String picpath;
        private static int SLT=1;//请求缩略图的标识
        private static int YT=2;//请求原图的标识
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            sdpath= Environment.getExternalStorageDirectory().getPath();
            picpath= sdpath+"/"+"temp.png";
    
            initView();
    
    
        }
    
        private void initView() {
            button = (Button) findViewById(R.id.button);
            button2 = (Button) findViewById(R.id.button2);
            imageView = (ImageView) findViewById(R.id.imageView);
    
            button.setOnClickListener(this);
            button2.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.button:
    //              Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    Intent intent = new Intent(Intent.ACTION_PICK, null);
                    intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                    startActivityForResult(intent,YT);
                    break;
                case R.id.button2:
                    Intent intent2=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    //定义一个路径
                    Uri uri=Uri.fromFile(new File(picpath));
                    //为拍照指定一个路径
                    intent2.putExtra(MediaStore.EXTRA_OUTPUT,uri);
                    startActivityForResult(intent2,YT);
    
                    break;
            }
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode==RESULT_OK){
                if (requestCode==SLT){
                    //通过Bundle 取图片
                    Bundle extras = data.getExtras();
                    Bitmap bitmap = (Bitmap) extras.get("data");
                    imageView.setImageBitmap(bitmap);
                }else if (requestCode==YT){
                    FileInputStream fileInputStream=null;
                    try {
                        fileInputStream=new FileInputStream(picpath);
                        //把流转化为BITMAP
                        Bitmap bitmap= BitmapFactory.decodeStream(fileInputStream);
                        imageView.setImageBitmap(bitmap);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }finally {
                        try {
                            fileInputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
    
        }
    }
    记住添加权限
    <uses-permission android:name="android.permission.CAMERA"></uses-permission>
     
  • 相关阅读:
    盘古越狱工具在用户空间的行为
    hdu 5538 House Building(长春现场赛——水题)
    html 锚点定位
    OOP版电子词典
    有趣的JavaScript原生数组函数
    &lt;LeetCode OJ&gt; 121. /122. Best Time to Buy and Sell Stock(I / II)
    hadoop 出现FATAL conf.Configuration: error parsing conf file,异常
    IT痴汉的工作现状10-Sprint Planning
    2015 Astar Contest
    无法使用BIPublisher开发报表
  • 原文地址:https://www.cnblogs.com/SongYongQian/p/7822736.html
Copyright © 2020-2023  润新知