<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                轉載請標明源地址:[http://blog.csdn.net/gaolei1201/article/details/50392478](http://blog.csdn.net/gaolei1201/article/details/50392478) 以前項目中做過上傳頭像和仿QQ空間、微博發表文字和圖片,也是花費了好多精力和時間多。現在花了幾天時間做了整理與大家分享一下。下面把幾個重要點列一下: 1.下面會把Demo源碼奉獻給大家,用的是以前公司的接口,首先需要登錄才能更新頭像和發表圖文 2.我在項目中用的是OKHttp框架來實現網絡請求,詳情可參考:[Android OkHttp完全解析](http://blog.csdn.net/lmj623565791/article/details/47911083),展現圖片用的是UniversalImageLoader 3.展示本地圖片時一定要壓縮,不然圖片大的話一個幾M,容易出現OOM 4.上傳圖片時也要壓縮,不然圖片大的話耗費時間太長,以及別人瀏覽圖片時太耗流量 5.在獲取上傳圖片時,剛開始是肯定獲取不到的,因為一張圖片上傳需要幾秒鐘,為了較好的用戶體驗可以先展示本地圖片,上傳成功后下次再展示網絡圖片;另一種方法就是把所有圖片都上傳完成,然后再獲取發表的內容 如圖: ![](https://box.kancloud.cn/2016-02-29_56d3fcdb05e43.jpg) ![](https://box.kancloud.cn/2016-02-29_56d3fcdb1fdcd.jpg) ![](https://box.kancloud.cn/2016-02-29_56d3fcdb3095d.jpg) # 下面是網絡請求封裝類,別的類用接口回調可以獲取請求結果 ~~~ <span style="font-size:14px;">public class NetRequest { private MyInterface.NetRequestIterface netRequestIterface; private Context context; public NetRequest(MyInterface.NetRequestIterface netRequestIterface, Context context) { this.netRequestIterface = netRequestIterface; this.context = context; } /** * 網絡請求用的是OKHttp,這個開源項目的好處是1.Android 6.0后不支持HttpClient請求,而它使用HttpUrlConnection 2.默認支持https */ public void httpRequest(Map<String, Object> map, final String requestUrl) { if (!CommonUtils.getUtilInstance().isConnectingToInternet(context)) { Toast.makeText(context, context.getString(R.string.internet_fail_connect),Toast.LENGTH_LONG).show(); return; } OkHttpClient mOkHttpClient = new OkHttpClient(); FormEncodingBuilder builder = new FormEncodingBuilder(); if (null != map && !map.isEmpty()) for (String key : map.keySet()) { builder.add(key, map.get(key)+""); } if(UserInfoUtil.getInstance().getAuthKey()!=null) { builder.add("authKey", UserInfoUtil.getInstance().getAuthKey()); } Log.d("gaolei", " authKey------------------"+UserInfoUtil.getInstance().getAuthKey()); Request request = new Request.Builder() .url(requestUrl) .post(builder.build()) .build(); try { mOkHttpClient.setConnectTimeout(5000, TimeUnit.MILLISECONDS); mOkHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { netRequestIterface.exception(e, requestUrl); } @Override public void onResponse(final Response response) throws IOException { String result = response.body().string(); netRequestIterface.changeView(result, requestUrl); } }); }catch (Exception e){ } } } </span> ~~~ 下面是壓縮圖片 ~~~ <span style="font-size:14px;"> //注意顯示本地圖片時一定要壓縮質量,不然容易出現OOM public Bitmap trasformToZoomPhotoAndLessMemory(String url) { File file = new File(url); Log.d("gaolei", "file.length()--------original-------------" + file.length()); BitmapFactory.Options options = new BitmapFactory.Options(); // 通過這個bitmap獲取圖片的寬和高 options.inJustDecodeBounds = true; int inSampleSize = 1; if (file.length() < 256 * 1024) { options.inPreferredConfig = Bitmap.Config.ARGB_8888; } else if (file.length() < 512 * 1024) { options.inPreferredConfig = Bitmap.Config.RGB_565; options.inSampleSize = 2; inSampleSize = 2; } else if (file.length() < 1024 * 1024) { options.inPreferredConfig = Bitmap.Config.RGB_565; options.inSampleSize = 4; inSampleSize = 4; } else { options.inPreferredConfig = Bitmap.Config.RGB_565; options.inSampleSize = 6; inSampleSize = 6; } options.inPurgeable = true; options.inInputShareable = true; // 注意這次要把options.inJustDecodeBounds 設為 false,這次圖片是要讀取出來的 options.inJustDecodeBounds = false; Log.d("gaolei", "inSampleSize-----------------" + inSampleSize); int degree = readPictureDegree(file.getAbsolutePath()); // Log.d("gaolei", "degree------------uploadImg--------------" + // degree); InputStream is = null; try { is = new FileInputStream(url); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } Bitmap cameraBitmap = BitmapFactory.decodeStream(is, null, options); // Bitmap cameraBitmap = BitmapFactory.decodeFile(url, options); Bitmap photo = rotaingImageView(degree, cameraBitmap); if (is != null) { try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return photo; }</span> ~~~ 下面是把圖片byte轉化為字符串上傳到服務器 ~~~ <span style="font-size:14px;"> //請注意:上傳圖片時 我是把圖片byte轉化為字符串 上傳到服務器,當然還可以用1.stream上傳到服務器 2.Socket上傳到服務器,不過我沒試過 public void uploadUserPhotoNew(final String filePath) { uploading_photo_progress.setVisibility(View.VISIBLE); //為什么另開一個線程呢?因為要把圖片字節流轉化為字符串上傳,比較耗時,阻塞UI線程,會使應用卡卡卡,所以要另開一線程 new Thread() { public void run() { String fileType = UploadPhotoUtil.getInstance().getFileType(filePath); String fileString = UploadPhotoUtil.getInstance().getUploadPhotoZoomString( filePath); Map<String, Object> map = new HashMap<String, Object>(); map.put("imgType", fileType); map.put("imgBody", fileString); Message msg = handler.obtainMessage(); msg.obj = map; msg.what = SAVE_PHOTO_IMAGE; handler.sendMessage(msg); } }.start(); }</span> ~~~ 下面是展示發表圖文ListView的Adapter,這里比較重要,因為圖片上傳需要一段時間,馬上獲取發表的圖文時,肯定顯示不出來圖片,現在先顯示本地的,等下次進入時顯示網絡的 ~~~ <span style="font-size:14px;">public class ThemeListViewAdapter extends BaseAdapter { private LayoutInflater inflater; private List<ThemeObject> list; private Context context; private List<String> uploadImgUrlList; public ThemeListViewAdapter(List<ThemeObject> list, List<String> uploadImgUrlList, Context context) { inflater = LayoutInflater.from(context); this.list = list; this.uploadImgUrlList = uploadImgUrlList; this.context = context; } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return list.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } public void changeList(List<ThemeObject> list) { this.list = list; } @Override public View getView(final int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); convertView = inflater.inflate(R.layout.theme_listview_items, null); holder.user_photo = (ImageView) convertView .findViewById(R.id.user_photo); holder.theme_img1 = (ImageView) convertView .findViewById(R.id.theme_img1); holder.theme_img2 = (ImageView) convertView .findViewById(R.id.theme_img2); holder.theme_img3 = (ImageView) convertView .findViewById(R.id.theme_img3); holder.theme_title = (TextView) convertView .findViewById(R.id.theme_title); holder.theme_desc = (TextView) convertView .findViewById(R.id.theme_desc); holder.user_name = (TextView) convertView .findViewById(R.id.user_name); holder.publish_theme_time = (TextView) convertView .findViewById(R.id.publish_theme_time); holder.theme_reply_num = (TextView) convertView .findViewById(R.id.theme_reply_num); holder.theme_praise_num = (TextView) convertView .findViewById(R.id.theme_praise_num); holder.game_playing_prefix = (TextView) convertView .findViewById(R.id.game_playing_prefix); holder.game_playing = (TextView) convertView .findViewById(R.id.game_playing); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.user_photo.setTag(position + ""); holder.theme_img1.setTag(position + ""); holder.theme_img2.setTag(position + ""); holder.theme_img3.setTag(position + ""); holder.game_playing_prefix.setTag(position + ""); holder.game_playing.setTag(position + ""); if (list.size() > 0) { final ThemeObject object = list.get(position); if (holder.user_photo.getTag().equals("" + position)) { if (object.getUserPhoto() != null) { new CommonUtils().displayCircleImage(object.getUserPhoto(), holder.user_photo, "photo"); } else { holder.user_photo.setImageDrawable(context.getResources() .getDrawable(R.drawable.personal_default_photo)); } List<PictureList> themePictureList = object.getPictureList(); int pictureListSize = themePictureList.size(); List<String> pictureUrlList = new ArrayList<String>(); for (int i = 0; i < themePictureList.size(); i++) { pictureUrlList.add(themePictureList.get(i).getUrl()); } Log.d("gaolei", "themePictureList.size()------------" + position + "-------" + themePictureList.size()); int uploadImgUrlListSize = uploadImgUrlList.size(); Log.d("gaolei", "uploadImgUrlListSize------------" + position + "-------" + uploadImgUrlListSize); if (pictureListSize == 0) { holder.theme_img1.setVisibility(View.GONE); holder.theme_img2.setVisibility(View.GONE); holder.theme_img3.setVisibility(View.GONE); } if (pictureListSize == 1) { holder.theme_img1.setVisibility(View.VISIBLE); if (holder.theme_img1.getTag().equals("" + position)) { CommonUtils.getUtilInstance().displayLowQualityInImage(pictureUrlList.get(0), holder.theme_img1); } } if (pictureListSize == 2) { holder.theme_img1.setVisibility(View.VISIBLE); holder.theme_img2.setVisibility(View.VISIBLE); if (holder.theme_img1.getTag().equals("" + position)) { CommonUtils.getUtilInstance().displayLowQualityInImage(pictureUrlList.get(0), holder.theme_img1); } if (holder.theme_img2.getTag().equals("" + position)) { CommonUtils.getUtilInstance().displayLowQualityInImage(pictureUrlList.get(1), holder.theme_img2); } } if (pictureListSize == 3) { holder.theme_img1.setVisibility(View.VISIBLE); holder.theme_img2.setVisibility(View.VISIBLE); holder.theme_img3.setVisibility(View.VISIBLE); if (holder.theme_img1.getTag().equals("" + position)) { CommonUtils.getUtilInstance().displayLowQualityInImage(pictureUrlList.get(0), holder.theme_img1); } if (holder.theme_img2.getTag().equals("" + position)) { CommonUtils.getUtilInstance().displayLowQualityInImage(pictureUrlList.get(1), holder.theme_img2); } if (holder.theme_img3.getTag().equals("" + position)) { CommonUtils.getUtilInstance().displayLowQualityInImage(pictureUrlList.get(2), holder.theme_img3); } } //這里比較重要,因為圖片上傳需要一段時間,馬上獲取發表的圖文時,肯定顯示不出來圖片,現在先顯示本地的,等下次進入時顯示網絡的 if (position == 0) { if (uploadImgUrlListSize == 1) { holder.theme_img1.setVisibility(View.VISIBLE); if (holder.theme_img1.getTag().equals("" + position)) { Bitmap bitmap = UploadPhotoUtil.getInstance().trasformToZoomBitmapAndLessMemory(uploadImgUrlList.get(0)); holder.theme_img1.setImageDrawable(new BitmapDrawable(context.getResources(), bitmap)); } } if (uploadImgUrlListSize == 2) { holder.theme_img1.setVisibility(View.VISIBLE); holder.theme_img2.setVisibility(View.VISIBLE); if (holder.theme_img1.getTag().equals("" + position)) { Bitmap bitmap = UploadPhotoUtil.getInstance().trasformToZoomBitmapAndLessMemory(uploadImgUrlList.get(0)); holder.theme_img1.setImageDrawable(new BitmapDrawable(context.getResources(), bitmap)); } if (holder.theme_img2.getTag().equals("" + position)) { Bitmap bitmap = UploadPhotoUtil.getInstance().trasformToZoomBitmapAndLessMemory(uploadImgUrlList.get(1)); holder.theme_img2.setImageDrawable(new BitmapDrawable(context.getResources(), bitmap)); } } if (uploadImgUrlListSize == 3) { holder.theme_img1.setVisibility(View.VISIBLE); holder.theme_img2.setVisibility(View.VISIBLE); holder.theme_img3.setVisibility(View.VISIBLE); if (holder.theme_img1.getTag().equals("" + position)) { Bitmap bitmap = UploadPhotoUtil.getInstance().trasformToZoomBitmapAndLessMemory(uploadImgUrlList.get(0)); holder.theme_img1.setImageDrawable(new BitmapDrawable(context.getResources(), bitmap)); } if (holder.theme_img2.getTag().equals("" + position)) { Bitmap bitmap = UploadPhotoUtil.getInstance().trasformToZoomBitmapAndLessMemory(uploadImgUrlList.get(1)); holder.theme_img2.setImageDrawable(new BitmapDrawable(context.getResources(), bitmap)); } if (holder.theme_img3.getTag().equals("" + position)) { Bitmap bitmap = UploadPhotoUtil.getInstance().trasformToZoomBitmapAndLessMemory(uploadImgUrlList.get(2)); holder.theme_img3.setImageDrawable(new BitmapDrawable(context.getResources(), bitmap)); } } } holder.user_name.setText(object.getUserName()); holder.theme_title.setText(object.getThemeTitle()); holder.theme_desc.setText(object.getThemeDescr()); holder.theme_reply_num.setText(object.getReplyNum() + ""); holder.publish_theme_time.setText(CommonUtils.getUtilInstance() .transformMillisToDate(object.getCreateTime())); holder.theme_praise_num.setText(object.getPraiseNum() + ""); } holder.user_photo.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub } }); } return convertView; } class ViewHolder { ImageView user_photo, theme_img1, theme_img2, theme_img3; TextView user_name, theme_title, theme_desc, publish_theme_time, theme_reply_num, theme_praise_num, game_playing, game_playing_prefix; } }</span> ~~~ 另外的開源項目地址:[http://www.itlanbao.com/code/20150810/10044/100222.html](http://www.itlanbao.com/code/20150810/10044/100222.html)?,效果如下圖: ![](https://box.kancloud.cn/2016-02-29_56d3fcdb62a6a.jpg) [源碼下載地址,歡迎光臨......](https://github.com/gaoleiandroid1201/UploadPhoto)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看