Firebase图片上传 - Android

时间:2017-06-13 04:07:08

标签: android firebase firebase-storage

我正在尝试将Image上传到firebase存储,但我既没有看到进度对话框也没有看到toast消息。

调试断点甚至没有在这些行停止。

请帮忙。

  ib_profileImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent_modifyImage = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivity(intent_modifyImage);
        }
    });
}

//Image Capture Activity Result
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK){
        progressDialog.setMessage("Uploading Image...");
        progressDialog.show();
        Uri uri = data.getData();
        StorageReference filepath = storageReference_image.child("profile_photos").child(uri.getLastPathSegment());
        filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                Toast.makeText(UserProfileActivity.this,"Upload Complete",Toast.LENGTH_SHORT).show();
                progressDialog.dismiss();
            }
        });
    }
}

2 个答案:

答案 0 :(得分:0)

试试这个

public class MainActivity extends AppCompatActivity {
Button chooseImg, uploadImg;
ImageView imgView;
int PICK_IMAGE_REQUEST = 111;
Uri filePath;
ProgressDialog pd;

//creating reference to firebase storage
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReferenceFromUrl("gs://fir-example-c4312.appspot.com");    //change the url according to your firebase app


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

    chooseImg = (Button)findViewById(R.id.chooseImg);
    uploadImg = (Button)findViewById(R.id.uploadImg);
    imgView = (ImageView)findViewById(R.id.imgView);

    pd = new ProgressDialog(this);
    pd.setMessage("Uploading....");


    chooseImg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_PICK);
            startActivityForResult(Intent.createChooser(intent, "Select Image"), PICK_IMAGE_REQUEST);
        }
    });

    uploadImg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(filePath != null) {
                pd.show();

                StorageReference childRef = storageRef.child("image.jpg");

                //uploading the image
                UploadTask uploadTask = childRef.putFile(filePath);

                uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        pd.dismiss();
                        Toast.makeText(MainActivity.this, "Upload successful", Toast.LENGTH_SHORT).show();
                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        pd.dismiss();
                        Toast.makeText(MainActivity.this, "Upload Failed -> " + e, Toast.LENGTH_SHORT).show();
                    }
                });
            }
            else {
                Toast.makeText(MainActivity.this, "Select an image", Toast.LENGTH_SHORT).show();
            }
        }
    });
}

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

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
        filePath = data.getData();

        try {
            //getting image from gallery
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);

            //Setting image to ImageView
            imgView.setImageBitmap(bitmap);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

}

不要忘记在firebase控制台中更改规则

默认情况下,我们不允许在没有身份验证的情况下访问firebase存储。要更改它,请使用任何Web浏览器访问firebase控制台。打开上面步骤中使用的firebase项目,然后转到“存储”,再转到“规则”选项卡。现在将读写规则更改为true,如下图所示。

enter image description here

答案 1 :(得分:0)

尝试这种方法,它适用于我

在onActivityResult内部,

' '.join(tokens)
#'I dropped the saw and it fell on the roof and damaged roof '+\
#' and some of the rooftiles'