将图像上传到Firebase时出现NullPointerException

时间:2016-07-30 12:57:14

标签: android firebase-realtime-database firebase-storage

我正在使用firebase技术开发一个基本的Android聊天应用程序。我有一个关于将图像上传到firebase数据库的问题。我点击ImageButton从我的图库中选择一个图像。当我选择图像时,我的图像无法上传到firebase数据库。相反,我的应用程序崩溃了

private static final int RC_PHOTO_PICKER = 1;       
 ImageButton imageBtn;                               
 private FirebaseStorage storage;                    
 private DatabaseReference databaseRef;              
 private StorageReference storageRef;                
 private FirebaseApp app;                            
 private FirebaseDatabase database; 

 @Override                                             
 protected void onCreate(Bundle savedInstanceState)    
 {             


  // image upload  code                                                                                                          

  app = FirebaseApp.getInstance();                                                                                           
  database = FirebaseDatabase.getInstance(app);                                                                              
  storage = FirebaseStorage.getInstance(app);                                                                                
  storageRef=storage.getReferenceFromUrl("gs://friendlychat-8a091.appspot.com/");                                            
  storageRef=storage.getReference("images");                                                                                 

  imageBtn = (ImageButton)findViewById(R.id.imageBtn);                                                                       
  imageBtn.setOnClickListener(new View.OnClickListener()                                                                     
  {                                                                                                                          
      public void onClick(View v)                                                                                            
      {                                                                                                                      
          Intent intent = new Intent(Intent.ACTION_GET_CONTENT);                                                             
          intent.setType("image/jpeg");                                                                                      
          intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);                                                                    
          startActivityForResult(Intent.createChooser(intent, getString(R.string.complete_action_with)), RC_PHOTO_PICKER);   
      }                                                                                                                      
  });                                                                                                                        


 }                            

  @Override                                                                        
  protected void onActivityResult(int requestCode, int resultCode, Intent data)    
  {
          if(requestCode==RC_PHOTO_PICKER && resultCode==RESULT_OK)                                                               
        {                                                                                                                       
            Uri selectedImageUri = data.getData();                                                                              
            final   StorageReference PhotoRef=storageRef.child("images/"+selectedImageUri.getLastPathSegment());                

            PhotoRef.putFile(selectedImageUri)                                                                                  
              .addOnFailureListener(new OnFailureListener() {                                                             
                  @Override                                                                                               
                  public void onFailure(@NonNull Exception exception)                                                     
                  {                                                                                                       
                     Toast.makeText(getApplicationContext(),"error ="+exception.getMessage(),Toast.LENGTH_LONG).show();   
                  }                                                                                                       
              })                                                                                                          
              .addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {                              
                  @Override                                                                                               
                  public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {                                           
                      Uri downloaduri = taskSnapshot.getDownloadUrl();                                                    
                      mMessageEditText.setText(downloaduri.toString());                                                   

                  }                                                                                                       
              });                                                                                                         

        }                                                                                                                                                                                               
  }

当我从图库中选择图像时,这是logcat消息

    07-30 18:50:00.946 5268-5268/com.google.firebase.codelab.friendlychat D/MainActivity: onActivityResult: requestCode=1, resultCode=-1
07-30 18:50:00.962 5268-5268/com.google.firebase.codelab.friendlychat D/AndroidRuntime: Shutting down VM
07-30 18:50:00.962 5268-5268/com.google.firebase.codelab.friendlychat W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x411342a0)
07-30 18:50:01.188 5268-5268/com.google.firebase.codelab.friendlychat D/FirebaseCrashApiImpl: throwable java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/757 (has extras) }} to activity {com.google.firebase.codelab.friendlychat/com.google.firebase.codelab.friendlychat.MainActivity}: java.lang.NullPointerException
07-30 18:50:01.290 5268-5268/com.google.firebase.codelab.friendlychat E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                        java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/757 (has extras) }} to activity {com.google.firebase.codelab.friendlychat/com.google.firebase.codelab.friendlychat.MainActivity}: java.lang.NullPointerException
                                                                                            at android.app.ActivityThread.deliverResults(ActivityThread.java:3179)
                                                                                            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3222)
                                                                                            at android.app.ActivityThread.access$1100(ActivityThread.java:140)
                                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1276)
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                            at android.os.Looper.loop(Looper.java:137)
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:4895)
                                                                                            at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                            at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
                                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
                                                                                            at dalvik.system.NativeStart.main(Native Method)
                                                                                         Caused by: java.lang.NullPointerException
                                                                                            at com.google.firebase.codelab.friendlychat.MainActivity.onActivityResult(MainActivity.java:329)
                                                                                            at android.app.Activity.dispatchActivityResult(Activity.java:5347)
                                                                                            at android.app.ActivityThread.deliverResults(ActivityThread.java:3175)
                                                                                            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3222) 
                                                                                            at android.app.ActivityThread.access$1100(ActivityThread.java:140) 
                                                                                            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1276) 
                                                                                            at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                                            at android.os.Looper.loop(Looper.java:137) 
                                                                                            at android.app.ActivityThread.main(ActivityThread.java:4895) 
                                                                                            at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                            at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994) 
                                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761) 
                                                                                            at dalvik.system.NativeStart.main(Native Method) 

07-30 18:53:01.196 5268-5270/com.google.firebase.codelab.friendlychat D/dalvikvm: GC_CONCURRENT freed 454K, 10% free 10859K/12039K, paused 19ms+7ms, total 76ms
07-30 18:53:01.259 5268-5276/com.google.firebase.codelab.friendlychat D/AbsListView: [unregisterDoubleTapMotionListener]
07-30 18:53:01.259 5268-5276/com.google.firebase.codelab.friendlychat I/MotionRecognitionManager:   .unregisterListener : / listener count = 0->0, listener=android.widget.AbsListView$4@41b77058

1 个答案:

答案 0 :(得分:0)

@Override
public void onClick(View view)
{
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, REQUEST_IMAGE);
}