如何使用Fresco

时间:2016-11-22 07:08:58

标签: android

以下是我如何从Firebase存储中下载图片或任何文件

// Create a storage reference from our app
StorageReference storageRef = storage.getReferenceFromUrl("gs://<your-bucket-name>");

// Create a reference with an initial file path and name
StorageReference pathReference = storageRef.child("images/stars.jpg");

    File localFile = File.createTempFile("images", "jpg");

    storageRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
            // Local temp file has been created
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            // Handle any errors
        }
    }); 

以下是我如何使用Fresco显示任何图像

GenericDraweeHierarchyBuilder builder =
                new GenericDraweeHierarchyBuilder(context.getResources());
        GenericDraweeHierarchy hierarchy = builder
                .setFadeDuration(100)
                .build();

        hierarchy.setActualImageScaleType(ScalingUtils.ScaleType.FOCUS_CROP);
        hierarchy.setActualImageFocusPoint(new PointF(0.5f,0f));

        Uri uri = Uri.parse(<imageUrl>);
        viewHolder.ivImage.setImageURI(uri);
        viewHolder.ivImage.setHierarchy(hierarchy);
        viewHolder.ivImage.setAspectRatio(1.15f);

所以我想要一个下载网址或uri,我可以将其设置为Fresco uri的变量名称。

我的问题是如何从Firebase存储中获取图片网址?

1 个答案:

答案 0 :(得分:1)

以下是我们如何从firebase存储中获取下载URL。

storageRef.child("users/me/profile.png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        @Override
        public void onSuccess(Uri uri) {
            // Got the download URL for 'users/me/profile.png'
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            // Handle any errors
        }
    });

现在创建uri并使用Fresco显示它。

GenericDraweeHierarchyBuilder builder =
                new GenericDraweeHierarchyBuilder(context.getResources());
        GenericDraweeHierarchy hierarchy = builder
                .setFadeDuration(100)
                .build();

        hierarchy.setActualImageScaleType(ScalingUtils.ScaleType.FOCUS_CROP);
        hierarchy.setActualImageFocusPoint(new PointF(0.5f,0f));

        Uri uri = Uri.parse(<downloadUrl>);
        viewHolder.ivImage.setImageURI(uri);
        viewHolder.ivImage.setHierarchy(hierarchy);
        viewHolder.ivImage.setAspectRatio(1.15f);