Imageview 打开相机
Posted
技术标签:
【中文标题】Imageview 打开相机【英文标题】:Imageview Open Camera 【发布时间】:2017-05-24 12:39:43 【问题描述】:我想在图像视图上添加点击事件。通过单击 On Image View 它应该会打开一个适用于 android 的相机。
ImageView imageView = (ImageView) findViewById(R.id.ImageView1);
imageView.setImageResource(R.drawable.camera);
【问题讨论】:
Button Click Listeners in Android的可能重复 你也可以使用这个链接来拍照***.com/questions/13977245/… 【参考方案1】: imageView = (ImageView) findViewById(R.id.ImageView1);
imageView.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent imageLoader = (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT)
? new Intent(MediaStore.ACTION_IMAGE_CAPTURE_SECURE)
: new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(imageLoader, 1);
);
处理请求:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
if (resultCode == RESULT_OK)
switch (requestCode)
case 1:
your_image_container_to_set_image.setImageBitmap((Bitmap) data.getExtras().get("data")); //use this if you trying to set image on Imageview
break;
您可能需要使用Permission
:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
【讨论】:
【参考方案2】:File file;
imageView = (ImageView) findViewById(R.id.ImageView1);
imageView.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
takePicture(v);
);
public void takePicture(View view)
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
file = Uri.fromFile(getOutputMediaFile());
intent.putExtra(MediaStore.EXTRA_OUTPUT, file);
startActivityForResult(intent, 100);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
if (requestCode == 100)
if (resultCode == RESULT_OK)
imageView.setImageURI(file);
private static File getOutputMediaFile()
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "CameraDemo");
if (!mediaStorageDir.exists())
if (!mediaStorageDir.mkdirs())
return null;
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
return new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
【讨论】:
以上是关于Imageview 打开相机的主要内容,如果未能解决你的问题,请参考以下文章
Android调用相机拍摄照片并显示到 ImageView控件中