Bmob的使用-上传图片
生活随笔
收集整理的這篇文章主要介紹了
Bmob的使用-上传图片
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Android Bmob 的圖片上傳與下載
| 上傳方法 | 上床圖片后放回一個 BmobFile 對象,保存到 User 里 |
| 下載方法 | 根據(jù)所獲得 User 的BmobFile 成員信息,調用 .getUrl() 方法獲得 Url 進行下載 |
實戰(zhàn)項目:
億簽-讓簽到更簡單( 實驗室簽到系統(tǒng) ):點擊跳轉 https://github.com/FishInWater-1999/android-SignInSystem
圖片上傳部分
注:picPath 為訪問相冊或相機拍照后,獲得的圖片路徑
/*定義一個Handler,定義延時執(zhí)行的行為*/ public void chnageImage(){final String picPath = path;final BmobFile bmobFile = new BmobFile(imageFactory(picPath));bmobFile.uploadblock(new UploadFileListener() {@Overridepublic void done(BmobException e) {if(e==null){user.setImageFile(bmobFile);user.update(new UpdateListener() {@Overridepublic void done(BmobException e) {if (e == null) {Toast.makeText(MainActivity.this, "頭像已更新", Toast.LENGTH_SHORT).show();} else {}}});//bmobFile.getFileUrl()--返回的上傳文件的完整地址new Thread(){@Overridepublic void run() {while ( bitmap == null ){bitmap = BitmapFactory.decodeFile(picPath);}Message message = handler.obtainMessage();message.obj = 0;handler.sendMessage(message);}}.start();}else{Toast.makeText(MainActivity.this, "設置失敗,請重新選擇", Toast.LENGTH_SHORT).show();}}@Overridepublic void onProgress(Integer value) {// 返回的上傳進度(百分比)}}); }下載圖片
new AvatarLoader(userViewHolder.head_icon, user).load();這里需要兩個封裝類
public class AvatarLoader extends ImageLoader{private User mUser;public AvatarLoader(ImageView imageView, User user) {super(imageView);mUser = user;}public void load(){BmobQuery<User> q=new BmobQuery<>();q.getObject(mUser.getObjectId(), new QueryListener<User>() {@Overridepublic void done(User user, BmobException e) {if(user.getImageFile()!=null) {setUrl(user.getImageFile().getUrl());AvatarLoader.super.load();}}});} } public class ImageLoader extends Handler {private ImageView mImageView;private String mUrl;private Bitmap mBitmap;public ImageLoader(ImageView imageView, String url) {mImageView = imageView;mUrl = url;}public ImageLoader(ImageView imageView) {mImageView = imageView;}public void setUrl(String url) {mUrl = url;}@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);if (msg.what == 0) {new Thread(){@Overridepublic void run() {super.run();try{URL url=new URL(mUrl);URLConnection connection=url.openConnection();connection.connect();InputStream inputStream=connection.getInputStream();mBitmap= BitmapFactory.decodeStream(inputStream);sendEmptyMessage(1);} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} catch (NullPointerException e){e.printStackTrace();}}}.start();}else {if(mImageView.getMeasuredWidth()==0) {final ViewTreeObserver vto = mImageView.getViewTreeObserver();vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {@Overridepublic void onGlobalLayout() {vto.removeOnGlobalLayoutListener(this);adjustViewHeight();}});}else{adjustViewHeight();}}}public void load(){sendEmptyMessage(0);}private void adjustViewHeight(){ViewGroup.LayoutParams params=mImageView.getLayoutParams();float ratio = (float) mBitmap.getHeight() / mBitmap.getWidth();params.height = (int) (mImageView.getMeasuredWidth() * ratio);params.width=mImageView.getMeasuredWidth();mImageView.setImageBitmap(mBitmap);}}由于拍攝的圖片一般較大所以進程需要壓縮
/* 壓縮路徑下的文件*/ private File imageFactory(String picPath){BitmapFactory.Options o=new BitmapFactory.Options();Bitmap bitmap=BitmapFactory.decodeFile(picPath, o);bitmap=Bitmap.createScaledBitmap(bitmap, 400, 400, false);File root= getExternalCacheDir();File pic=new File(root,"test.jpg");try {FileOutputStream fos=new FileOutputStream(pic);bitmap.compress(Bitmap.CompressFormat.JPEG,50,fos);} catch (FileNotFoundException e) {e.printStackTrace();}return pic; }使用到這些內(nèi)容的項目地址:
點擊右邊跳轉:點擊跳轉 https://github.com/FishInWater-1999/android-SignInSystem
如果有幫助歡迎點贊哦~~
有問題請在評論區(qū)留言
總結
以上是生活随笔為你收集整理的Bmob的使用-上传图片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 教女友用 trello 管理衣物和穿搭L
- 下一篇: C语言实现最大字段和(动态规划法和分治法