Android开发 调用照相机实现代码和注意事项

准备工作:设置 调用相机的回调码 和 设置 调用照相机拍照后保存图片的位置,名称,及后缀名(图片类型)

	private int xiangji=3;
private  File sdcardTempFile = new File("/mnt/sdcard/", "tmp_pic_" + SystemClock.currentThreadTimeMillis() + ".jpg");


在button监听器中调用相机:

	//调用系统照相机拍照
		bt4.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);  
				Uri u=Uri.fromFile(sdcardTempFile); 
				intent.putExtra(MediaStore.Images.Media.ORIENTATION, 0); 
				intent.putExtra(MediaStore.EXTRA_OUTPUT, u); 
				intent.putExtra("return-data", true);
				startActivityForResult(intent, xiangji); 

			}
		});

回调函数中接收图片:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	 super.onActivityResult(requestCode, resultCode, data); 
    	if (resultCode == RESULT_OK) {
    		System.out.println("requestCode"+requestCode);
    		
    		switch (requestCode){
    		
    		case 2:
    			 Uri tuku_uri = data.getData();
        		 System.out.println(tuku_uri.getPath());
        		   ContentResolver tuku_cr = this.getContentResolver();
        		   try {
    				bmp = BitmapFactory.decodeStream(tuku_cr.openInputStream(tuku_uri));
    					MCShareLaunchShareHelper.shareContentWithBitmap("测试分享本地图片", bmp, "your share url", "", MoxunActivity.this);
    			} catch (FileNotFoundException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    			break;
    		case 3:
        		   try {
        			   Uri xiangji_uri = Uri.parse(android.provider.MediaStore.Images.Media.insertImage(getContentResolver(), sdcardTempFile.getAbsolutePath(), null, null)); 
        			   System.out.println(xiangji_uri.getPath());
              		   ContentResolver xiangji_cr = this.getContentResolver();
              		   
              		   bmp = BitmapFactory.decodeStream(xiangji_cr.openInputStream(xiangji_uri));
    					MCShareLaunchShareHelper.shareContentWithBitmap("测试照相机图片", bmp, "your share url", "", MoxunActivity.this);
    			} catch (FileNotFoundException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
//方法二
//if (requestCode == MyApp.CAMERA_RECODE) {
//				try {
//					bmp=BitmapFactory.decodeFile(sdcardTempFile.getAbsolutePath());
//					img.setImageBitmap(bmp);
//					picCount++;
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//
//    			break;
//    		}
//    }

    } 




你可能感兴趣的:(Android_,android,相机)