国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android實(shí)現(xiàn)圖片上傳功能

2019-12-12 03:47:52
字體:
供稿:網(wǎng)友

最近在開發(fā)中,涉及到用戶的意見反饋功能這一方面的開發(fā),需要用戶輸入的文字或者提交的圖片,效果大概類似于微信朋友圈那樣的圖片選擇器,一開始自己找了個(gè)用universal-image-loader框架寫的,很容實(shí)現(xiàn),但是容易出現(xiàn)內(nèi)存溢出,并且不好解決,是在沒辦法,就自己看了一些資料,準(zhǔn)備自己寫;在這里說下本人實(shí)現(xiàn)的思路,進(jìn)入頁面也就是顯示選擇圖片的頁面用GridView來實(shí)現(xiàn),點(diǎn)擊添加圖標(biāo)的時(shí)候,用Dialog實(shí)現(xiàn),給Dialog添加相應(yīng)的動(dòng)畫就可以了,進(jìn)入圖片展示頁面還是用GridView來實(shí)現(xiàn),點(diǎn)擊所有圖片時(shí)用的是Dialog和listview來實(shí)現(xiàn)的,以下是相應(yīng)的代碼實(shí)現(xiàn):

private void showDialog() {  View view = getLayoutInflater().inflate(R.layout.user_header_dialog, null);  final Dialog dialog = new Dialog(this, R.style.transparentFrameWindowStyle);  dialog.setContentView(view, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));  Window window = dialog.getWindow();  // 設(shè)置顯示動(dòng)畫  window.setWindowAnimations(R.style.main_menu_animstyle);  WindowManager.LayoutParams wl = window.getAttributes();  wl.x = 0;  wl.y = getWindowManager().getDefaultDisplay().getHeight();  // 以下這兩句是為了保證按鈕可以水平滿屏  wl.width = ViewGroup.LayoutParams.MATCH_PARENT;  wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;  // 設(shè)置顯示位置  dialog.onWindowAttributesChanged(wl);  // 設(shè)置點(diǎn)擊外圍解散  dialog.setCanceledOnTouchOutside(true);  dialog.show();  btn_picture = (Button) window.findViewById(R.id.btn_picture);  btn_photo = (Button) window.findViewById(R.id.btn_photo);  btn_cancle = (Button) window.findViewById(R.id.btn_cancle);  btn_picture.setOnClickListener(new View.OnClickListener() {// 圖庫     @SuppressLint("InlinedApi")     @Override     public void onClick(View v) {      Intent intent = new Intent(PhotoSelectActivity.this, AlbumActivity.class);      startActivity(intent);      dialog.dismiss();     }    });  btn_photo.setOnClickListener(new View.OnClickListener() {// 相機(jī)     @SuppressLint("InlinedApi")     @Override     public void onClick(View v) {      photo();      dialog.dismiss();     }    });  btn_cancle.setOnClickListener(new View.OnClickListener() {// 取消     @Override     public void onClick(View v) {      dialog.dismiss();     }    }); }

這是彈框部分的代碼,在這里需要注意的就是android6.0系統(tǒng)調(diào)用的時(shí)候特別是相機(jī)和訪問sd權(quán)限的問題,跟android6.0以下的系統(tǒng)是不一樣的,android6.0以下的系統(tǒng)在AndroidManifest.xml文件中配置就可以了,android6.0及6.0以上的話不僅需要再AndroidManifest.xml中聲明還需要?jiǎng)討B(tài)申請(qǐng)權(quán)限,如未申請(qǐng)權(quán)限就會(huì)造成程序的閃退,這里的話沒有對(duì)android6.0及6.0以上做適配,關(guān)于android6.0及6.0以上系統(tǒng)權(quán)限的話,會(huì)在之后博文中提到;

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  switch (requestCode) {  case TAKE_PICTURE:   if (Bimp.tempSelectBitmap.size() < 9 && resultCode == RESULT_OK) {    File file = new File(Environment.getExternalStorageDirectory() + "/" + mImageFileName);    mImagePath = file.getPath();    Bitmap bitmapFromUrl = FileUtils.getBitmapFromUrl(mImagePath, 320, 480);    String[] split = mImagePath.split("0/");    String strUrl = "";    if (split != null && split.length > 0) {     strUrl = split[1];    }    // 重新緩存圖片    FileUtils.setPicToView(PhotoSelectActivity.this,bitmapFromUrl, strUrl);    // 獲取重新緩存圖片的大小    File iconDir = FileUtils.getIconDir(PhotoSelectActivity.this);    String absolutePath = iconDir.getAbsolutePath();    String picPath = absolutePath + strUrl;    ImageItem takePhoto = new ImageItem();    takePhoto.setBitmap(bitmapFromUrl);    takePhoto.setImagePath(picPath);    Bimp.tempSelectBitmap.add(takePhoto);   }   break;  } }

這里是調(diào)用相機(jī)拍照返回時(shí)調(diào)用這里,獲取到圖片同時(shí)對(duì)圖片進(jìn)行壓縮處理,同時(shí)緩存在sd中,并獲取相應(yīng)的路徑;

/**  * 清空?qǐng)D片集合  */ private void cleanImageList() {  Bimp.max = 0;  Bimp.tempSelectBitmap.clear(); }

在點(diǎn)擊返回或者物理物理返回鍵的的時(shí)候要對(duì)定義的靜態(tài)變量賦值為0,同時(shí)清空?qǐng)D片保存時(shí)定義的靜態(tài)list集合;

private void initPow() {  View view = getLayoutInflater().inflate(R.layout.listview_popupwindows, null);  final Dialog dialog = new Dialog(this, R.style.Dialog_Fullscreen);  dialog.setContentView(view, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));  Window window = dialog.getWindow();  // 設(shè)置顯示動(dòng)畫  window.setWindowAnimations(R.style.main_menu_animstyle);  WindowManager.LayoutParams wl = window.getAttributes();  wl.x = 0;  wl.y = getWindowManager().getDefaultDisplay().getHeight();  int height = 0;  int h=(int) (mScreenHeight / 1.6);  int listH=AlbumActivity.contentList.size()*DensityUtil.dip2px(AlbumActivity.this,80);  if (listH==0) {   height=h;  }else{   if (listH>h) {    height=h;   }else{    height=listH;   }  }  // 以下這兩句是為了保證按鈕可以水平滿屏  wl.width = ViewGroup.LayoutParams.MATCH_PARENT;  wl.height = height;  // 設(shè)置顯示位置  dialog.onWindowAttributesChanged(wl);  // 設(shè)置點(diǎn)擊外圍解散  dialog.setCanceledOnTouchOutside(true);  dialog.show();  ListView listview = (ListView) window.findViewById(R.id.listview);  ListAdapter listAdapter = new ListAdapter(AlbumActivity.this);  listview.setAdapter(listAdapter);  listview.setOnItemClickListener(new OnItemClickListener() {   @Override   public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {    dataList = (ArrayList<ImageItem>) AlbumActivity.contentList.get(arg2).imageList;    String folderName = AlbumActivity.contentList.get(arg2).bucketName;    tv_all.setText("" + folderName);    gridImageAdapter = new AlbumGridViewAdapter(AlbumActivity.this, dataList, Bimp.tempSelectBitmap);    agridView.setAdapter(gridImageAdapter);    dialog.dismiss();   }  }); }

這里的話是在圖片選擇展示頁面,點(diǎn)擊所有圖片時(shí)的彈框,用的是一個(gè)Dialog和listview來實(shí)現(xiàn)的,在這里要注意的是就是listview展示的高度問題,這里所限獲取到所有l(wèi)istview條目高度和,同時(shí)獲取到屏幕的高度,如果listview條目高度和大于屏幕高度/1.6時(shí),就采用屏幕高度/1.6,如果listview條目高度和小于屏幕高度/1.6時(shí),就采用listview條目高度;這樣就差不多實(shí)現(xiàn)了,下面是運(yùn)行效果:

源碼:Androidphoto

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 百色市| 文登市| 荥经县| 崇文区| 祁门县| 保山市| 中卫市| 云南省| 阿克| 九台市| 景德镇市| 九江市| 苏尼特右旗| 怀集县| 盐源县| 习水县| 建阳市| 丰原市| 松江区| 天镇县| 嘉祥县| 平南县| 古浪县| 泰安市| 澄迈县| 兴海县| 阳高县| 镇巴县| 福海县| 皮山县| 绥棱县| 沧州市| 布尔津县| 麻阳| 三明市| 紫云| 阿克苏市| 鹿泉市| 伊金霍洛旗| 荥阳市| 昌黎县|