Firebase图片上传到存储Android错误

时间:2016-11-07 08:14:45

标签: java android firebase firebase-storage

有, 我一直在关注Firebase的YouTube视频制作一个简单的博客应用,用户可以上传一个带有一个标题和图片的图片。一些描述。因此,当我单击提交按钮时,我的进度条将保持循环无限时间。我没有得到任何解决方案。请帮我。

其中两个活动是PostActivity.java,另一个是MainActivity.Java

PostActivity.java: -

public class PostActivity extends AppCompatActivity {

    private ImageButton img_btn_add_photo;
    private EditText et_post_title,et_post_description;
    private Button btn_post_submit;

    private Uri mImageUri = null;

    private static final int GALLERY_REQUEST = 1;
    private ProgressDialog mProgressDialog;
    private StorageReference mStorage;

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

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

        mProgressDialog = new ProgressDialog(this);

        et_post_title = (EditText) findViewById(R.id.et_post_title);
        et_post_description = (EditText) findViewById(R.id.et_post_description);

        btn_post_submit = (Button) findViewById(R.id.btn_post_submit);

        img_btn_add_photo = (ImageButton) findViewById(R.id.img_btn_add_photo);
        img_btn_add_photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
                galleryIntent.setType("image/*");
                startActivityForResult(galleryIntent,GALLERY_REQUEST);
            }
        });


        btn_post_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submitToDB();
            }
        });

    }

    private void submitToDB() {


        mProgressDialog.setMessage("Submitting Post...");
        mProgressDialog.show();

        final String title = et_post_title.getText().toString().trim();
        final String descr = et_post_description.getText().toString().trim();

        if (!TextUtils.isEmpty(title) && !TextUtils.isEmpty(descr) && mImageUri != null){

            StorageReference filePath = mStorage.child("Blog_Images").child(mImageUri.getLastPathSegment());
            filePath.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                    Uri downloadUri = taskSnapshot.getDownloadUrl();

                    mProgressDialog.dismiss();

                }
            });

        }

    }

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


        if (requestCode == GALLERY_REQUEST && resultCode == RESULT_OK){

            mImageUri = data.getData();
            img_btn_add_photo.setImageURI(mImageUri);

        }

    }
}

Manifest.xml

<uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".PostActivity"></activity>
    </application>

</manifest>

MainAcitivity.java菜单中只有一个添加按钮。所以我没有在这里上传。请帮忙。

1 个答案:

答案 0 :(得分:0)

确保您已在firebase存储中启用了无需身份验证的文件

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