不将图片上传到Firebase存储空间

时间:2017-07-09 17:03:03

标签: android firebase firebase-storage

我想将图片上传到存储空间。每次我启动我的模拟器时都没有错误,一切都运行正常,只有当我在模拟器中选择图片并上传它们时,即使刷新后它也不会显示在Firebase存储部分中。这是我的代码:

    package com.safariagaming;

import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

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 com.safariagaming.mtc_project.R;

public class Account_Page extends AppCompatActivity {

    private Button mSelectImage;

    private StorageReference mStorage;

    private static final int GALLERY_INTENT = 2;

    private ProgressDialog mProgressDialog;

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

        mSelectImage = (Button) findViewById(R.id.selectImage_BUTTON);

        mStorage = FirebaseStorage.getInstance().getReference(); //.child at end ifyou want to add an child to it

        mProgressDialog = new ProgressDialog(this);


        mSelectImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(Intent.ACTION_PICK);

                intent.setType("image/*");

                startActivityForResult(intent, GALLERY_INTENT);

            }
        });

    }


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

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

            mProgressDialog.setMessage("Uploading...");

            Uri uri = data.getData();

            StorageReference filepath = mStorage.child("Photos").child(uri.getLastPathSegment());

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


                    Toast.makeText(Account_Page.this, "Upload Done", Toast.LENGTH_LONG).show();

                mProgressDialog.dismiss();
                }//you can also add onFaliure listener
            });


        }

    }
}

3 个答案:

答案 0 :(得分:0)

将文件作为流而不是Uri来存储
删除Uri部分并改为:

InputStream inputStream = this.getContentResolver().openInputStream(data.getData());

并使用putFile(uri)更改putFile(inputStream)

并且还考虑使用合理的文件名更改uri.getLastPathSegment()部分,例如your_name.png(请注意,即使您的照片不是png格式,也会根据我的经验生效) 这应该工作

答案 1 :(得分:0)

这是我将图片上传到存储空间的方式

FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReferenceFromUrl(FILE_STORAGE_PATH);
InputStream stream = null;
try {
    stream = new FileInputStream(new File(uri.getLastPathSegment()));
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
StorageReference photoRef = storageRef.child("Photos/" + image_name + ".jpg");
        UploadTask uploadTask = photoRef.putStream(stream);
        uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                Log.d(TAG, "Success TO UPLOAD IMAGE");


            }
        }).addOnFailureListener(WorkCompletionActivity.this, new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                //Failure
            }
        });

答案 2 :(得分:0)

该代码对我来说非常合适。 只需更改Firebase控制台中的规则

即可
allow read, write: if true;

一切都会完美无缺