无法将正确的照片URL上传到Firebase

时间:2018-10-15 14:29:39

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

当我尝试上传图像并单击提交按钮时,logcat会显示此消息。

Logcat结果

2018-10-15 22:21:32.191 22289-22477/com.example.edward.neweventmanagementsystem E/StorageUtil: error getting token java.util.concurrent.ExecutionException: com.google.firebase.internal.api.FirebaseNoSignedInUserException: Please sign in before trying to get a token.

Java文件

package com.example.edward.neweventmanagementsystem;

import android.Manifest;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.edward.neweventmanagementsystem.Model.EventInfo;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.OnProgressListener;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;

import java.util.Calendar;
import java.util.regex.Pattern;

import static android.widget.Toast.LENGTH_SHORT;

public class CreateEvent extends AppCompatActivity {

    private static final String TAG = "activity_create_event";
    private Uri filePath;
    private TextView mDisplayDate;
    private DatePickerDialog.OnDateSetListener mDateSetListener;
    private DatabaseReference mDatabaseReference;
    private Button mRegisterButton;
    EditText mEventNameText, mContactNumText, mEventLocationText, mRegisterEventId;;
    TextView mEventDate;
    RadioGroup mEventType;
    FirebaseStorage storage;
    StorageReference storageRef,imageRef;
    Uri uriImage ;//= Uri.parse("com.example.edward.eventmanagementsystem.ManageEvent/"+ R.drawable.ic_launcher_background);
    public static final int PICK_IMAGE = 1;
    ImageView mimageToUpload;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_event);

        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();

        FirebaseDatabase firebaseDatabase;
        mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("ListOfEvent"); //.push();

        mRegisterButton = (Button)findViewById(R.id.btnRegisterEvent);
        storage = FirebaseStorage.getInstance();
        storageRef = storage.getReference();

        mDisplayDate = (TextView) findViewById(R.id.RegisterEventStartDate);
        mDisplayDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar cal = Calendar.getInstance();
                int year = cal.get(Calendar.YEAR);
                int month = cal.get(Calendar.MONTH);
                int day = cal.get(Calendar.DAY_OF_MONTH);

                DatePickerDialog dialog = new DatePickerDialog(
                        CreateEvent.this,
                        android.R.style.Theme_Holo_Light_Dialog_MinWidth,
                        mDateSetListener,
                        year, month, day);
                dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                dialog.show();
            }
        });

        mDateSetListener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int day) {
                month = month + 1;
                Log.d(TAG, "onDateSet: date: mm/dd/yyyy: " + month + "/" + day + "/" + year);

                String date = month +  "/"  + day + "/" + year;
                mDisplayDate.setText(date);
            }
        };

        //insert data to database
        mRegisterEventId = (EditText) findViewById(R.id.RegisterEventId);
        mEventNameText = (EditText) findViewById(R.id.RegisterEventName);
        mContactNumText = (EditText) findViewById(R.id.RegisterContactNumber);
        mEventDate = (TextView) findViewById(R.id.RegisterEventStartDate);
        mEventType =  (RadioGroup) findViewById(R.id.RegisterEventRadiogroup);
        mEventLocationText = (EditText) findViewById(R.id.RegisterEventLocation);
        mimageToUpload = (ImageView) findViewById(R.id.imageToUpload);
        mRegisterButton = (Button) findViewById(R.id.btnRegisterEvent);

        mimageToUpload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (ContextCompat.checkSelfPermission(CreateEvent.this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED){
                    selectPdf();
                }
                else {
                    ActivityCompat.requestPermissions(CreateEvent.this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},9);
                }
            }
        });

        mRegisterButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final ProgressDialog mDialog = new ProgressDialog(CreateEvent.this);
                mDialog.setMessage("Please waiting...");
                mDialog.show();

                int selectedId = mEventType.getCheckedRadioButtonId();
                final RadioButton radioButton = (RadioButton)findViewById(selectedId);

                final String id = mRegisterEventId.getText().toString().trim();
                final String name = mEventNameText.getText().toString().trim();
                final String contact = mContactNumText.getText().toString().trim();
                final String date = mEventDate.getText().toString().trim();
                final String type = radioButton.getText().toString().trim();
                final String location = mEventLocationText.getText().toString().trim();

                if (TextUtils.isEmpty(id)) {
                    mRegisterEventId.setError("Enter Event ID!");
                    return;
                }

                if (TextUtils.isEmpty(name)) {
                    mEventNameText.setError("Enter Event Name!");
                    return;
                }


                if (TextUtils.isEmpty(location)) {
                    mEventLocationText.setError("Enter Location!");
                    return;
                }


                if (TextUtils.isEmpty(type)) {
                    radioButton.setError("Please select type of event type!");
                    return;
                }
                if(isValidPhone(contact)){

                    Toast.makeText(getApplicationContext(),"Phone number is valid",Toast.LENGTH_SHORT).show();
                }else {
                    mContactNumText.setError("Phone number is not valid");
                    Toast.makeText(getApplicationContext(),"Phone number is not valid",Toast.LENGTH_SHORT).show();
                    return;
                }


                if (TextUtils.isEmpty(date)) {
                    mEventDate.setError("Please select event date!");
                    return;
                }

                mDatabaseReference.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        if(dataSnapshot.child(mRegisterEventId.getText().toString()).exists()) {
                            mDialog.dismiss();
                            Toast.makeText(CreateEvent.this, "ID already exists!", Toast.LENGTH_SHORT).show();
                        }else {
                            mDialog.dismiss();
                            EventInfo eventInfo = new EventInfo(mRegisterEventId.getText().toString().trim(),mEventNameText.getText().toString().trim(), mContactNumText.getText().toString().trim(), mEventDate.getText().toString().trim(), radioButton.getText().toString().trim(), mEventLocationText.getText().toString().trim());
                            mDatabaseReference.child(mRegisterEventId.getText().toString()).setValue(eventInfo);
                        }
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });

                final String fileName = System.currentTimeMillis()+"";


                if(uriImage != null) {


                    final StorageReference storageReference = storage.getReference();

                    System.out.println(uriImage);
                    storageReference.child("profileImageUrl").child(fileName).putFile(uriImage)
                            .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                                @Override
                                public void onSuccess(final UploadTask.TaskSnapshot taskSnapshot) {

                                    String url = taskSnapshot.getMetadata().getReference().getDownloadUrl().toString();
//                                    Uri downloadUrl = taskSnapshot.getMetadata().getReference().getDownloadUrl().getResult();
//                                    String urlImage = downloadUrl.toString();


                                    mDatabaseReference.child(id).child("profileImageUrl").setValue(url).addOnCompleteListener(new OnCompleteListener<Void>() {
                                        @Override
                                        public void onComplete(@NonNull Task<Void> task) {


                                            if (task.isSuccessful()){
                                                System.out.println(taskSnapshot.getUploadSessionUri().toString());
                                            }
                                            else{

                                                Toast.makeText(getApplicationContext(),"File not Successfully Uploaded",LENGTH_SHORT).show(); }
                                        }
                                    });
                                }
                            }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Toast.makeText(getApplicationContext(),"File not Successfully Uploaded",LENGTH_SHORT).show();

                        }
                    }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {

                        }
                    });
                }else{

                }
                Toast.makeText(getApplicationContext(),"New event created successfully!",LENGTH_SHORT).show();
                Intent ManageEventMenu =  new Intent(CreateEvent.this, com.example.edward.neweventmanagementsystem.ManageEventMenu.class);
                startActivity(ManageEventMenu);
            }
        });
    }


    public boolean isValidPhone(CharSequence phone) {
        boolean check=false;
        if(!Pattern.matches("[a-zA-Z]+", phone))
        {
            if(phone.length() < 10 || phone.length() > 11)
            {
                check = false;
            }
            else
            {
                check = true;
            }
        }
        else
        {
            check=false;
        }
        return check;
    }

    private void selectPdf() {
        Intent photoPickerIntent  = new Intent();
        photoPickerIntent .setType("image/*");
        photoPickerIntent .setAction(Intent.ACTION_OPEN_DOCUMENT);
        startActivityForResult(photoPickerIntent ,86);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == 86 && resultCode == RESULT_OK && data != null){

            final Uri imageUri = data.getData();
            uriImage = imageUri;
            mimageToUpload.setImageURI(uriImage);
        }
        else {
            Toast.makeText(getApplicationContext(),"Please select file", LENGTH_SHORT).show();
        }
    }
}

任何人都知道如何解决此问题。非常感谢。 另外,当尝试在我的应用程序中显示消息时,我也遇到问题。其他字段没有问题,例如“文本”,“单选按钮”,“日期”,只有照片,但我无法上传真实网址。

菲。目前,firebase数据库中的url显示是照片存储位置

2 个答案:

答案 0 :(得分:1)

似乎用户未签名(已从Firebase认证)。 他们是解决问题的两种方法 1)使用身份验证对用户进行身份验证 2)将您的规则设置为基于火的实时数据库

将此规则复制并粘贴到Firebase实时数据库>规则

{
"rules": {
".read": true,
".write": true
}
}

但是这样做将使 您的安全规则被定义为公共规则,因此任何人都可以窃取,修改或删除数据库中的数据

答案 1 :(得分:0)

该异常清楚地表明您试图在没有用户登录的情况下执行操作。

您可以按照此手册进行身份验证,然后执行操作

https://firebase.google.com/docs/auth/android/password-auth