Android 如何更改代码T_T Firebase getDownloadUrl();对于“firebase存储:16.0.1”

Android 如何更改代码T_T Firebase getDownloadUrl();对于“firebase存储:16.0.1”,android,firebase,firebase-storage,Android,Firebase,Firebase Storage,如何更改代码T\u T 我将版本更改为错误解决方案 “firebase存储11.8.0”->“firebase存储:16.0.1” “firebase存储:16.0.1”版本导致的错误为 获取下载URL 这是我的密码 public class SetupActivity extends AppCompatActivity { //xml 레이아웃 private CircleImageView setupImage; private Uri mainImageURI = null; pr

如何更改代码T\u T

我将版本更改为错误解决方案

“firebase存储11.8.0”->“firebase存储:16.0.1” “firebase存储:16.0.1”版本导致的错误为 获取下载URL

这是我的密码

public class SetupActivity extends AppCompatActivity {



//xml 레이아웃
private CircleImageView setupImage;
private Uri mainImageURI = null;

private String user_id;

private boolean isChanged = false;

private EditText setupName;
private Button setupBtn;
private ProgressBar setupProgress;

private StorageReference mStorageRef;
private FirebaseAuth firebaseAuth;
private FirebaseFirestore firebaseFirestore;

private Bitmap compressedImageFile;

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

    Toolbar setupToolbar = findViewById(R.id.setupToolbar);
    setSupportActionBar(setupToolbar);
    getSupportActionBar().setTitle("계정 설정");

    //파이어베이스 이용 인스턴스

    firebaseAuth = FirebaseAuth.getInstance();


    //firebaseAuth의 사용자를 호출
    user_id = firebaseAuth.getCurrentUser().getUid();

    firebaseFirestore = FirebaseFirestore.getInstance();
    mStorageRef = FirebaseStorage.getInstance().getReference();


    setupImage = findViewById(R.id.setup_image);
    setupName = findViewById(R.id.setup_name);
    setupBtn = findViewById(R.id.setup_btn);
    setupProgress = findViewById(R.id.setup_progress);

    setupProgress.setVisibility(View.VISIBLE);
    setupBtn.setEnabled(false);

    firebaseFirestore.collection("Users").document(user_id).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DocumentSnapshot> task) {

            if (task.isSuccessful()) {

                if (task.getResult().exists()) {

                    String name = task.getResult().getString("name");
                    String image = task.getResult().getString("image");

                    mainImageURI = Uri.parse(image);

                    setupName.setText(name);


                    RequestOptions placeholderRequest = new RequestOptions();
                    placeholderRequest.placeholder(R.drawable.defaulticon);

                    Glide.with(SetupActivity.this).setDefaultRequestOptions(placeholderRequest).load(image).into(setupImage);


                }

            } else {

                String error = task.getException().getMessage();
                Toast.makeText(SetupActivity.this, "(FIRESTORE Retrieve Error) : " + error, Toast.LENGTH_LONG).show();

            }

            setupProgress.setVisibility(View.INVISIBLE);
            setupBtn.setEnabled(true);

        }
    });

    setupBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            final String user_name = setupName.getText().toString();

            if (!TextUtils.isEmpty(user_name) && mainImageURI != null) {

                setupProgress.setVisibility(View.VISIBLE);

                if (isChanged) {

                    user_id = firebaseAuth.getCurrentUser().getUid();

                    File newImageFile = new File(mainImageURI.getPath());
                    try {

                        compressedImageFile = new Compressor(SetupActivity.this)
                                .setMaxHeight(125)
                                .setMaxWidth(125)
                                .setQuality(50)
                                .compressToBitmap(newImageFile);

                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    compressedImageFile.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                    byte[] thumbData = baos.toByteArray();

                    UploadTask image_path = mStorageRef.child("profile_images").child(user_id + ".jpg").putBytes(thumbData);

                    image_path.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {

                            if (task.isSuccessful()) {
                                storeFirestore(task, user_name);

                            } else {

                                String error = task.getException().getMessage();
                                Toast.makeText(SetupActivity.this, "(IMAGE Error) : " + error, Toast.LENGTH_LONG).show();

                                setupProgress.setVisibility(View.INVISIBLE);

                            }
                        }
                    });

                } else {

                    storeFirestore(null, user_name);

                }

            }

        }

    });

    // IMAGE

    setupImage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

                if (ContextCompat.checkSelfPermission(SetupActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

                    Toast.makeText(SetupActivity.this, "Permission Denied", Toast.LENGTH_LONG).show();
                    ActivityCompat.requestPermissions(SetupActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);

                } else {

                    BringImagePicker();

                }

            } else {

                BringImagePicker();

            }

        }

    });


}


private void storeFirestore(@NonNull Task<UploadTask.TaskSnapshot> task, String user_name) {


    Uri download_uri;

    if (task != null) {


        download_uri = task.getResult().getDownloadUrl();

    } else {

        download_uri = mainImageURI;

    }

    Map<String, String> userMap = new HashMap<>();
    userMap.put("name", user_name);
    userMap.put("image", download_uri.toString());

    firebaseFirestore.collection("Users").document(user_id).set(userMap).addOnCompleteListener(new OnCompleteListener<Void>() {
        @Override
        public void onComplete(@NonNull Task<Void> task) {

            if (task.isSuccessful()) {

                Toast.makeText(SetupActivity.this, "The user Settings are updated.", Toast.LENGTH_LONG).show();
                Intent mainIntent = new Intent(SetupActivity.this, MainActivity.class);
                startActivity(mainIntent);
                finish();

            } else {

                String error = task.getException().getMessage();
                Toast.makeText(SetupActivity.this, "(FIRESTORE Error) : " + error, Toast.LENGTH_LONG).show();

            }

            setupProgress.setVisibility(View.INVISIBLE);

        }
    });


}

private void BringImagePicker() {

    CropImage.activity()
            .setGuidelines(CropImageView.Guidelines.ON)
            .setAspectRatio(1, 1)
            .start(SetupActivity.this);

}


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

    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);
        if (resultCode == RESULT_OK) {

            mainImageURI = result.getUri();
            setupImage.setImageURI(mainImageURI);

            isChanged = true;

        } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {

            Exception error = result.getError();

        }
    }


}
}
这里的解决方案是->


那么如何更改代码TT呢

事实上,从文件获取URL在Firebase存储中已更改,因此在storeFirestore方法中,您可以执行以下操作:

mStorageRef.child("profile_images").child(user_id + ".jpg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {
                       //Download: uri.toStirng()
                    }
                });
    task.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
    @Override
    public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
        if (!task.isSuccessful()) {
            throw task.getException();
        }

        // Continue with the task to get the download URL
        return ref.getDownloadUrl();
    }
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
        @Override
        public void onComplete(@NonNull Task<Uri> task) {
            if (task.isSuccessful()) {
                Uri downloadUri = task.getResult();
            } else {
                // Handle failures
                // ...
            }
        }
    });
您可以拥有以下内容:

mStorageRef.child("profile_images").child(user_id + ".jpg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {
                       //Download: uri.toStirng()
                    }
                });
    task.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
    @Override
    public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
        if (!task.isSuccessful()) {
            throw task.getException();
        }

        // Continue with the task to get the download URL
        return ref.getDownloadUrl();
    }
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
        @Override
        public void onComplete(@NonNull Task<Uri> task) {
            if (task.isSuccessful()) {
                Uri downloadUri = task.getResult();
            } else {
                // Handle failures
                // ...
            }
        }
    });