【问题标题】:Cannot select image and upload?无法选择图片并上传?
【发布时间】:2017-11-10 21:34:52
【问题描述】:

我正在自学 FireBase 存储,因此我决定制作一个简单的应用程序,让用户选择一张图片并将其上传到 Firebase。 但是,一旦我选择要上传的图片,我的应用就会崩溃并说我的 URI 为空,即使我是从我选择的图片中获取它的

Full stacktrace

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

        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK) {
            return;
        }
        if (requestCode == 1) {
            final Bundle extras = data.getExtras();
            if (extras != null) {
                //Get image
                 path = extras.getParcelable("data");
            }
            StorageReference storageRef = FirebaseStorage.getInstance().getReference();
            storageRef.putFile(path)
                    .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                            // Get a URL to the uploaded content
                            @SuppressWarnings("VisibleForTests") Uri downloadUrl = taskSnapshot.getDownloadUrl();
                            Log.e("myTag", "done, " +downloadUrl);
                            Intent sharingIntent = new Intent(Intent.ACTION_SEND);     
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception exception) {
                          Log.e("myTag", exception.toString());
                        }
                    });
        }
    }

我已经尝试解决这个错误超过五个小时,尽管阅读了十亿次文档,但我仍然感到困惑。任何帮助表示赞赏!

【问题讨论】:

    标签: java android database algorithm firebase


    【解决方案1】:

    选定的Uri 作为the datathe data 返回,而不是作为额外的。试试这个改变:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK) {
            return;
        }
        if (requestCode == 1) {
            path = data.getData(); // assumes path is declared as a Uri
            StorageReference storageRef = FirebaseStorage.getInstance().getReference("images");
            storageRef.putFile(path)...
    

    【讨论】:

    • 非常感谢您的回答!我试过了,现在我的异常更改为Cannot upload to getRoot. You should upload to a storage location such as .getReference('image.png').putFile...
    • 我还尝试调整 firebase 中的规则以允许读取和正确,但我仍然收到此错误。我该怎么办?
    • @RuchirBaronia:我更新了我的答案。将您的 ref 创建到 root 的子文件夹,例如“图片”:StorageReference storageRef = FirebaseStorage.getInstance().getReference("images")
    猜你喜欢
    • 1970-01-01
    • 2014-09-03
    • 2017-07-19
    • 2020-09-19
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    相关资源
    最近更新 更多