这是我的 CameraFragment 中的代码,我希望它传递/共享 捕获的图像到我的 CrimeListFragment < / strong>(我的其他活动)。。我不知道该如何称呼。任何愿意帮助的人?
private lateinit var imageView: ImageView
private lateinit var button: Button
private lateinit var next: ImageButton
private var photoFile: File? = null
private val CAPTURE_IMAGE_REQUEST = 1
private lateinit var mCurrentPhotoPath: String
private val IMAGE_DIRECTORY_NAME = "DCIM"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.fragment_camera)
imageView = findViewById(id.imageView)
button = findViewById(R.id.btnCaptureImage)
next = findViewById(R.id.imgBtnNext)
/*clicking button camera*/
button.setOnClickListener {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
captureImage()
} else {
captureImage2()
}
}
next.setOnClickListener {
if (imageView.getDrawable() == null){
Toast.makeText(this,"Empty Image", Toast.LENGTH_SHORT).show()
}
else {
val intent = Intent(this, CrimeListFragment::class.java)
startActivity(intent)
}
}
}
/* Capture Image function for 4.4.4 and lower. Not tested for Android Version 3 and 2 */
private fun captureImage2() {
try {
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
photoFile = createImageFile4()
if (photoFile != null) {
displayMessage(baseContext, photoFile!!.getAbsolutePath())
Log.i("Mayank", photoFile!!.getAbsolutePath())
val photoURI = Uri.fromFile(photoFile)
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
startActivityForResult(cameraIntent, CAPTURE_IMAGE_REQUEST)
// intent.putExtra("crimePicture", photoFile)
}
} catch (e: Exception) {
displayMessage(baseContext, "Camera is not available." + e.toString())
}
}
private fun captureImage() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE), 0)
} else {
val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
if (takePictureIntent.resolveActivity(packageManager) != null) {
// Create the File where the photo should go
try {
photoFile = createImageFile()
displayMessage(baseContext, photoFile!!.getAbsolutePath())
Log.i("Mayank", photoFile!!.getAbsolutePath())
// Continue only if the File was successfully created
if (photoFile != null) {
var photoURI = FileProvider.getUriForFile(this,
"com.testcrimereportingapp.crimereportingapp69.fileprovider",
photoFile!!
)
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
startActivityForResult(takePictureIntent, CAPTURE_IMAGE_REQUEST)
}
} catch (ex: Exception) {
// Error occurred while creating the File
displayMessage(baseContext,"Capture Image Bug: " + ex.message.toString())
}
} else {
displayMessage(baseContext, "Nullll")
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
//Bundle extras = data.getExtras();
//Bitmap imageBitmap = (Bitmap) extras.get("data");
//imageView.setImageBitmap(imageBitmap);
if (requestCode == CAPTURE_IMAGE_REQUEST && resultCode == Activity.RESULT_OK) {
val myBitmap = BitmapFactory.decodeFile(photoFile!!.getAbsolutePath())
imageView.setImageBitmap(myBitmap)
} else {
displayMessage(baseContext, "Request cancelled or something went wrong.")
}
}
private fun createImageFile4(): File? {
// External sdcard location
val mediaStorageDir = File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
IMAGE_DIRECTORY_NAME)
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
displayMessage(baseContext, "Unable to create directory.")
return null
}
}
val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(Date())
return File(mediaStorageDir.path + File.separator
+ "IMG_" + timeStamp + ".jpg")
}
@Throws(IOException::class)
private fun createImageFile(): File {
// Create an image file name
val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
val imageFileName = "JPEG_" + timeStamp + "_"
val storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
val image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
)
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.absolutePath
return image
}
private fun displayMessage(context: Context, message: String) {
Toast.makeText(context, message, Toast.LENGTH_LONG).show()
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
if (requestCode == 0) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED
&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {
captureImage()
}
}
}
这是我第一次在此处对Stack Overflow发表问题。 非常感谢在那里有好心的人! 这些代码来自这个人https://www.youtube.com/watch?v=5wbeWN4hQt0&t=110s ..归功于他