应用程序在单击带有错误的图像时崩溃“java.lang.RuntimeException:失败传递结果ResultInfo {who = null,request = 0,result = -1,data = Intent {act = inline-data(has extras) )}} to activity {com.example.android.imageuploader / com.example.android.imageuploader.MainActivity}:java.lang.NullPointerException“
主要活动: -
package com.example.android.imageuploader;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import static android.R.attr.cacheColorHint;
import static android.R.attr.data;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CODE = 0;
private StorageReference storageReference;
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
storageReference = FirebaseStorage.getInstance().getReference();
progressDialog = new ProgressDialog(this);
Button clickButton = (Button) findViewById(R.id.button_click);
clickButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, REQUEST_CODE); //starting camera intent
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
progressDialog.setMessage("Upload In Progress...");
progressDialog.show();
Uri uri = data.getData();
StorageReference filePath = storageReference.child("Pictures").child(uri.getLastPathSegment());
filePath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "Upload Successful!!!", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(), "Upload Unsuccessful!!!", Toast.LENGTH_SHORT).show();
}
});
}
}
}
logcat的
FATAL EXCEPTION: main
Process: com.example.android.imageuploader, PID: 2801
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.android.imageuploader/com.example.android.imageuploader.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:3363)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3406)
at android.app.ActivityThread.access$1300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5019)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.android.imageuploader.MainActivity.onActivityResult(MainActivity.java:56)
at android.app.Activity.dispatchActivityResult(Activity.java:5423)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3359)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3406)
at android.app.ActivityThread.access$1300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5019)
at java.lang.reflect.Method.invokeNative(Native Method)
答案 0 :(得分:0)
读取logcat。 onActivityResult方法中的NullPointException。 com.example.android.imageuploader.MainActivity。 onActivityResult(MainActivity.java:56) at
答案 1 :(得分:0)
如果有人仍然在想,我有这个问题,在我的情况下,这是因为意图不再返回图像的Uri而是返回图像位图作为额外的,所以您可以尝试检索位图从“ OnActivityResults” intent(“ data”)中额外创建一个文件,然后从该位图创建一个文件并使用它上传到firebase,为什么这个意图确实如此,所以我不确定我是否正在使用文件提供程序,然后将其删除了,所以我认为在这个阶段,正是这种东西改变了意图的行为。