我正在尝试使用手机相机拍照并将照片设置为我的布局中的imageview。我是Android的新手,我已经做过研究,但却找不到任何结论。
当我运行应用程序并点击相机按钮时,我在logcat中收到此错误:
android.content.ActivityNotFoundException:找不到处理Intent的活动{act = android.provider.MediaStore.IMAGE_CAPTURE}
这是我的代码中具有相机意图的部分:
public class PicOfDay extends Fragment implements View.OnClickListener {
private static final int CAMERA_REQUEST = 1888;
public ImageView picOfDay;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.pic_of_day, container, false);
picOfDay.setOnClickListener(this);
setHasOptionsMenu(true);
return view;
}
private void takePhoto() {
Intent camIntent = new Intent("android.provider.MediaStore.IMAGE_CAPTURE");
startActivityForResult(camIntent, CAMERA_REQUEST);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
picOfDay.setImageBitmap(photo);
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_picofday, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.camera) {
takePhoto();
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
我认为错误可能是由您使用
引起的" IMAGE_CAPTURE"而不是" ACTION_IMAGE_CAPTURE"请尝试使用此代码:
Intent camIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
另外(android.provider.MediaStore.Action_IMAGE_CAPTURE);不应该在引号中。见Android Docs
详情。
答案 1 :(得分:0)
Intent操作错误:应该是"android.media.action.IMAGE_CAPTURE"
或者@Keylwin说的,MediaStore.ACTION_IMAGE_CAPTURE
(没有引号应该是一个常量值),以便直接兼容。
请阅读this:
private void takePhoto() {
Intent camIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // "android.media.action.IMAGE_CAPTURE"
startActivityForResult(camIntent, CAMERA_REQUEST);
}