【问题标题】:how to change code T_T Firebase getDownloadUrl(); for 'firebase-storage:16.0.1'如何更改代码 T_T Firebase getDownloadUrl();对于'firebase-storage:16.0.1'
【发布时间】:2018-07-23 08:09:51
【问题描述】:

如何更改代码T_T?

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

'firebase-storage 11.8.0' -> 'firebase-storage:16.0.1' 'firebase-storage : 16.0.1' 的版本导致错误 获取下载地址 () ;

现在这是我的代码

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();

        }
    }


}
}

这里的解决方案是 -> enter link description here

那么如何更改代码 TT ?

【问题讨论】:

    标签: android firebase firebase-storage


    【解决方案1】:

    确实,从文件中获取 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()
                        }
                    });
    

    或者您可以按照文档中的建议进行操作(您提供的链接)https://firebase.google.com/docs/storage/android/upload-files?authuser=0#get_a_download_url 你有同样的方法上传任务,所以在这里:

    if (task != null) {
    

    你可以有以下:

        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
                    // ...
                }
            }
        });
    

    【讨论】:

      猜你喜欢
      • 2021-02-15
      • 1970-01-01
      • 2022-01-04
      • 2019-04-02
      • 2017-02-25
      • 1970-01-01
      • 2018-09-25
      • 1970-01-01
      • 2021-04-06
      相关资源
      最近更新 更多