如何将图像从Firebase实时数据库放置到电话数据库

时间:2019-01-14 12:41:07

标签: android firebase firebase-realtime-database

我正在尝试从Firebase Realtime Database拍摄图像并将其放入手机存储中。我看到getDownloadUrl()不再受支持,并且在StackOverflow上看到,现在必须按照下面的方法下载Url。除了profilepicUrl,所有内容都添加到数据库中。

private void saveUserInfo()
{

    if (profilePicUri != null)
    {
        // The file path to the Photo folder in firebase storage
        StorageReference path = mStorageReference.child("Profile_Photo/Users/" + userId +".jpg");

        // Change the Uri file to an image using a Bitmap
        try
        {
            bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), profilePicUri);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        //Compress image size to save space
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 20, byteArrayOutputStream);
        byte [] imageData = byteArrayOutputStream.toByteArray();

        //Upload the image too Firebase Storage
        UploadTask uploadTask = path.putBytes(imageData);

        uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>()
        {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot)
            {
                Task<Uri> uri = taskSnapshot.getStorage().getDownloadUrl();
                profilePicUrl = uri.toString();
            }
        });
    }

    Users user = new Users(firstName,lastName,username,profilePicUrl,level,steps,distanceWalked);

    database.child(currentUser.getUid()).setValue(user);

   // startActivity(new Intent(this, MainActivity.class));
}

1 个答案:

答案 0 :(得分:0)

onSuccess(UploadTask.TaskSnapshot taskSnapshot)内部,继续获取图片的网址:

  uploadTask.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {
                        String imageUri = uri.toString(); 
                     //image url you can store in database

                    }
                })