相机不进入 onActivityResult
Posted
技术标签:
【中文标题】相机不进入 onActivityResult【英文标题】:Camera doesn't enter onActivityResult 【发布时间】:2017-10-17 03:24:00 【问题描述】:我正在尝试拍照并将图片发送到服务器,但目前我没有输入 onActivityResult 并且不知道发生了什么
代码如下:
public void capturar(View view)
Intent intent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
if (intent.resolveActivity(getPackageManager()) != null)
startActivityForResult(intent,REQUEST_IMAGE_CAPTURE);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK)
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
String encodedImage = encodeImage(imageBitmap);
Log.d("encoded",encodedImage);
sendImage(encodedImage);
我可以预览相机,但点击 android 照片并没有点击 activiyt 结果上的 Log.d,任何提示»?
LogCat:
05-16 23:50:36.304 9940-9940/com.example.afcosta.inesctec.pt.android E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.afcosta.inesctec.pt.android, PID: 9940
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.SecurityException: Permission Denial: starting Intent act=android.media.action.IMAGE_CAPTURE cmp=com.android.camera/.Camera from ProcessRecord631d205 9940:com.example.afcosta.inesctec.pt.android/u0a74 (pid=9940, uid=10074) with revoked permission android.permission.CAMERA
at android.os.Parcel.readException(Parcel.java:1684)
at android.os.Parcel.readException(Parcel.java:1637)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3101)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1518)
at android.app.Activity.startActivityForResult(Activity.java:4225)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
at android.app.Activity.startActivityForResult(Activity.java:4183)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
at com.example.afcosta.inesctec.pt.android.MainMenu.capturar(MainMenu.java:55)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
【问题讨论】:
这些方法放在哪里?都在一个片段中?都在一个 Activity 中? 你的onActivityResult中的resultCode是什么? 在活动中,resultCode为1 这是RESULT_CANCELED
的值,如docs。所以,你的 CameraActivity 没有返回正确或者你正在退出相机
【参考方案1】:
您的清单中缺少访问相机硬件的权限。添加:
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.hardware.camera.autofocus" />
【讨论】:
不工作,我想我需要检查我的运行时权限查看 ***.com/questions/32789027/… 确定你的目标是android M+。你没有指定哪个版本,顺便说一句,你总是需要把它们放在清单中,即使你在运行时检查它们以上是关于相机不进入 onActivityResult的主要内容,如果未能解决你的问题,请参考以下文章