欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2017年02月07日 14:23:49   作者:wangwo1991  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圖片上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

 }

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

源碼:Androidphoto

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論