FireBase存储映像上传Android [英] FireBase Storage Image Upload Android

查看:53
本文介绍了FireBase存储映像上传Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个与Firebase Storage一起运行的应用程序.想法是从图片库"中选择一个图像,然后将其上传到Firebase存储.与Firebase的连接似乎工作正常,我可以选择一个图像.当我按下提交"按钮将其上传到Firebase时,就会出现问题.

I have created an app running with Firebase Storage. The idea is that you select an Image from your Photo Gallery and then Upload it to Firebase Storage. The connection with Firebase seems to work fine, I can select an Image. The problem arises when I push the Submit Button to upload it to Firebase.

当我单击一次时,什么也没有发生.

When I click it one time, nothing happens.

单击几次后,我收到消息发生未知错误,请检查HTTP结果代码和内部异常".

When I click it a few times, I get the message "an unknown error occurred, please check the HTTP result code and inner exception"..

我该怎么办,寻求建议.

What should I do, looking for advice..

来自清单:

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

来自活动:

public class PostActivity extends AppCompatActivity {

private static final int GALLERY_REQUEST = 2;
private Uri uri = null;
private ImageButton imageButton;
private EditText editName;
private EditText editDescription;
private StorageReference storageReference;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_post);
    //reference the two edittext Views, initialize them
    editName = (EditText) findViewById(R.id.editName);
    editDescription = (EditText) findViewById(R.id.editDescription);

    //add the reference to the storagereference, initialize it
    storageReference = FirebaseStorage.getInstance().getReference();
}

public void ImageButtonClicked (View view){
    Intent galleryintent = new Intent (Intent.ACTION_GET_CONTENT);
    galleryintent.setType("Image/*");  
    startActivityForResult(galleryintent, GALLERY_REQUEST);
}

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

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

uri = data.getData();
imageButton = (ImageButton) findViewById(R.id.imageButton);
imageButton.setImageURI(uri);
    }
}

public void submitButtonClicked (View view){

    String titleValue = editName.getText().toString().trim();
    String titleDescription = editDescription.getText().toString().trim();

    if (!TextUtils.isEmpty(titleValue) && !TextUtils.isEmpty(titleDescription)){

        StorageReference filePath = storageReference.child("PostImage").child(uri.getLastPathSegment());

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

Uri downloadurl = taskSnapshot.getDownloadUrl();
Toast.makeText(PostActivity.this,"uploadcomplete",Toast.LENGTH_LONG).show();

            }
        });
filePath.putFile(uri).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {

       Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();

推荐答案

尝试使用此方法将图像上传到Firebase存储:

Try this method for image upload to firebase storage:

private void uploadMethod() {
        progressDialog();
        FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();
        StorageReference storageReferenceProfilePic = firebaseStorage.getReference();
        StorageReference imageRef = storageReferenceProfilePic.child("Your Path" + "/" + "Image Name" + ".jpg");

        imageRef.putFile(imageUri)
                .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        //if the upload is successful
                        //hiding the progress dialog
                        //and displaying a success toast
                        dismissDialog();
                        String profilePicUrl = taskSnapshot.getDownloadUrl().toString();
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception exception) {
                        //if the upload is not successful
                        //hiding the progress dialog
                        dismissDialog();
                        //and displaying error message
                        Toast.makeText(getActivity(), exception.getCause().getLocalizedMessage(), Toast.LENGTH_LONG).show();
                    }
                })
                .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                        //calculating progress percentage
//                        double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
//                        //displaying percentage in progress dialog
//                        progressDialog.setMessage("Uploaded " + ((int) progress) + "%...");
                    }
                });

    }

这篇关于FireBase存储映像上传Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆