应用无法连接到Firebase存储 - 无法打开,无错误

时间:2016-12-30 06:03:24

标签: java android firebase firebase-storage

我正在编写一个教程来创建一个录音机,然后将录音上传到Firebase。我多次观看教程并确保应用程序已连接到Firebase;但是,由于此问题,应用程序现在无法打开。我是Java编程的新手。有人可以给我一些指导吗?谢谢

package bfb.ess.myapplicationbfb;

import android.app.ProgressDialog;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

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 java.io.File;
import java.io.IOException;

public class speakp extends AppCompatActivity {

    private Button mRecordBtn;
    private TextView mRecordlabel;

    private MediaRecorder mRecorder;

    private String mFileName = null;

    private static final String LOG_TAG = "Record_log";

    private StorageReference mStorage;


    private ProgressDialog mProgress;


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

        mStorage = FirebaseStorage.getInstance().getReference();

        mRecordlabel = (TextView) findViewById(R.id.recordlabel);
        mRecordBtn =(Button) findViewById(R.id.recordBtn);

        mProgress = new ProgressDialog(this);

        mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        mFileName +="/recorded_audio.3gp";

        mRecordBtn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {

                if(motionEvent.getAction() == MotionEvent.ACTION_DOWN) {

                    startRecording();

                    mRecordlabel.setText("Recording Started ...");

                } else if (motionEvent.getAction() == MotionEvent.ACTION_UP) {

                    stopRecording();

                    mRecordlabel.setText("Recording Stopped ...");
                }


                return false;
            }
        });
    }

    private void startRecording() {
        mRecorder = new MediaRecorder();
        mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mRecorder.setOutputFile(mFileName);
        mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

        try {
            mRecorder.prepare();
        } catch (IOException e) {
            Log.e(LOG_TAG, "prepare() failed");
        }

        mRecorder.start();
    }

    private void stopRecording() {
        mRecorder.stop();
        mRecorder.release();
        mRecorder = null;

        uploadAudio();
    }

    private void uploadAudio() {



        mProgress.setMessage("Uploading Audio ...");
        mProgress.show();

        StorageReference filepath = mStorage.child("Audio").child("new_audio.3gp");

        Uri uri = Uri.fromFile(new File(mFileName));

        filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                mProgress.dismiss();

                mRecordlabel.setText("Uploading Finished");

            }
        });

    }
}

2 个答案:

答案 0 :(得分:0)

转到Firebase控制台并更新存储的读写规则

您可以查看Google Firebase示例:Storage Sample

如果正确,请检查音频文件的URI。因为我猜uri的lastPathSegment出错了。

默认:

service firebase.storage {
  match /b/<name_of_app>/o {
    match /{allPaths=**} {
      allow read, write: if request.auth != null;
    }
  }
}

将其更新为:

service firebase.storage {
      match /b/<name_of_app>/o {
        match /{allPaths=**} {
          allow read;
          allow write;
        }
      }
    }

注意: 即使您未获得授权,此更新也允许您存储数据。 它仅用于开发目的,因为它可能导致访问数据的安全问题而无需获得授权。 因此,在将应用程序上传到Play商店时要小心,更新此参数并根据您的要求进行更改。

答案 1 :(得分:0)

您需要在firebase控制台中更新存储的读写规则,

转到Firebase控制台,选择存储 单击Rules选项卡

默认:

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if request.auth != null;
    }
  }
}

更改为:

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write;
    }
  }
}
正如Mohom.R所说

注意:即使您未获得授权,此更新也允许您存储数据。它仅用于开发目的,因为它可能导致访问数据的安全问题而无需获得授权。因此,在将应用程序上传到Play商店时要小心,更新此参数并根据您的要求进行更改。