package com.example.mobilewarehousesystem.ui.import
import android.Manifest
import android.content.DialogInterface
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.os.Bundle
import android.provider.MediaStore
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultCallback
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.example.mobilewarehousesystem.databinding.FragmentImportBinding
import com.google.mlkit.vision.barcode.Barcode
import com.google.mlkit.vision.barcode.BarcodeScanner
import com.google.mlkit.vision.common.InputImage
import androidx.appcompat.app.AlertDialog
class ImportFragment : Fragment() {
//show UI
private var _binding: FragmentImportBinding?=null
private val binding get() = _binding!!
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
_binding = FragmentImportBinding.inflate(inflater, container, false)
setHasOptionsMenu(true)
return binding.root
}
private val CAMERA_PERMISSION_CODE=123
private val READ_STORAGE_PERMISSION_CODE=113
private val WRITE_STORAGE_PERMISSION_CODE=113
private lateinit var cameraLauncher: ActivityResultLauncher<Intent>
private lateinit var galleryLauncher: ActivityResultLauncher<Intent>
private val TAG = "My Tag"
lateinit var inputImage: InputImage
lateinit var barcodeScanner: BarcodeScanner
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
cameraLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult(), object :ActivityResultCallback<ActivityResult>{
override fun onActivityResult(result: ActivityResult?) {
val data=result?.data
try{
val photo = data?.extras?.get("data") as Bitmap
inputImage= InputImage.fromBitmap(photo, 0)
processQr()
}catch(e:Exception){
Log.d(TAG, "onActivityResult:"+e.message)
}
}
})
galleryLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult(), object :ActivityResultCallback<ActivityResult>{
override fun onActivityResult(result: ActivityResult?) {
val data=result?.data
inputImage = InputImage.fromFilePath(this@ImportFragment, data?.data)
processQr()
}
})
binding.btnScanBarcode.setOnClickListener{
val options=arrayOf("camera","gallery")
val builder=AlertDialog.Builder(this@ImportFragment)
builder.setTitle("Pick a option")
builder.setItems(options, DialogInterface.OnClickListener { dialog, which ->
if(which==0){
val cameraIntent=Intent(MediaStore.ACTION_IMAGE_CAPTURE)
cameraLauncher.launch(cameraIntent)
}else{
val storageIntent=Intent()
storageIntent.setType("image/*")
storageIntent.setAction(Intent.ACTION_GET_CONTENT)
galleryLauncher.launch(storageIntent)
}
})
builder.show()
}
}
private fun processQr(){
binding.ivQrCode.visibility=View.GONE
binding.tvResult.visibility=View.GONE
barcodeScanner.process(inputImage).addOnSuccessListener{
for (barcode:Barcode in it){
val valueType=barcode.valueType
when (valueType) {
Barcode.TYPE_WIFI -> {
val ssid = barcode.wifi!!.ssid
val password = barcode.wifi!!.password
val type = barcode.wifi!!.encryptionType
binding.tvResult.text = "ssid ${ssid} \n password ${password} \n type ${type}"
}
Barcode.TYPE_URL -> {
val title = barcode.url!!.title
val url = barcode.url!!.url
binding.tvResult.text = "title ${title} \n url ${url}"
}
Barcode.TYPE_TEXT->{
val data = barcode.displayValue
binding.tvResult.text="Result ${data}"
}
}
}
}.addOnFailureListener{
Log.d(TAG, "processQr: ${it.message}")
}
}
override fun onResume() {
super.onResume()
checkPermission(Manifest.permission.CAMERA,CAMERA_PERMISSION_CODE)
}
private fun checkPermission(permission:String, requestCode:Int){
if(ContextCompat.checkSelfPermission(this@ImportFragment, permission)== PackageManager.PERMISSION_DENIED){
ActivityCompat.requestPermissions(this@ImportFragment, arrayOf(permission),requestCode)
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if(requestCode==CAMERA_PERMISSION_CODE){
if(grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED){
checkPermission(
android.Manifest.permission.READ_EXTERNAL_STORAGE,
READ_STORAGE_PERMISSION_CODE)
}else{
Toast.makeText(this@ImportFragment, "Camera Permission Denied", Toast.LENGTH_SHORT).show()
}
}else if(requestCode==READ_STORAGE_PERMISSION_CODE){
if((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)){
checkPermission(
android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
WRITE_STORAGE_PERMISSION_CODE)
}else{
Toast.makeText(this@ImportFragment, "Storage Permission Denied", Toast.LENGTH_SHORT).show()
}
}else if(requestCode==WRITE_STORAGE_PERMISSION_CODE) {
if (!(grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
Toast.makeText(this@ImportFragment, "Storage Permission Denied", Toast.LENGTH_SHORT).show()
}
}
}
}
我包括7个错误,都是实现错误??这些代码是在知识有限的情况下,1比1地从主要活动转换成片段的,我找到了一些解决方案,但仍然不知道如何应用到我的代码中。
您必须使用上下文,而不是this@ImportFragment.
替换:
this@ImportFragment
具有
requireContext()
或者试着把
context!!
通常,如果您编写上下文
Android studio建议您将其替换为RequireText()
,这里有更多详细信息:
/**
* Return the {@link Context} this fragment is currently associated with.
*
* @throws IllegalStateException if not currently associated with a context.
* @see #getContext()
*/
@NonNull
public final Context requireContext() {
Context context = getContext();
if (context == null) {
throw new IllegalStateException("Fragment " + this + " not attached to a context.");
}
return context;
}
我对Kotlin是新来的,这是我的问题: 我使用android studio 3.2.1 kotlin版本:1.2.71 对此有什么想法吗?
问题内容: 我在文件Sandbox.java中具有以下类: Eclipse中的编译在第14行“无法推断map(Function)的类型参数”中显示错误。 使用纯javac(JDK 1.8.0_121)可以编译相同的代码而不会出现问题。 如果我将正确的行更改为: 然后代码将在Eclipse中编译而没有错误。 有谁知道为什么会有这样的行为?是虫子吗? 我使用Eclipse 4.6.2.20161208
我试图使用一个类型族来生成依赖于某个类型级别自然数的约束。下面是这样一个函数: 然后我有一个函数,它有这个约束。 当我试图在模式匹配中使用这个函数时,我的类型族应该产生这个约束,ghc说它不能推导出约束 下面是一个例子: 它会产生错误
代码如下: 我得到以下错误: /users/johann/work/dev/kotlin/rxjava-walkthrough-kotlin/app/src/test/java/ktplay/rxkotlinplay.kt:(49,14):不能使用提供的参数调用以下函数:@checkreturnvalue@schedulersupport public final fun subscribe(p0
我的公共类中有一个私有子类。这个私有类扩展了AsyncTask。在onPostExecute()中,我必须通过自定义适配器发送对象列表。但问题是,当我获取主公共类(私有子类的父类)的“上下文”时,它在@Override(OnPostExecute())上给出了一个错误。 它在@Override上给出了一个错误。我必须获取私有子类的父类的上下文(这扩展了asyncTask并具有onPostExecu
我试图构建查询投影,我遇到了以下错误消息: 导致问题的代码: 方法需要类型