【问题标题】:android studio getDownloadUrl() resolve [duplicate]android studio getDownloadUrl()解决[重复]
【发布时间】:2021-12-12 01:27:45
【问题描述】:

我知道有几种方法可以替换 getDownloadUrl(),但它对我不起作用。 如果有人知道好的方法请帮助我

所以我正在尝试上传一些带有照片的帖子。除了我上传的图片外,其他的东西都会出现。我认为 getDownloadUrl() 是问题所在。

 public void onClick(View v) {
        if (v.getId() == R.id.addphoto_btn_upload && photoUrl != null) {

            Log.d("AddPhotoActivity", " upload button clicked");

            File myFile = new File(photoUrl);
            Uri contentUri = Uri.fromFile(myFile);

            StorageReference storageReference = firebaseStorage.getReferenceFromUrl("gs://mystagram.appspot.com/").child("images").child(contentUri.getLastPathSegment());

            UploadTask uploadTask = storageReference.putFile(contentUri);
            uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    Toast.makeText(AddPhotoActivity.this, getString(R.string.upload_success), Toast.LENGTH_SHORT).show();




                    Task<Uri> uri = taskSnapshot.getStorage().getDownloadUrl();
 


                  
                    DatabaseReference images = firebaseDatabase.getReference().child("images").push();

                    //Time
                    Date date = new Date();
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                    ContentDTO contentDTO = new ContentDTO();


                   
                    contentDTO.imageUrl = uri.toString();

                    contentDTO.explain = explain_editText.getText().toString();
                    
                    contentDTO.userId = firebaseAuth.getCurrentUser().getEmail();
                    
                    contentDTO.timeStamp = Long.valueOf(simpleDateFormat.format(date));

                    images.setValue(contentDTO);
                    AddPhotoActivity.this.finish();

                }
            });

我该如何更改?

【问题讨论】:

    标签: java android firebase android-studio deprecated


    【解决方案1】:

    任务 uri = taskSnapshot.getStorage().getDownloadUrl();

    这不会为您提供可下载的链接,您必须使用 addOnCompleteListener 为您提供Task&lt;UploadTask.TaskSnapshot&gt; task,通过您可以获得您的文件链接。

    这里有一个运行示例:

        public void uploadImageSavePost(Bitmap bitmap) {
            StorageReference ref = FirebaseStorage.getInstance().getReference();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 20, baos);
            byte[] data = baos.toByteArray();
            Long tsLong = System.currentTimeMillis() / 1000;
            String ts = tsLong.toString();
            final UploadTask uploadTask = ref.child("images/" + ts + ".jpg").putBytes(data);
            uploadTask.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                if (task.isSuccessful()) {
                    task.getResult().getMetadata().getReference().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                            img = uri.toString(); // here it is the link of your file.
                            savePost();
                        }
                    });
                }
            }
        });
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-18
      • 2016-08-27
      • 2019-02-13
      • 1970-01-01
      • 2019-02-08
      • 1970-01-01
      • 2018-03-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多