重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Android开发中如何实现调用Camera?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
创新互联公司是一家专业提供乌达企业网站建设,专注与成都做网站、成都网站制作、html5、小程序制作等业务。10年已为乌达众多企业、政府机构等服务。创新互联专业网站制作公司优惠进行中。
xml:
activity:
package com.wj.cameratest; import java.io.File; import android.app.Activity; import android.content.Intent; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class CameraShowActivity extends Activity { private ImageView mImageView; private Button mButtonCamera; private Button mButtonPhoto; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_camera_show); mImageView = (ImageView) this.findViewById(R.id.imageview_preview); mButtonCamera = (Button) this.findViewById(R.id.button_cameraButton); mButtonPhoto = (Button) this.findViewById(R.id.button_photoButton); mButtonCamera.setOnClickListener(new OnClickListener() { //打开Camera @Override public void onClick(View v) { Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment .getExternalStorageDirectory(), "camera.jpg"))); intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0); startActivityForResult(intent, 10); } }); mButtonPhoto.setOnClickListener(new OnClickListener() { //获取相册 @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX",1); intent.putExtra("aspectY",1); intent.putExtra("outputX", 80); intent.putExtra("outputY", 80); intent.putExtra("return-data",true); startActivityForResult(intent, 11); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 10 && resultCode == Activity.RESULT_OK) { this.mImageView.setImageDrawable(Drawable.createFromPath(new File( Environment.getExternalStorageDirectory(), "camera.jpg") .getAbsolutePath())); System.out.println("data-->"+data); }else if (requestCode == 11 && resultCode ==Activity.RESULT_OK) { System.out.println("data2-->"+data); } } }
Manifest.xml
android 调用相册里的图片并返回
Intent intent=new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", 80); intent.putExtra("outputY", 80); intent.putExtra("return-data", true); startActivityForResult(intent, 0);
在原来的Activity中如下获取选到的图片:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { System.out.println(resultCode); Bitmap cameraBitmap = (Bitmap) data.getExtras().get("data"); super.onActivityResult(requestCode, resultCode, data); }
看完上述内容,你们掌握Android开发中如何实现调用Camera的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!