问题描述
以下代码将打开相机,但会覆盖整个屏幕,并在捕获图像后返回到片段。没有单击图像的迹象。 (图片已安全地保存在图库中。我只想在片段部分中打开相机。我从。我不知道它在做什么,只是删除了'viewHolder'(try()的第4行),并在代码的顶部声明了URI变量。
我还尝试了其他答案,例如,但是它们与我一样复杂在API 19上开发。
Following code opens camera but covers complete screen, and after capturing image it returns to the fragment No signs of clicked image. ( image is safely saved in gallery. I want to open camera ONLY IN FRAGMENT part. I have copied the code from here. I just removed 'viewHolder' (4rth line of try()) as I don't know what it was doing. and declared URI variable at top in my code.
I also have tried other answers like this but they are complicated as I am developing on API 19.
//my declared variables
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 1888;
Button button;
ImageView imageView;
Uri imageUri;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
getActivity().startActivityForResult(intent, 100);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_scan, container, false);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 100:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = imageUri;
getActivity().getContentResolver().notifyChange(selectedImage, null);
ContentResolver cr = getActivity().getContentResolver();
Bitmap bitmap;
try {
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
imageView.setImageBitmap(bitmap);
Toast.makeText(getActivity(), selectedImage.toString(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getActivity(), "Failed to load", Toast.LENGTH_SHORT)
.show();
Log.e("Camera", e.toString());
}
}
}
}
推荐答案
可以。您正在启动第三方相机应用程序,这些应用程序通常会占据前台。
Sure. You are launching a third-party camera app, and those usually take over the foreground.
使用 android.hardware.Camera
从头开始编写自己的相机代码和/或 android.hardware.camera2。*
一组类。您无法将第三方相机应用程序启动成片段。
Write your own camera code from scratch, using android.hardware.Camera
and/or the android.hardware.camera2.*
set of classes. You cannot launch a third-party camera app into a fragment.
这篇关于如何仅以片段形式打开相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!