下面列出了com.bumptech.glide.load.model.ModelLoader#com.avos.avoscloud.AVFile 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void uploadLogFile(String user, String device, File gzFile) throws Exception {
Logger.i("DemoLeanCloudUploader-->uploadLogFile,gzFile:" + gzFile);
if (gzFile == null || !gzFile.isFile()) {
Logger.e("DemoLeanCloudUploader-->uploadLogFile,not exists");
return;
}
String leanCloudFileName = getLeanCloudFileName(user, device, gzFile.getName());
Logger.i("DemoLeanCloudUploader-->uploadLogFile,leanCloudFileName:" + leanCloudFileName);
try {
AVFile avFile = AVFile.withAbsoluteLocalPath(leanCloudFileName, gzFile.getAbsolutePath());
avFile.save();
gzFile.delete();
Logger.i("DemoLeanCloudUploader-->uploadLogFile,success");
} catch (FileNotFoundException ex) {
ex.printStackTrace();
throw ex;
} catch (AVException avException) {
avException.printStackTrace();
throw avException;
}
}
/**
* 设置用户信息
*/
private void setUserInfo() {
User user = UserUtils.getUser();
if(user != null){
String username = user.getUsername();
int sex = user.getSex();
int age = user.getAge();
mCilAge.setRightText(String.valueOf(age).concat(UiUtils.getString(R.string.year)));
mCilSex.setRightText(UserUtils.getSexText(sex));
mCilUsername.setRightText(username);
AVFile avatar = user.getAvatar();
if(avatar != null){
ImageLoader.getIns(this).loadIcon(avatar.getUrl(), mImgIcon);
}
}
}
@Override
public void onBindViewHolder(final FriendListViewHolder holder, final int position)
{
AutoUtils.autoSize(holder.itemView);
User user = mDatas.get(position);
holder.mCb.setVisibility(View.GONE);
holder.mTvUsername.setText(user.getUsername());
holder.mTvSimpleDesc.setText(user.getSimpleDesc());
AVFile avatar = user.getAvatar();
if (avatar != null)
PicassoUtils.displayFitImage(mContext, Uri.parse(avatar.getThumbnailUrl(true, Constant
.AVATAR_WIDTH, Constant.AVATAR_HEIGHT)), holder.mIvAvatar, null);
else holder.mIvAvatar.setImageResource(R.mipmap.avatar);
holder.itemView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (mListener != null)
mListener.onClick(holder, position);
}
});
}
@Override
public void afterCreate()
{
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) actionBar.setTitle("");
User me = AVUser.getCurrentUser(User.class);
if (me == null)
{
Toast.makeText(this, "请先进行登录,", Toast.LENGTH_SHORT).show();
finish();
}
//初始化用户信息
mTvUsername.setText(me.getUsername());
AVFile avAvatar = me.getAvatar();
if (avAvatar != null)
PicassoUtils.displayFitImage(this, Uri.parse(avAvatar.getThumbnailUrl(true, Constant
.AVATAR_WIDTH, Constant.AVATAR_HEIGHT)), mIvAvatar, null);
//初始化事件
initEvent();
//初始化viewpager
initViewPagerAndTabLayout();
}
/**
* 压缩上传
*
* @return
*/
public Observable<AVFile> compressImageAndUpload(String path, int maxWidth, int maxHeight)
{
return compressImage(path, maxWidth, maxHeight)
.observeOn(Schedulers.io())
.subscribeOn(Schedulers.computation())
.flatMap(new Func1<AVFile, Observable<AVFile>>()
{
@Override
public Observable<AVFile> call(AVFile avFile)
{
return uploadFile(avFile);
}
});
}
public Observable<AVFile> uploadFile(final AVFile file)
{
return Observable.create(new Observable.OnSubscribe<AVFile>()
{
@Override
public void call(Subscriber<? super AVFile> subscriber)
{
try
{
file.save();
subscriber.onNext(file);
} catch (AVException e)
{
subscriber.onError(e);
e.printStackTrace();
}
}
});
}
/**
* 压缩图片
*/
public Observable<AVIMMessage> compressImage(final AVIMImageMessage message)
{
//压缩上传的图片并重新构造 message
int maxWidth = (int) (ScreenUtils.getScreenWidth(mActivity) * ChatConstant
.MESSAGE_PIC_WIDTH_MAX_RATIO);
int maxHeight = (int) (ScreenUtils.getScreenHeight(mActivity) * ChatConstant
.MESSAGE_PIC_HEIGHT_MAX_RATIO);
return mChatBll.compressImageAndUpload(message.getLocalFilePath(), maxWidth, maxHeight)
.observeOn(AndroidSchedulers.mainThread())
.flatMap(new Func1<AVFile, Observable<AVIMMessage>>()
{
@Override
public Observable<AVIMMessage> call(AVFile file)
{
AVIMImageMessage msg = new AVIMImageMessage(file);
Map<String, Object> attrs = message.getAttrs();
msg.setAttrs(attrs);
AVIMMessage avimMessage = msg;
return Observable.just(avimMessage);
}
});
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
final Map<String, Object> item = new HashMap<String, Object>();
AVUser user = (AVUser) MeetList.get(position).get("user");
String name = user.getUsername();
AVFile avatar = user.getAVFile("avatar");
String avatarUrl = getAvatarUrl(user);
String times = MeetList.get(position).get("times").toString();
times = "相遇" + times + "次";
item.put("name", name);
item.put("avatar", avatarUrl);
item.put("times", times);
return item;
}
/**
* 设置用户信息
*/
private void setUserInfo(){
User user = UserUtils.getUser();
if(user != null){
String username = user.getUsername();
String phone = user.getMobilePhoneNumber();
mTxtUsername.setText(username);
mTxtPhone.setText(phone);
AVFile avatar = user.getAvatar();
if(avatar != null){
ImageLoader.getIns(this).loadIcon(avatar.getUrl(), mImgIcon);
}
}
}
@Override
protected void convert(BaseViewHolder helper, AccountBook item) {
int coverId = UiUtils.getImageResIdByName(item.getCover() != null
? item.getCover() : UiUtils.getString(R.string.def_book_cover));
int sceneId = UiUtils.getSceneImageResIdByName(item.getScene());
helper.setImageResource(R.id.img_cover, coverId)
.setImageResource(R.id.img_scene, sceneId)
.setText(R.id.txt_name, item.getName())
// .setText(R.id.txt_total_cost, item.getTotalCost())
// .setText(R.id.txt_total_income, item.getTotalIncome())
.setVisible(R.id.txt_current, item.isCurrent());
LinearLayout lltIcons = helper.getView(R.id.llt_icons);
lltIcons.removeAllViews();
for (User share : item.getShares()) {
ImageView imgIcon = new ImageView(mContext);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(DimenUtils.dp2px(25), DimenUtils.dp2px(25));
lp.leftMargin = DimenUtils.dp2px(5);
imgIcon.setLayoutParams(lp);
AVFile avatar = share.getAvatar();
if(avatar != null){
ImageLoader.getIns(mContext).loadIcon(avatar.getUrl(), imgIcon);
}else{
imgIcon.setImageResource(R.mipmap.ic_def_icon);
}
lltIcons.addView(imgIcon);
}
helper.addOnClickListener(R.id.img_add_user);
}
@Override
protected void convert(BaseViewHolder helper, Msg item) {
ImageView imgAvatar = helper.getView(R.id.img_avatar);
// 判断消息类型
if(item.getType() == AppConfig.TYPE_MSG_APPLY_BOOK){// 申请加入帐薄
// 设置头像
User applyUser = item.getApplyUser();
AVFile avatar = applyUser.getAvatar();
if(avatar != null){
ImageLoader.getIns(mContext).loadIcon(avatar.getUrl(), imgAvatar);
}else{
imgAvatar.setImageResource(R.mipmap.ic_def_icon);
}
// 设置昵称
helper.setText(R.id.txt_username, applyUser.getUsername());
// 设置帐薄名
AccountBook applyBook = item.getApplyBook();
String applyBookSign = UiUtils.getString(R.string.apply_book_sign);
if(applyBook != null){
helper.setText(R.id.btn_agree, UiUtils.getString(R.string.agree))
.setText(R.id.txt_book, String.format(applyBookSign, applyBook.getName()));
}else{
helper.setText(R.id.btn_agree, UiUtils.getString(R.string.delete_msg))
.setText(R.id.txt_book, String.format(applyBookSign, UiUtils.getString(R.string.booK_has_delete)));
}
// 添加点击事件
helper.setVisible(R.id.btn_agree, true)
.addOnClickListener(R.id.btn_agree);
}else{
imgAvatar.setImageResource(R.mipmap.ic_sys_msg);
helper.setText(R.id.txt_username, UiUtils.getString(R.string.system_message))
.setText(R.id.txt_book, UiUtils.show(item.getContent()))
.setVisible(R.id.btn_agree, false);
}
}
public void getUserInfoSuccess(User user) {
mCurrentUser = user;
mTvUsername.setText(mCurrentUser.getUsername());
final String avatarUrl = user.getAvatarUrl();
if (avatarUrl != null) {
PicassoUtils.displayFitImage(this, Uri.parse(avatarUrl), mIvAvatar, null);
}
final AVFile avFile = user.getAvatar();
mIvAvatar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (avFile != null) {
int width = (int) (avFile.getMetaData("width") == null ? 500 : avFile.getMetaData
("width"));
int height = (int) (avFile.getMetaData("height") == null ? 500 : avFile.getMetaData
("height"));
Intent intent = new Intent(PersonalCenterActivity.this, ShowImageActivity.class);
intent.putExtra(ShowImageActivity.EXTRA_HEIGHT, height);
intent.putExtra(ShowImageActivity.EXTRA_WIDTH, width);
intent.putExtra(ShowImageActivity.EXTRA_URI, Uri.parse(avFile.getUrl()));
startActivity(intent);
}
}
});
initViewPagerAndTab();
mSplashView.loadFinish();
}
/**
* 为了避免重复的inflate以及addview,先判断是否是第一次设置,
* 不是的话则拿到上次更新到的下标,从这个下标开始,添加新更新的数据
*/
public void setAcceptInviteUsers() {
if (mIsAcceptUserSet && mLlAcceptUser.getChildCount() > 5)//最多只同时显示5个受约的小伙伴
{
return;
}
List<User> acceptInviteUsers = mInvitation.getAcceptInviteUsers();
int count = Math.min(acceptInviteUsers.size(), 5);
int startIndex = 0;
if (mIsAcceptUserSet)//如果已经设置过了
{
startIndex = mAcceptInviteNums;
}
for (int i = startIndex; i < count; i++) {
User user = acceptInviteUsers.get(i);
View view = LayoutInflater.from(this).inflate(R.layout.item_accept_invite_user,
mLlAcceptUser, false);
mLlAcceptUser.addView(view, mLlAcceptUser.getChildCount());
((TextView) view.findViewById(R.id.tvUsername)).setText(user.getUsername());
ImageView ivAvatar = (ImageView) view.findViewById(R.id.ivAvatar);
//设置头像
AVFile avFile = user.getAvatar();
if (avFile != null) {
PicassoUtils.displayFitImage(this, Uri.parse(avFile.getThumbnailUrl(true, 100,
100)), ivAvatar, null);
}
}
mIsAcceptUserSet = true;
mAcceptInviteNums = count;
}
/**
* 下载图片,包含内容中的图片以及受约用户的头像
*/
private void downloadPic() {
if (!mIsContentPicLoaded)//如果已经加载过就不要继续加载了
{
int screenWidth = ScreenUtils.getScreenWidth(this);
int screenHeight = ScreenUtils.getScreenHeight(this);
List<AVFile> contentFile = mInvitation.getPics();
if (contentFile != null && contentFile.size() > 0) {
for (int i = 0; i < contentFile.size(); i++) {
final AVFile file = contentFile.get(i);
final int originalImageWidth = (int) file.getMetaData("width");
final int originalImageHeight = (int) file.getMetaData("height");
double[] ratio = FileUtils.compressIfMoreThanDesireHeightWidth
(originalImageWidth, originalImageHeight, CONTENT_PIC_WIDTH_MAX_RATIO,
CONTENT_PIC_HEIGHT_MAX_RATIO, this);
final int width = (int) (screenWidth * ratio[0]);
final int height = (int) (screenHeight * ratio[1]);
ImageView imageView = getContentImageView(width, height);
//将图片添加到内容容器的末尾
mLlContentContainer.addView(imageView, mLlContentContainer.getChildCount());
//点击图片查看高清无码大图
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(InvitationDetailActivity.this,
ShowImageActivity.class);
intent.putExtra(ShowImageActivity.EXTRA_URI, Uri.parse(file.getUrl()))
.putExtra(ShowImageActivity.EXTRA_WIDTH, originalImageWidth)
.putExtra(ShowImageActivity.EXTRA_HEIGHT, originalImageHeight);
startActivity(intent);
}
});
PicassoUtils.displayFitImage(this, Uri.parse(file.getThumbnailUrl(false, width,
height)),
imageView, null);
}
}
mIsContentPicLoaded = true;
}
}
private void loadData() {
AVFile avatar = AVUser.getCurrentUser(User.class).getAvatar();
if (avatar != null) {
avatar.getThumbnailUrl(false, 100, 100);
String url = avatar.getUrl();
PicassoUtils.displayFitImage(this, Uri.parse(url), mIvAvatar, null);
}
mSwipeRefreshLayout.setRefreshing(true);
List<Invitation> datas = new ArrayList<>();
mRvInvitation.setAdapter(mRvInvitationAdapter = new HomeInvitationItemAdapter(this,
datas));
mController.getNewlyInvitationDatas();
}
public Observable<String> uploadAvatar(final String path)
{
return Observable.create(new Observable.OnSubscribe<String>()
{
@Override
public void call(Subscriber<? super String> subscriber)
{
File file = new File(path);
try
{
//对头像进行压缩
Bitmap bitmap = FileUtils.compressImage(path, Constant.UPLOAD_AVATAR_WIDTH, Constant
.UPLOAD_AVATAR_HEIGHT);
ByteArrayOutputStream os = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 60, os);
AVFile avatar = new AVFile(System.currentTimeMillis() + "", os.toByteArray());
avatar.addMetaData("width", bitmap.getWidth());
avatar.addMetaData("height", bitmap.getHeight());
avatar.save();
User user = AVUser.getCurrentUser(User.class);
user.setAvatar(avatar);
user.save();
subscriber.onNext(path);
} catch (Exception e)
{
e.printStackTrace();
subscriber.onError(e);
}
}
});
}
public String getAvatarUrl()
{
AVFile avFile = this.getAvatar();
if (avFile != null)
return avFile.getThumbnailUrl(true, Constant.AVATAR_WIDTH, Constant.AVATAR_HEIGHT);
else return null;
}
/**
* 其实 leancloud 发送消息的时候会自动为 message 赋这些值。
* 为什么我还要自己来赋值呢?因为拓麻的 observable 有延迟啊。
* <p/>
* 初始化一些信息,并返回属性map
*/
public Map<String, Object> initMessage(AVIMMessage message)
{
message.setTimestamp(System.currentTimeMillis());
message.setFrom(AVImClientManager.getInstance().getClientId());
Map<String, Object> attrs = new HashMap<>();
AVFile avatar = AVUser.getCurrentUser(User.class).getAvatar();
attrs.put(ChatConstant.MSG_ATTR_AVATAR, avatar != null ? avatar.getThumbnailUrl(true,
Constant.AVATAR_WIDTH, Constant.AVATAR_HEIGHT) : null);
return attrs;
}
/**
* 压缩聊天中的图片
*/
public Observable<AVFile> compressImage(String path, int maxWidth, int maxHeight)
{
Bitmap bitmap = FileUtils.compressImage(path, maxWidth, maxHeight);
ByteArrayOutputStream os = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 60, os);
//构造 message
AVFile file = new AVFile(System.currentTimeMillis() + "", os.toByteArray());
file.addMetaData("width", bitmap.getWidth());
file.addMetaData("height", bitmap.getHeight());
return Observable.just(file);
}
public static String getUrl(AVFile file, int width) {
if (AVOSCloud.getStorageType() == AVOSCloud.StorageType.StorageTypeQiniu) {
return getUrl(file.getUrl(), width);
} else {
return file.getUrl();
}
}
public String getAvatarUrl(AVUser user) {
AVFile avatar = user.getAVFile("avatar");
if (avatar != null) {
return avatar.getUrl();
} else {
return null;
}
}
public String getAvatarUrl(AVUser user) {
AVFile avatar = user.getAVFile("avatar");
if (avatar != null) {
return avatar.getUrl();
} else {
return null;
}
}
public String getAvatarUrl(AVUser user) {
AVFile avatar = user.getAVFile("avatar");
if (avatar != null) {
return avatar.getUrl();
} else {
return null;
}
}
public String getAvatarUrl(AVUser user) {
AVFile avatar = user.getAVFile("avatar");
if (avatar != null) {
return avatar.getUrl();
} else {
return null;
}
}
public String getAvatarUrl(AVUser user) {
AVFile avatar = user.getAVFile("avatar");
if (avatar != null) {
return avatar.getUrl();
} else {
return null;
}
}
public String getAvatarUrl(AVUser user) {
AVFile avatar = user.getAVFile("avatar");
if (avatar != null) {
return avatar.getUrl();
} else {
return null;
}
}
public void upload() throws IOException, AVException {
if (msg.getType() != Msg.Type.Audio && msg.getType() != Msg.Type.Image) {
return;
}
String objectId = msg.getObjectId();
if (objectId == null) {
throw new NullPointerException("objectId mustn't be null");
}
String filePath = PathUtils.getChatFilePath(objectId);
AVFile file = AVFile.withAbsoluteLocalPath(objectId, filePath);
file.save();
String url = file.getUrl();
msg.setContent(url);
}
public String getAvatarUrl(AVUser user) {
AVFile avatar = user.getAVFile("avatar");
if (avatar != null) {
return avatar.getUrl();
} else {
return null;
}
}
public String getAvatarUrl(AVUser user) {
AVFile avatar = user.getAVFile("avatar");
if (avatar != null) {
return avatar.getUrl();
} else {
return null;
}
}
/**
* 下载图片
*/
public void downloadImage() {
isDownloadImage(this.new IsDownloadCallBack() {
@Override
public void done(boolean isDownload, final AVObject object) {
if (isDownload) {
if (object != null) {
AVFile file = object.getAVFile(IMAGE);
String url = file.getUrl();
Download download = new Download(url);
download.dowmSdInBackground(DIR, FILE_NAME, download.new DoneCallBack() {
@Override
public void done(String path) {
sp.edit().putInt("image", object.getInt(ID)).apply();
}
}, download.new ProgressCallback() {
@Override
public void done(int progress) {
}
}, download.new FailedCallback() {
@Override
public void failed(Exception e) {
}
});
}
}
}
});
}