在上传到Firebase之前如何缩小图像尺寸?

时间:2018-12-27 06:44:30

标签: android firebase firebase-storage

在上传到Firebase之前如何缩小图像尺寸?或之后

这是我的第一个应用程序 https://play.google.com/store/apps/details?id=com.devkang.mystep

我认为上载和下载时间太长。

private void sendPost() {
    mProgressDialog.setMessage("Sending post...");
    mProgressDialog.setCancelable(false);
    mProgressDialog.setIndeterminate(true);
    mProgressDialog.show();

    FirebaseUtils.getUserRef(FirebaseUtils.getCurrentUser().getEmail().replace(".", ","))
            .addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    User user = dataSnapshot.getValue(User.class);
                    final String postId = FirebaseUtils.getUid();
                    TextView postDialogTextView = (TextView) mRootView.findViewById(R.id.post_dialog_edittext);
                    String text = postDialogTextView.getText().toString();

                  //add hashTag
                    EditText tag = (EditText) mRootView.findViewById(R.id.post_dialog_hash_tag);
                    String hashTag = tag.getText().toString();

                    mPost.setUser(user);
                    mPost.setNumComments(0);
                    mPost.setNumLikes(0);
                    mPost.setTimeCreated(System.currentTimeMillis());
                    mPost.setPostId(postId);
                    mPost.setPostText(text);
                    mPost.setPostHashTag(hashTag);

                    if (mSelectedUri != null) {
                        FirebaseUtils.getImageSRef()
                                .child(mSelectedUri.getLastPathSegment())
                                .putFile(mSelectedUri)
                                .addOnSuccessListener(getActivity(),
                                        new OnSuccessListener<UploadTask.TaskSnapshot>() {
                                            @Override
                                            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                                                String url = Constants.POST_IMAGES + "/" + mSelectedUri.getLastPathSegment();
                                                mPost.setPostImageUrl(url);
                                                addToMyPostList(postId);
                                            }
                                        });
                    } else {
                        addToMyPostList(postId);
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    mProgressDialog.dismiss();
                }
            });
}

2 个答案:

答案 0 :(得分:0)

Compressor Lib  试试这个lib。 Compressor可让您将大型照片压缩为较小尺寸的照片,而图像质量的损失则很小或可忽略不计。

答案 1 :(得分:0)

您可以使用位图压缩算法(例如PNG压缩(无损压缩))压缩图像文件,然后将压缩的图像保存到位置(mNewSelectedUri)并上传到Firebase。

package com.travelplanner.rest.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;

@Entity
@Table(name = "users_details")
public class UserDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @Column(name = "email")
    private String email;

    @Transient
    private String password;

    @OneToMany(mappedBy = "userDetails", cascade = CascadeType.ALL)
    @JsonIgnore
    @JsonManagedReference
    private List<TravelPlans> travelPlans;

    public UserDetails() {
    }

    public UserDetails(int id, String firstName, String lastName, String email) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public List<TravelPlans> getTravelPlans() {
        return travelPlans;
    }

    public void setTravelPlans(List<TravelPlans> travelPlans) {
        this.travelPlans = travelPlans;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email + "]";
    }

    public void add(TravelPlans travelPlan) {
        if (travelPlans == null) {
            travelPlans = new ArrayList<>();
        }

        travelPlans.add(travelPlan);

        travelPlan.setUserDetails(this);
    }

}

注意: 如果要检查图像文件的大小,可以在上传之前使用以下方法检查文件大小。它将返回以MB为单位的文件大小。 / em>

package com.travelplanner.rest.entity;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonBackReference;

@Entity
@Table(name="travel_plans")
public class TravelPlans {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="id")
    private int id;

    @Column(name="place")
    private String place;

    @Column(name="hotel")
    private String hotel;

    @Column(name="transport")
    private String transport;

    @Column(name="date_from")
    private String dateFrom;

    @Column(name="date_till")
    private String dateTill;

    @Column(name="created_at")
    private String createdAt;

    @JsonBackReference
    @ManyToOne(cascade= {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
    @JoinColumn(name="user_id")
    private UserDetails userDetails;

    public TravelPlans() {}

    public TravelPlans(int id, String place, String hotel, String transport, String dateFrom, String dateTill,
            String createdAt) {
        super();
        this.id = id;
        this.place = place;
        this.hotel = hotel;
        this.transport = transport;
        this.dateFrom = dateFrom;
        this.dateTill = dateTill;
        this.createdAt = createdAt;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getPlace() {
        return place;
    }

    public void setPlace(String place) {
        this.place = place;
    }

    public String getHotel() {
        return hotel;
    }

    public void setHotel(String hotel) {
        this.hotel = hotel;
    }

    public String getTransport() {
        return transport;
    }

    public void setTransport(String transport) {
        this.transport = transport;
    }

    public String getDateFrom() {
        return dateFrom;
    }

    public void setDateFrom(String dateFrom) {
        this.dateFrom = dateFrom;
    }

    public String getDateTill() {
        return dateTill;
    }

    public void setDateTill(String dateTill) {
        this.dateTill = dateTill;
    }

    public String getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(String createdAt) {
        this.createdAt = createdAt;
    }

    public UserDetails getUserDetails() {
        return userDetails;
    }

    public void setUserDetails(UserDetails userDetails) {
        this.userDetails = userDetails;
    }

    @Override
    public String toString() {
        return "TravelPlans [id=" + id + ", place=" + place + ", hotel=" + hotel + ", transport=" + transport + "]";
    }

}

参考:figured out