我是android开发的新手。这是一个测试应用程序,我试图通过它为我的活动调用图像。一切似乎都很好,除非我在 onACtivityResult(); 中返回相机图像;应用程序崩溃。我已经包含了日志,以便我可以指出错误实际发生的位置。
这是 Mainfest 文件:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera2"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.android.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter android:icon="@mipmap/ic_launcher_round">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="http"
android:host="www.example.com"
android:pathPrefix="/nilabhra" />
</intent-filter>
</activity>
<activity
android:name=".DisplayMessageActivity"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
<intent-filter android:icon="@mipmap/ic_launcher_round">
<action android:name="android.intent.action.SEND"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>
</intent-filter>
<intent-filter>
<action android:name="android.media.action.IMAGE_CAPTURE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
这是捕获功能:
public void capturePhoto(){
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (captureIntent.resolveActivity(getPackageManager())!= null){
startActivityForResult(captureIntent, REQUEST_IMAGE_CAPTURE);
Log.i(TAG, "capturePhoto: funtion ran succesfully");
}
}
这是 OnActivityResult:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE) {
if (resultCode == RESULT_OK) {
Log.i(TAG, "onActivityResult: success");
}
} else {
if (resultCode == RESULT_CANCELED) {
Log.e(TAG, "onActivityResult: error and RESULT_CANCEllED TRUE");
}
}
}
这是我拍摄相机应用程序并返回 Intent 后的日志:
03-07 12:44:49.963 27953-27953/myapp.test.nilabhra.com.freshstart E/AndroidRuntime: FATAL EXCEPTION: main
Process: myapp.test.nilabhra.com.freshstart, PID: 27953
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {myapp.test.nilabhra.com.freshstart/myapp.test.nilabhra.com.freshstart.DisplayMessageActivity}: java.lang.NullPointerException: uri
at android.app.ActivityThread.deliverResults(ActivityThread.java:3717)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3760)
at android.app.ActivityThread.access$1400(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1396)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5451)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: uri
at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:60)
at android.content.ContentResolver.query(ContentResolver.java:476)
at android.content.ContentResolver.query(ContentResolver.java:435)
at myapp.test.nilabhra.com.freshstart.DisplayMessageActivity.onActivityResult(DisplayMessageActivity.java:164)
at android.app.Activity.dispatchActivityResult(Activity.java:6528)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3713)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3760)
at android.app.ActivityThread.access$1400(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1396)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5451)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)