您好,我将图片上传到Firebase时出错了。当我设置配置文件时,我在活动中运行相同的代码,并且运行完美,但是当我在活动中进行此操作时您可以更改个人资料图片时出现错误。
错误消息
Exception java.lang.RuntimeException: Failure delivering result
ResultInfo{who=null, request=2, result=-1, data=Intent { dat=file:///storage/emulated/0/DCIM/Facebook/FB_IMG_1476211508746.jpg typ=image/jpeg }} to activity {com.samo.facedatefb/com.samo.facedatefb.Settings}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
android.app.ActivityThread.deliverResults (ActivityThread.java:3741)
android.app.ActivityThread.handleSendResult (ActivityThread.java:3784)
android.app.ActivityThread.access$1300 (ActivityThread.java:169)
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1472)
android.os.Handler.dispatchMessage (Handler.java:11
android.os.Looper.loop (Looper.java:194)
android.app.ActivityThread.main (ActivityThread.java:5546)
java.lang.reflect.Method.invoke (Method.java)
java.lang.reflect.Method.invoke (Method.java:372)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:967)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:762)
arrow_drop_down
Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
com.samo.facedatefb.Settings.onActivityResult (Settings.java:232)
android.app.Activity.dispatchActivityResult (Activity.java:6177)
android.app.ActivityThread.deliverResults (ActivityThread.java:3737)
android.app.ActivityThread.handleSendResult (ActivityThread.java:3784)
android.app.ActivityThread.access$1300 (ActivityThread.java:169)
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1472)
android.os.Handler.dispatchMessage (Handler.java:111)
android.os.Looper.loop (Looper.java:194)
android.app.ActivityThread.main (ActivityThread.java:5546)
java.lang.reflect.Method.invoke (Method.java)
java.lang.reflect.Method.invoke (Method.java:372)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:967)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:762)
这是代码
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_INTENT && resultCode == RESULT_OK) {
mProgressDialog.setMessage("Uploading...");
mProgressDialog.show();
StorageReference imageRef = mStorage.child(myEmail);
StorageReference imageImagesRef = mStorage.child("photos/" + myEmail);
imageRef.getName().equals(imageImagesRef.getName()); // true
imageRef.getPath().equals(imageImagesRef.getPath()); // false
Uri uri = data.getData();
imageView.setImageURI(uri);
imageView.setDrawingCacheEnabled(true);
imageView.buildDrawingCache();
Bitmap bitmap = imageView.getDrawingCache();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] data2 = baos.toByteArray();
UploadTask uploadTask = imageImagesRef.putBytes(data2);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle unsuccessful uploads
Toast.makeText(getApplicationContext(), "Sorry, your image wasn't set, please try again",
Toast.LENGTH_LONG).show();
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
mProgressDialog.dismiss();
Toast.makeText(getApplicationContext(), "Upload done",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), MapsActivity.class);
startActivity(intent);
}
});
}
}
和
} else if (arrayList.get(position).equals("Change profile photo")) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, GALLERY_INTENT);
}
答案 0 :(得分:1)
// This is null
Uri uri = data.getData();
因此,在压缩位图之前检查空指针并尝试查找从图库中提取图像的教程。如果您的设备的Android版本是 marshmallow 或更高版本,请检查运行时权限。