【发布时间】:2018-06-16 12:08:42
【问题描述】:
我正在开发一个使用 Firebase 身份验证通过电话号码登录用户的应用。登录后,相机会打开,用户可以捕获存储到 Firebase 的照片。但是,当我拍摄照片并点击确定时,应用程序就会关闭。互联网上有很多类似的问题,我已经尝试解决每一个问题,但没有解决我的问题。请在此处提出修复建议。
这是Activity.java的代码:
private Button tapCameraBtn;
private static final int CAMERA_REQUEST_CODE = 1;
private StorageReference storageReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_contact);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
//referencing the storage directory
tapCameraBtn = (Button) findViewById(R.id.tapCameraBtn);
tapCameraBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST_CODE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK && data!= null){
Uri uri = data.getData();
storageReference = FirebaseStorage.getInstance().getReference();
//creating another storage reference for filepath
StorageReference filepath = storageReference.child("Photos").child(uri.getLastPathSegment());
//uploading image captuured to firebase
filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(AddContactActivity.this, "Uploading finished!", Toast.LENGTH_LONG).show();
}
});
}
}
}
清单文件在这里:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CAMERA"/>
<application
android:allowBackup="true"
android:icon="@drawable/app_icon"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".SplashActivity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".AuthActivity"
android:theme="@style/Theme.AppCompat.NoActionBar" />
<activity android:name=".APIActivity" android:parentActivityName=".GestureActivity"/>
<activity android:name=".GestureActivity" android:theme="@style/Theme.AppCompat.NoActionBar" />
<activity android:name=".AddContactActivity" android:theme="@style/Theme.AppCompat.NoActionBar"></activity>
</application>
另外,我已允许在我的设备中使用相机权限。
这里有一点更新,这个应用程序现在正在运行但在 API 21 的设备上,但是当我尝试在其他三个 API 高于 21 的设备上运行相同的应用程序时,它给了我下面提到的错误:
致命异常:主要 进程:com.example.pooja.auxilio,PID:17109 java.lang.RuntimeException:将结果 ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} 传递给活动 {com.example.pooja.auxilio/ 失败com.example.pooja.auxilio.AddContactActivity}:java.lang.IllegalArgumentException:uri 不能为空 在 android.app.ActivityThread.deliverResults(ActivityThread.java:4173) 在 android.app.ActivityThread.handleSendResult(ActivityThread.java:4216) 在 android.app.ActivityThread.-wrap20(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1590) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:163) 在 android.app.ActivityThread.main(ActivityThread.java:6342) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770) 引起:java.lang.IllegalArgumentException:uri 不能为空 在 com.google.android.gms.common.internal.Preconditions.checkArgument(未知来源) 在 com.google.firebase.storage.StorageReference.putFile(未知来源) 在 com.example.pooja.auxilio.AddContactActivity.onActivityResult(AddContactActivity.java:104) 在 android.app.Activity.dispatchActivityResult(Activity.java:7111) 在 android.app.ActivityThread.deliverResults(ActivityThread.java:4169) 在 android.app.ActivityThread.handleSendResult(ActivityThread.java:4216) 在 android.app.ActivityThread.-wrap20(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1590) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:163) 在 android.app.ActivityThread.main(ActivityThread.java:6342) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
【问题讨论】:
-
您能否发布您遇到的错误的堆栈跟踪?
-
@DiegoMalone 这显示了:java.lang.RuntimeException: 传递结果失败 ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (有额外的) }} 到活动 {com.example.pooja.auxilio/com.example.pooja.auxilio.AddContactActivity}: java.lang.NullPointerException: 尝试调用虚拟方法'java.lang.String android.net.Uri.getLastPathSegment( )' 在空对象引用上
-
这些其他问题链接是什么,您可以发布它们,因为您的代码看起来不错
-
你能调试和分享来自URI的图片路径
标签: android firebase firebase-storage