为什么不生成Firebase存储映像下载链接? [关闭]

问题描述 投票:0回答:1

Image Of Code for getting Download Url我来自孟加拉国,我听说服务器中存在某些问题,这是真的吗?我非常需要一种解决方案,我已经尝试了Internet上所有可用的方法。

android firebase firebase-storage
1个回答
0
投票

要将图像上传到云存储,您需要遵循代码

在gradle中实施库以选择图像格式存储:

implementation 'com.theartofdev.edmodo:android-image-cropper:2.8.0'

清单中:

<activity
        android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
        android:theme="@style/Base.Theme.AppCompat" />

活动中:

private Uri imageUri;
private ImageView _itemImageView;
private Button _uploadBtn;

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

 _itemImageView = findViewById(/*your id here*/);
 _uploadBtn = findViewById(/*your id here*/);

_itemImageView.setOnClickListener(n -> {
     CropImage.activity().setGuidelines(CropImageView.Guidelines.ON).setAspectRatio(1, 1).start(AddNewItemActivity.this);
  });

 _uploadBtn.setOnClickListener(n -> {
   if (imageUri != null) {
     uploadImage();
   }

 });

}

 private void uploadImage() {
    if (imageUri != null) {

        File newImageFile = new File(imageUri.getPath());

        _progressBar.setVisibility(View.VISIBLE);

        DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("your loaction ");
        String pushKey = reference.push().getKey();


        StorageReference storage = FirebaseStorage.getInstance().getReference("your storage location")
                .child("thumbs/");

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

        UploadTask uploadTask = storage.child(pushKey + ".jpeg").putBytes(thumbData);
        uploadTask.addOnSuccessListener(taskSnapshot -> {
            if (taskSnapshot.getMetadata() != null && taskSnapshot.getMetadata().getReference() != null) {
                Task<Uri> result = taskSnapshot.getStorage().getDownloadUrl();
                result.addOnSuccessListener(uri -> {
                    String thumbImageDownloadUri = uri.toString();

                    //here update the profile information to the firebase database or firestore
                    //do following
                    uploadData(thumbImageDownloadUri, reference, pushKey);
                });
            }
        }).addOnFailureListener(e -> {
            Toast.makeText(AddNewItemActivity.this, "error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
            _progressBar.setVisibility(View.GONE);
        });
    }

}
   //this will upload the download image url to the data base form where user can download image

   private void uploadData(String thumbImageUri, DatabaseReference reference, String pushKey) {
    reference.child(defItemCategory[0] + "/" + pushKey).setValue(
            /*add your custom data model*/
            new User(thumbImageUri)
    ).addOnCompleteListener(task -> {
        if (task.isSuccessful()) {
            Toast.makeText(AddNewItemActivity.this, "upload successful", Toast.LENGTH_SHORT).show();
            _progressBar.setVisibility(View.GONE);
        }
    }).addOnFailureListener(e -> {
        Toast.makeText(AddNewItemActivity.this, "error: " + e.getMessage(), Toast.LENGTH_SHORT).show();
        _progressBar.setVisibility(View.GONE);
    });

}



// get your selected image result 
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable 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) {
            imageUri = result.getUri();
            if (imageUri != null) {
                _itemImageView.setImageURI(imageUri);
            }
        } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
            Exception error = result.getError();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.