在片段中使用相机 Android
Using the Camera in a fragment Android
我想使用我的一个应用程序片段中的相机功能来拍照。截至目前,每当我尝试进入另一个选项卡而没有任何错误消息或警告时,应用程序会立即关闭。
对不起,这可能是非常明显的事情,但这是我第一次尝试编程。
这是我的片段代码:
class ScanFragment : Fragment() {
private val REQUEST_CODE = 69
private lateinit var imageView: ImageView
private lateinit var btnTakePicture: Button
val Fragment.packageManager get() = activity?.packageManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
btnTakePicture.setOnClickListener{
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(takePictureIntent, REQUEST_CODE)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK){
val takenImage = data?.extras?.get("data") as Bitmap
imageView.setImageBitmap(takenImage) //Foto wird im ImageView dargestellt
} else {
super.onActivityResult(requestCode, resultCode, data)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
)
: View? {
// Inflate the layout for this fragment
val v = inflater.inflate(R.layout.fragment_scan, container, false);
btnTakePicture = v.findViewById(R.id.btnTakePicture)
imageView = v.findViewById(R.id.imageView)
return v
}
}
'''
我不知道如何在 Kotlin 中执行此操作,但我可以在 Java
中向您展示
在你的片段上:
private ParentActivityClass myParentContext;
@Override
public void onAttach(@NotNull Context context) {
super.onAttach(context);
myParentContext= (ParentActivityClass) context;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
btnTakePicture.setOnClickListener(v -> {
Intent intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
myParentContext.startActivityForResult(intent, REQUEST_CODE)
});
}
我认为,它会起作用。
我想使用我的一个应用程序片段中的相机功能来拍照。截至目前,每当我尝试进入另一个选项卡而没有任何错误消息或警告时,应用程序会立即关闭。
对不起,这可能是非常明显的事情,但这是我第一次尝试编程。
这是我的片段代码:
class ScanFragment : Fragment() {
private val REQUEST_CODE = 69
private lateinit var imageView: ImageView
private lateinit var btnTakePicture: Button
val Fragment.packageManager get() = activity?.packageManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
btnTakePicture.setOnClickListener{
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(takePictureIntent, REQUEST_CODE)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK){
val takenImage = data?.extras?.get("data") as Bitmap
imageView.setImageBitmap(takenImage) //Foto wird im ImageView dargestellt
} else {
super.onActivityResult(requestCode, resultCode, data)
}
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
)
: View? {
// Inflate the layout for this fragment
val v = inflater.inflate(R.layout.fragment_scan, container, false);
btnTakePicture = v.findViewById(R.id.btnTakePicture)
imageView = v.findViewById(R.id.imageView)
return v
}
} '''
我不知道如何在 Kotlin 中执行此操作,但我可以在 Java
中向您展示在你的片段上:
private ParentActivityClass myParentContext;
@Override
public void onAttach(@NotNull Context context) {
super.onAttach(context);
myParentContext= (ParentActivityClass) context;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
btnTakePicture.setOnClickListener(v -> {
Intent intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
myParentContext.startActivityForResult(intent, REQUEST_CODE)
});
}
我认为,它会起作用。