简体   繁体   English

如何直接从URL上传图像到FireBase?

[英]How To Upload Images Directly From An URL to FireBase?

I have an app setup in which i am authenticating a user through Google sign in. And if the user authenticates properly, The user will be allowed to enter to the next activity. 我有一个应用程序设置,其中我通过Google登录来验证用户身份。如果用户正确验证身份,则将允许该用户进入下一个活动。 As well as the Name, email and profile image of the user from the account will be saved to Firebase. 以及该帐户的用户的名称,电子邮件和个人资料图像也将保存到Firebase。

Now the Problem is that i am able to store the name and the email to the Firebase but i am unable to save the profile image directly from URL from the account to the Firebase. 现在的问题是,我能够将名称和电子邮件存储到Firebase,但是我无法直接将URL中的个人资料图像从帐户保存到Firebase。

 GoogleSignInAccount acct = GoogleSignIn.getLastSignedInAccount(getApplicationContext());
                            String F_name = acct.getDisplayName();
                            String L_name = acct.getFamilyName();
                            String email = acct.getEmail(); 
                            final Uri photo = acct.getPhotoUrl();

                            final StorageReference FILEPATH = FirebaseStorage.getInstance().getReference().child("Profile images/"+email +".jpg");


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


                                    Map<String, Object> profileToSave = new HashMap<>();

                                    profileToSave.put("Profile Pic",photo.toString());
                                    docRef.set(profileToSave).addOnSuccessListener(new OnSuccessListener<Void>() {
                                        @Override
                                        public void onSuccess(Void aVoid) {


                                            startActivity(new Intent(getApplicationContext(),profile.class));
                                            finish();


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

                                            Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
                                        }
                                    });
                                }
                            });

And the following lines are from the error i got 以下几行是我得到的错误

E/UploadTask: could not locate file for uploading:

E/StorageException: StorageException has occurred.

An unknown error occurred, please check the HTTP result code and inner exception for server response. Code: -13000 HttpResult: 0

E/StorageException: No content provider:

Looking forward to any suggestions as soon as possible. 希望尽快有任何建议。 Let me know if some one got the solution or an other way to do So. 让我知道是否有人找到了解决方案或另一种方法。 Thanks 谢谢

The short answer is that you can't.. but in reality, there isn't much reason to. 简短的答案是您不能..但是实际上,没有太多理由。 If you have the url, you can already use it, so there is no need to upload the file to Firebase. 如果您拥有该URL,则可以使用它,因此无需将文件上传到Firebase。 I suggest you simply store the URL in your Realtime Database. 我建议您仅将URL存储在实时数据库中。 Now, to sync this with email/password users (who don't automatically have a profile picture on account creation) and to allow users to update their profile picture: 现在,将其与电子邮件/密码用户(在创建帐户时不会自动拥有个人资料图片)同步,并允许用户更新其个人资料图片:

Let users set their profile picture (by uploading an image to Firebase Storage). 让用户设置他们的个人资料图片(通过将图像上传到Firebase Storage)。 Once the upload is complete, get the download URL (of the storage reference!) and update photoUrl in your Realtime Database. 上传完成后,获取(存储参考的)下载URL,并更新实时数据库中的photoUrl。

See my code below: 请参阅下面的代码:

storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
            @Override
            public void onSuccess(Uri uri) {
                Log.d(TAG, "before onSuccess: " + user.getPhotoUrl());
                UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
                        .setPhotoUri(uri).build();
                user.updateProfile(profileUpdates);
                Log.d(TAG, "after onSuccess: " + uri);
                //add new photoUrl to db
                databaseReference.child("photoUrl").setValue(uri.toString());
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.d(TAG, "onFailure:" + e);
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM