gpt4 book ai didi

android - 相机应用程序在 android ffmpeg 应用程序上失败

转载 作者:行者123 更新时间:2023-12-04 23:20:50 26 4
gpt4 key购买 nike

我正在尝试在 android 上运行一个简单的录像机应用程序。代码如下:

package com.example.camera

//import android.R
import android.content.DialogInterface
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.arthenica.mobileffmpeg.FFmpeg


const val EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE"

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (checkPermission()) {
//main logic or main code
FFmpeg.execute("-f android_camera -i 0:0 -r 30 -pixel_format bgr0 -t 00:00:05 /sdcard/test.mp4")

// . write your main code to execute, It will execute if the permission is already given.
} else {
requestPermission()
}
}

private fun checkPermission(): Boolean {
return if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED
) {
// Permission is not granted
false
} else true
}

private fun requestPermission() {
ActivityCompat.requestPermissions(
this, arrayOf(android.Manifest.permission.CAMERA),
PERMISSION_REQUEST_CODE
)
}

override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>,
grantResults: IntArray
) {
when (requestCode) {
PERMISSION_REQUEST_CODE -> if (grantResults.size > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED
) {
Toast.makeText(applicationContext, "Permission Granted", Toast.LENGTH_SHORT)
.show()

// main logic
} else {
Toast.makeText(applicationContext, "Permission Denied", Toast.LENGTH_SHORT)
.show()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED
) {
showMessageOKCancel("You need to allow access permissions",
DialogInterface.OnClickListener { dialog, which ->
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermission()
}
})
}
}
}
}
}

private fun showMessageOKCancel(
message: String,
okListener: DialogInterface.OnClickListener
) {
AlertDialog.Builder(this@MainActivity)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show()
}

companion object {
private const val PERMISSION_REQUEST_CODE = 200
}
}


调用录像机的主要命令在这里:
           FFmpeg.execute("-f android_camera -i 0:0 -r 30 -pixel_format bgr0 -t 00:00:05 /sdcard/test.mp4")
该应用程序在我的 android 10 Motorola G Power 上打开。我点击“允许”以允许权限。然后应用程序崩溃,我不断收到此错误:
2021-03-22 13:42:51.534 31138-31138/com.example.camera E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.camera, PID: 31138
java.lang.IllegalStateException: Could not find method sendMessage(View) in a parent or ancestor Context for android:onClick attribute defined on view class com.google.android.material.button.MaterialButton with id 'button2'
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:436)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:393)
at android.view.View.performClick(View.java:7161)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:967)
at android.view.View.performClickInternal(View.java:7133)
at android.view.View.access$3500(View.java:804)
at android.view.View$PerformClick.run(View.java:27416)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:241)
at android.app.ActivityThread.main(ActivityThread.java:7617)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:941)
2021-03-22 13:42:51.546 31138-31138/com.example.camera I/Process: Sending signal. PID: 31138 SIG: 9
我究竟做错了什么?请指教。
编辑
布局xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:ems="10"
android:inputType="textPersonName"
android:text="@string/edit_message"
app:layout_constraintBaseline_toBaselineOf="@+id/button2"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:onClick="sendMessage"
android:text="@string/button_send"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/editText"
tools:layout_editor_absoluteY="16dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

最佳答案

由于您没有发布布局 xml,我假设您在 xml 中为按钮注册了单击方法并调用了方法 sendMessage .
你实际上并没有在你的 Activity 中创建方法
在您的按钮中

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:onClick="sendMessage"
android:text="@string/button_send"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/editText"
tools:layout_editor_absoluteY="16dp" />
你有属性 android:onClick="sendMessage"所以现在你需要为你的 Activity 添加一个方法。
fun sendMessage(view:View){

}

关于android - 相机应用程序在 android ffmpeg 应用程序上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66751257/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com