码迷,mamicode.com
首页 > 移动开发 > 详细

Android图片上传,可以选择多张图片,缩放预览,拍照上传等

时间:2015-04-18 19:03:01      阅读:321      评论:0      收藏:0      [点我收藏+]

标签:

仿照微信,朋友圈分享图片功能 。可以进行图片的多张选择,拍照添加图片,以及进行图片的预览,预览时可以进行缩放,并且可以删除选中状态的图片 。很不错的源码,大家有需要可以下载看看 。

技术分享

微信

技术分享

微信

技术分享

微信

技术分享

微信

下载地址 : 微信上传图片源码

//部分代码如下

 

[java] view plaincopy技术分享技术分享
 
  1. package com.king.photo.activity;  
  2.    
  3. import android.annotation.SuppressLint;  
  4. import android.app.Activity;  
  5. import android.content.Context;  
  6. import android.content.Intent;  
  7. import android.graphics.Bitmap;  
  8. import android.graphics.BitmapFactory;  
  9. import android.graphics.Color;  
  10. import android.graphics.drawable.BitmapDrawable;  
  11. import android.graphics.drawable.ColorDrawable;  
  12. import android.os.Bundle;  
  13. import android.os.Handler;  
  14. import android.os.Message;  
  15. import android.provider.MediaStore;  
  16. import android.util.Log;  
  17. import android.view.Gravity;  
  18. import android.view.KeyEvent;  
  19. import android.view.LayoutInflater;  
  20. import android.view.View;  
  21. import android.view.View.OnClickListener;  
  22. import android.view.ViewGroup;  
  23. import android.view.ViewGroup.LayoutParams;  
  24. import android.view.animation.AnimationUtils;  
  25. import android.widget.AdapterView;  
  26. import android.widget.AdapterView.OnItemClickListener;  
  27. import android.widget.BaseAdapter;  
  28. import android.widget.Button;  
  29. import android.widget.GridView;  
  30. import android.widget.ImageView;  
  31. import android.widget.LinearLayout;  
  32. import android.widget.PopupWindow;  
  33. import android.widget.RelativeLayout;  
  34.    
  35. import com.king.photo.R;  
  36. import com.king.photo.util.Bimp;  
  37. import com.king.photo.util.FileUtils;  
  38. import com.king.photo.util.ImageItem;  
  39. import com.king.photo.util.PublicWay;  
  40. import com.king.photo.util.Res;  
  41.    
  42.    
  43.    
  44.  //首页面activity  
  45.    
  46. public class MainActivity extends Activity {  
  47.    
  48.     private GridView noScrollgridview;  
  49.     private GridAdapter adapter;  
  50.     private View parentView;  
  51.     private PopupWindow pop = null;  
  52.     private LinearLayout ll_popup;  
  53.     public static Bitmap bimap ;  
  54.       
  55.     protected void onCreate(Bundle savedInstanceState) {  
  56.         super.onCreate(savedInstanceState);  
  57.         Res.init(this);  
  58.         bimap = BitmapFactory.decodeResource(  
  59.                 getResources(),  
  60.                 R.drawable.icon_addpic_unfocused);  
  61.         PublicWay.activityList.add(this);  
  62.         parentView = getLayoutInflater().inflate(R.layout.activity_selectimg, null);  
  63.         setContentView(parentView);  
  64.         Init();  
  65.     }  
  66.    
  67.     public void Init() {  
  68.           
  69.         pop = new PopupWindow(MainActivity.this);  
  70.           
  71.         View view = getLayoutInflater().inflate(R.layout.item_popupwindows, null);  
  72.    
  73.         ll_popup = (LinearLayout) view.findViewById(R.id.ll_popup);  
  74.           
  75.         pop.setWidth(LayoutParams.MATCH_PARENT);  
  76.         pop.setHeight(LayoutParams.WRAP_CONTENT);  
  77.         pop.setBackgroundDrawable(new BitmapDrawable());  
  78.         pop.setFocusable(true);  
  79.         pop.setOutsideTouchable(true);  
  80.         pop.setContentView(view);  
  81.           
  82.         RelativeLayout parent = (RelativeLayout) view.findViewById(R.id.parent);  
  83.         Button bt1 = (Button) view  
  84.                 .findViewById(R.id.item_popupwindows_camera);  
  85.         Button bt2 = (Button) view  
  86.                 .findViewById(R.id.item_popupwindows_Photo);  
  87.         Button bt3 = (Button) view  
  88.                 .findViewById(R.id.item_popupwindows_cancel);  
  89.         parent.setOnClickListener(new OnClickListener() {  
  90.               
  91.             @Override  
  92.             public void onClick(View v) {  
  93.                 // TODO Auto-generated method stub  
  94.                 pop.dismiss();  
  95.                 ll_popup.clearAnimation();  
  96.             }  
  97.         });  
  98.         bt1.setOnClickListener(new OnClickListener() {  
  99.             public void onClick(View v) {  
  100.                 photo();  
  101.                 pop.dismiss();  
  102.                 ll_popup.clearAnimation();  
  103.             }  
  104.         });  
  105.         bt2.setOnClickListener(new OnClickListener() {  
  106.             public void onClick(View v) {  
  107.                 Intent intent = new Intent(MainActivity.this,  
  108.                         AlbumActivity.class);  
  109.                 startActivity(intent);  
  110.                 overridePendingTransition(R.anim.activity_translate_in, R.anim.activity_translate_out);  
  111.                 pop.dismiss();  
  112.                 ll_popup.clearAnimation();  
  113.             }  
  114.         });  
  115.         bt3.setOnClickListener(new OnClickListener() {  
  116.             public void onClick(View v) {  
  117.                 pop.dismiss();  
  118.                 ll_popup.clearAnimation();  
  119.             }  
  120.         });  
  121.           
  122.         noScrollgridview = (GridView) findViewById(R.id.noScrollgridview);    
  123.         noScrollgridview.setSelector(new ColorDrawable(Color.TRANSPARENT));  
  124.         adapter = new GridAdapter(this);  
  125.         adapter.update();  
  126.         noScrollgridview.setAdapter(adapter);  
  127.         noScrollgridview.setOnItemClickListener(new OnItemClickListener() {  
  128.    
  129.             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,  
  130.                     long arg3) {  
  131.                 if (arg2 == Bimp.tempSelectBitmap.size()) {  
  132.                     Log.i("ddddddd", "----------");  
  133.                     ll_popup.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.activity_translate_in));  
  134.                     pop.showAtLocation(parentView, Gravity.BOTTOM, 0, 0);  
  135.                 } else {  
  136.                     Intent intent = new Intent(MainActivity.this,  
  137.                             GalleryActivity.class);  
  138.                     intent.putExtra("position", "1");  
  139.                     intent.putExtra("ID", arg2);  
  140.                     startActivity(intent);  
  141.                 }  
  142.             }  
  143.         });  
  144.    
  145.     }  
  146.    
  147.     @SuppressLint("HandlerLeak")  
  148.     public class GridAdapter extends BaseAdapter {  
  149.         private LayoutInflater inflater;  
  150.         private int selectedPosition = -1;  
  151.         private boolean shape;  
  152.    
  153.         public boolean isShape() {  
  154.             return shape;  
  155.         }  
  156.    
  157.         public void setShape(boolean shape) {  
  158.             this.shape = shape;  
  159.         }  
  160.    
  161.         public GridAdapter(Context context) {  
  162.             inflater = LayoutInflater.from(context);  
  163.         }  
  164.    
  165.         public void update() {  
  166.             loading();  
  167.         }  
  168.    
  169.         public int getCount() {  
  170.             if(Bimp.tempSelectBitmap.size() == 9){  
  171.                 return 9;  
  172.             }  
  173.             return (Bimp.tempSelectBitmap.size() + 1);  
  174.         }  
  175.    
  176.         public Object getItem(int arg0) {  
  177.             return null;  
  178.         }  
  179.    
  180.         public long getItemId(int arg0) {  
  181.             return 0;  
  182.         }  
  183.    
  184.         public void setSelectedPosition(int position) {  
  185.             selectedPosition = position;  
  186.         }  
  187.    
  188.         public int getSelectedPosition() {  
  189.             return selectedPosition;  
  190.         }  
  191.    
  192.         public View getView(int position, View convertView, ViewGroup parent) {  
  193.             ViewHolder holder = null;  
  194.             if (convertView == null) {  
  195.                 convertView = inflater.inflate(R.layout.item_published_grida,  
  196.                         parent, false);  
  197.                 holder = new ViewHolder();  
  198.                 holder.image = (ImageView) convertView  
  199.                         .findViewById(R.id.item_grida_image);  
  200.                 convertView.setTag(holder);  
  201.             } else {  
  202.                 holder = (ViewHolder) convertView.getTag();  
  203.             }  
  204.    
  205.             if (position ==Bimp.tempSelectBitmap.size()) {  
  206.                 holder.image.setImageBitmap(BitmapFactory.decodeResource(  
  207.                         getResources(), R.drawable.icon_addpic_unfocused));  
  208.                 if (position == 9) {  
  209.                     holder.image.setVisibility(View.GONE);  
  210.                 }  
  211.             } else {  
  212.                 holder.image.setImageBitmap(Bimp.tempSelectBitmap.get(position).getBitmap());  
  213.             }  
  214.    
  215.             return convertView;  
  216.         }  
  217.    
  218.         public class ViewHolder {  
  219.             public ImageView image;  
  220.         }  
  221.    
  222.         Handler handler = new Handler() {  
  223.             public void handleMessage(Message msg) {  
  224.                 switch (msg.what) {  
  225.                 case 1:  
  226.                     adapter.notifyDataSetChanged();  
  227.                     break;  
  228.                 }  
  229.                 super.handleMessage(msg);  
  230.             }  
  231.         };  
  232.    
  233.         public void loading() {  
  234.             new Thread(new Runnable() {  
  235.                 public void run() {  
  236.                     while (true) {  
  237.                         if (Bimp.max == Bimp.tempSelectBitmap.size()) {  
  238.                             Message message = new Message();  
  239.                             message.what = 1;  
  240.                             handler.sendMessage(message);  
  241.                             break;  
  242.                         } else {  
  243.                             Bimp.max += 1;  
  244.                             Message message = new Message();  
  245.                             message.what = 1;  
  246.                             handler.sendMessage(message);  
  247.                         }  
  248.                     }  
  249.                 }  
  250.             }).start();  
  251.         }  
  252.     }  
  253.    
  254.     public String getString(String s) {  
  255.         String path = null;  
  256.         if (s == null)  
  257.             return "";  
  258.         for (int i = s.length() - 1; i > 0; i++) {  
  259.             s.charAt(i);  
  260.         }  
  261.         return path;  
  262.     }  
  263.    
  264.     protected void onRestart() {  
  265.         adapter.update();  
  266.         super.onRestart();  
  267.     }  
  268.    
  269.     private static final int TAKE_PICTURE = 0x000001;  
  270.    
  271.     public void photo() {  
  272.         Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
  273.         startActivityForResult(openCameraIntent, TAKE_PICTURE);  
  274.     }  
  275.    
  276.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  277.         switch (requestCode) {  
  278.         case TAKE_PICTURE:  
  279.             if (Bimp.tempSelectBitmap.size() < 9 && resultCode == RESULT_OK) {  
  280.                   
  281.                 String fileName = String.valueOf(System.currentTimeMillis());  
  282.                 Bitmap bm = (Bitmap) data.getExtras().get("data");  
  283.                 FileUtils.saveBitmap(bm, fileName);  
  284.                   
  285.                 ImageItem takePhoto = new ImageItem();  
  286.                 takePhoto.setBitmap(bm);  
  287.                 Bimp.tempSelectBitmap.add(takePhoto);  
  288.             }  
  289.             break;  
  290.         }  
  291.     }  
  292.       
  293.     public boolean onKeyDown(int keyCode, KeyEvent event) {  
  294.         if (keyCode == KeyEvent.KEYCODE_BACK) {  
  295.             for(int i=0;i<PublicWay.activityList.size();i++){  
  296.                 if (null != PublicWay.activityList.get(i)) {  
  297.                     PublicWay.activityList.get(i).finish();  
  298.                 }  
  299.             }  
  300.             System.exit(0);  
  301.         }  
  302.         return true;  
  303.     }  
  304.    
  305. }  
  306.    
  307.    

 

下载地址 : 微信上传图片源码

备份下载地址:http://download.csdn.net/detail/jdsjlzx/8486449

 

补充:

通过GridView仿微信动态添加本地图片

 

此篇文章主要讲述GridView控件实现添加本地图片并显示.主要是关于GridView控件的基本操作,通常可以通过自定义继承BaseAdapter的适配器加载图片,而下面讲述的不是自定义的适配器,而是调用SimpleAdapter实现的.至于上传发布与网络交互此处不讲述,后面文章会讲!

一. 实现效果

    主要是通过点击+从本地相册中添加图片,同时显示图片至GridView.点击图片可以进行删除操作,同时界面中的发布EditView控件也很好看,不足之处在于+好没有移动至最后,但原理相同.
  技术分享  技术分享  技术分享

二. 项目工程结构

 
技术分享
 

三. 界面布局详细代码

    1.主界面activity_main.xml
    主要通过相对布局实现,第一部分是底部的TextView,中间是EditView和GridView相对布局,下面是两个按钮.同时EditView调用res/drawable-hdpi中的editview_shape.xml,GridView显示的每张图片通过griditem_addpic.xml实现.

[html] view plaincopy技术分享技术分享
 
 
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:id="@+id/container"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     tools:context="com.example.suishoupaipublish.MainActivity"  
  7.     tools:ignore="MergeRootFrame" >  
  8.       
  9.     <!-- 顶部添加文字 -->   
  10.     <RelativeLayout    
  11.         android:id="@+id/Layout_top"    
  12.         android:orientation="horizontal"     
  13.         android:layout_width="fill_parent"    
  14.         android:layout_height="40dp"    
  15.         android:layout_marginTop="5dp"  
  16.         android:layout_alignParentTop="true"    
  17.         android:gravity="center">    
  18.         <TextView    
  19.             android:layout_width="fill_parent"     
  20.             android:layout_height="wrap_content"      
  21.             android:textSize="25sp"  
  22.             android:gravity="center"  
  23.             android:text="发布信息" />   
  24.     </RelativeLayout>    
  25.     <!-- 底部按钮 -->    
  26.     <RelativeLayout    
  27.         android:id="@+id/Layout_bottom"     
  28.         android:layout_alignParentBottom="true"  
  29.         android:layout_width="fill_parent"     
  30.         android:layout_height="50dp"  
  31.         android:gravity="center" >    
  32.         <Button    
  33.             android:id="@+id/button1"    
  34.             android:layout_width="wrap_content"    
  35.             android:layout_height="fill_parent"  
  36.             android:textSize="20sp"  
  37.             android:text="发布拍拍" />    
  38.         <Button    
  39.             android:id="@+id/button2"    
  40.             android:layout_width="wrap_content"    
  41.             android:layout_height="fill_parent"  
  42.             android:layout_toRightOf="@+id/button1"  
  43.             android:textSize="20sp"  
  44.             android:text="取消发布" />  
  45.     </RelativeLayout>    
  46.     <!-- 显示图片 -->    
  47.     <RelativeLayout    
  48.         android:id="@+id/Content_Layout"       
  49.         android:layout_width="fill_parent"     
  50.         android:layout_height="fill_parent"    
  51.         android:layout_above="@id/Layout_bottom"     
  52.         android:layout_below="@id/Layout_top"      
  53.         android:gravity="center">       
  54.         <LinearLayout     
  55.             android:layout_width="match_parent"    
  56.             android:layout_height="match_parent"    
  57.             android:orientation="vertical"  
  58.             android:layout_alignParentBottom="true" >   
  59.             <!-- 设置运行多行 设置圆角图形 黑色字体-->  
  60.             <EditText   
  61.                 android:id="@+id/editText1"  
  62.                 android:layout_height="120dp"  
  63.                 android:layout_width="fill_parent"  
  64.                 android:textColor="#000000"  
  65.                 android:layout_margin="12dp"  
  66.                 android:textSize="20sp"  
  67.                 android:hint="随手说出你此刻的心声..."  
  68.                 android:maxLength="500"  
  69.                 android:singleLine="false"  
  70.                 android:background="@drawable/editview_shape" />  
  71.             <!-- 网格显示图片 行列间距5dp 每列宽度90dp -->  
  72.             <GridView  
  73.                 android:id="@+id/gridView1"  
  74.                 android:layout_width="fill_parent"  
  75.                 android:layout_height="200dp"  
  76.                 android:layout_margin="10dp"  
  77.                 android:background="#EFDFDF"  
  78.                 android:horizontalSpacing="5dp"  
  79.                 android:verticalSpacing="5dp"  
  80.                 android:numColumns="4"  
  81.                 android:columnWidth="90dp"  
  82.                 android:stretchMode="columnWidth"  
  83.                 android:gravity="center" >  
  84.             </GridView>  
  85.             <TextView   
  86.                 android:layout_width="fill_parent"  
  87.                 android:layout_height="wrap_content"  
  88.                 android:text="(友情提示:只能添加9张图片,长按图片可以删除已添加图片)"  
  89.                 android:gravity="center" />  
  90.         </LinearLayout>  
  91.     </RelativeLayout>  
  92.       
  93. </RelativeLayout>  
    2.显示ImageView图片布局griditem_addpic.xml
[html] view plaincopy技术分享技术分享
 
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:gravity="center"  
  6.     android:descendantFocusability="blocksDescendants"  
  7.     android:orientation="vertical" >  
  8.     <RelativeLayout  
  9.         android:layout_gravity="center"  
  10.         android:layout_width="80dp"  
  11.         android:layout_height="80dp"  
  12.         android:orientation="vertical" >  
  13.         <ImageView  
  14.             android:layout_marginTop="10dp"  
  15.             android:layout_marginRight="10dp"  
  16.             android:id="@+id/imageView1"  
  17.             android:layout_width="fill_parent"  
  18.             android:layout_height="fill_parent"  
  19.             android:scaleType="fitXY"  
  20.             android:src="@drawable/gridview_addpic" />  
  21.     </RelativeLayout>  
  22. </LinearLayout>  
    3.设置EditView控件圆角和颜色 editview_shape.xml
[html] view plaincopy技术分享技术分享
 
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"   
  3.     android:shape="rectangle"   
  4.     android:padding="10dp">  
  5.     <!-- 填充editView的颜色 -->  
  6.     <soild android:color="#ffffff"/>  
  7.     <!-- 设置圆角的弧度,radius半径越大,editView的边角越圆 -->  
  8.     <corners   
  9.         android:radius="15dp"  
  10.         android:bottomRightRadius="15dp"  
  11.         android:bottomLeftRadius="15dp"  
  12.         android:topLeftRadius="15dp"    
  13.         android:topRightRadius="15dp"/>  
  14.     <stroke  
  15.         android:color="#32CD32"    
  16.         android:width="4px" />   
  17. </shape>  

四. 代码详解

 
    它主要是思想如下:
    1.通过SimpleAdapter适配器实现实现加载图片,在gridView1.setOnItemClickListener()点击函数中响应不同操作.
    2.当点击加号图片(+)时,调用本地相册通过Intent实现获取图片路径存于字符串pathImage.
    3.获取图片路径后在onResume中刷新图片,通过GridView的setAdapter()和notifyDataSetChanged()()函数刷新加载图片.
    4.点击图片时会获取其position,通过dialog()函数弹出对话框提示是否删除,通过remove实现删除.
    具体代码如下所示:
[java] view plaincopy技术分享技术分享
 
 
  1. public class MainActivity extends Activity {  
  2.   
  3.     private GridView gridView1;              //网格显示缩略图  
  4.     private Button buttonPublish;            //发布按钮  
  5.     private final int IMAGE_OPEN = 1;        //打开图片标记  
  6.     private String pathImage;                //选择图片路径  
  7.     private Bitmap bmp;                      //导入临时图片  
  8.     private ArrayList<HashMap<String, Object>> imageItem;  
  9.     private SimpleAdapter simpleAdapter;     //适配器  
  10.       
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.activity_main);  
  15.         /* 
  16.          * 防止键盘挡住输入框 
  17.          * 不希望遮挡设置activity属性 android:windowSoftInputMode="adjustPan" 
  18.          * 希望动态调整高度 android:windowSoftInputMode="adjustResize" 
  19.          */  
  20.         getWindow().setSoftInputMode(WindowManager.LayoutParams.  
  21.                 SOFT_INPUT_ADJUST_PAN);  
  22.         //锁定屏幕  
  23.         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);  
  24.         setContentView(R.layout.activity_main);  
  25.         //获取控件对象  
  26.         gridView1 = (GridView) findViewById(R.id.gridView1);  
  27.   
  28.         /* 
  29.          * 载入默认图片添加图片加号 
  30.          * 通过适配器实现 
  31.          * SimpleAdapter参数imageItem为数据源 R.layout.griditem_addpic为布局 
  32.          */  
  33.         //获取资源图片加号  
  34.         bmp = BitmapFactory.decodeResource(getResources(), R.drawable.gridview_addpic);  
  35.         imageItem = new ArrayList<HashMap<String, Object>>();  
  36.         HashMap<String, Object> map = new HashMap<String, Object>();  
  37.         map.put("itemImage", bmp);  
  38.         imageItem.add(map);  
  39.         simpleAdapter = new SimpleAdapter(this,   
  40.                 imageItem, R.layout.griditem_addpic,   
  41.                 new String[] { "itemImage"}, new int[] { R.id.imageView1});   
  42.         /* 
  43.          * HashMap载入bmp图片在GridView中不显示,但是如果载入资源ID能显示 如 
  44.          * map.put("itemImage", R.drawable.img); 
  45.          * 解决方法: 
  46.          *              1.自定义继承BaseAdapter实现 
  47.          *              2.ViewBinder()接口实现 
  48.          *  参考 http://blog.csdn.net/admin_/article/details/7257901 
  49.          */  
  50.         simpleAdapter.setViewBinder(new ViewBinder() {    
  51.             @Override    
  52.             public boolean setViewValue(View view, Object data,    
  53.                     String textRepresentation) {    
  54.                 // TODO Auto-generated method stub    
  55.                 if(view instanceof ImageView && data instanceof Bitmap){    
  56.                     ImageView i = (ImageView)view;    
  57.                     i.setImageBitmap((Bitmap) data);    
  58.                     return true;    
  59.                 }    
  60.                 return false;    
  61.             }  
  62.         });    
  63.         gridView1.setAdapter(simpleAdapter);  
  64.           
  65.         /* 
  66.          * 监听GridView点击事件 
  67.          * 报错:该函数必须抽象方法 故需要手动导入import android.view.View; 
  68.          */  
  69.         gridView1.setOnItemClickListener(new OnItemClickListener() {  
  70.             @Override  
  71.             public void onItemClick(AdapterView<?> parent, View v, int position, long id)  
  72.             {  
  73.                 if( imageItem.size() == 10) { //第一张为默认图片  
  74.                     Toast.makeText(MainActivity.this, "图片数9张已满", Toast.LENGTH_SHORT).show();  
  75.                 }  
  76.                 else if(position == 0) { //点击图片位置为+ 0对应0张图片  
  77.                     Toast.makeText(MainActivity.this, "添加图片", Toast.LENGTH_SHORT).show();  
  78.                     //选择图片  
  79.                     Intent intent = new Intent(Intent.ACTION_PICK,         
  80.                             android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);    
  81.                     startActivityForResult(intent, IMAGE_OPEN);    
  82.                     //通过onResume()刷新数据  
  83.                 }  
  84.                 else {  
  85.                     dialog(position);  
  86.                     //Toast.makeText(MainActivity.this, "点击第"+(position + 1)+" 号图片",   
  87.                     //      Toast.LENGTH_SHORT).show();  
  88.                 }  
  89.             }  
  90.         });    
  91.     }  
  92.       
  93.     //获取图片路径 响应startActivityForResult    
  94.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {    
  95.         super.onActivityResult(requestCode, resultCode, data);          
  96.         //打开图片    
  97.         if(resultCode==RESULT_OK && requestCode==IMAGE_OPEN) {          
  98.             Uri uri = data.getData();    
  99.             if (!TextUtils.isEmpty(uri.getAuthority())) {    
  100.                 //查询选择图片    
  101.                 Cursor cursor = getContentResolver().query(    
  102.                         uri,    
  103.                         new String[] { MediaStore.Images.Media.DATA },    
  104.                         null,     
  105.                         null,     
  106.                         null);    
  107.                 //返回 没找到选择图片    
  108.                 if (null == cursor) {    
  109.                     return;    
  110.                 }    
  111.                 //光标移动至开头 获取图片路径    
  112.                 cursor.moveToFirst();    
  113.                 pathImage = cursor.getString(cursor    
  114.                         .getColumnIndex(MediaStore.Images.Media.DATA));    
  115.             }  
  116.         }  //end if 打开图片  
  117.     }  
  118.       
  119.     //刷新图片  
  120.     @Override  
  121.     protected void onResume() {  
  122.         super.onResume();  
  123.         if(!TextUtils.isEmpty(pathImage)){  
  124.             Bitmap addbmp=BitmapFactory.decodeFile(pathImage);  
  125.             HashMap<String, Object> map = new HashMap<String, Object>();  
  126.             map.put("itemImage", addbmp);  
  127.             imageItem.add(map);  
  128.             simpleAdapter = new SimpleAdapter(this,   
  129.                     imageItem, R.layout.griditem_addpic,   
  130.                     new String[] { "itemImage"}, new int[] { R.id.imageView1});   
  131.             simpleAdapter.setViewBinder(new ViewBinder() {    
  132.                 @Override    
  133.                 public boolean setViewValue(View view, Object data,    
  134.                         String textRepresentation) {    
  135.                     // TODO Auto-generated method stub    
  136.                     if(view instanceof ImageView && data instanceof Bitmap){    
  137.                         ImageView i = (ImageView)view;    
  138.                         i.setImageBitmap((Bitmap) data);    
  139.                         return true;    
  140.                     }    
  141.                     return false;    
  142.                 }  
  143.             });   
  144.             gridView1.setAdapter(simpleAdapter);  
  145.             simpleAdapter.notifyDataSetChanged();  
  146.             //刷新后释放防止手机休眠后自动添加  
  147.             pathImage = null;  
  148.         }  
  149.     }  
  150.       
  151.     /* 
  152.      * Dialog对话框提示用户删除操作 
  153.      * position为删除图片位置 
  154.      */  
  155.     protected void dialog(final int position) {  
  156.         AlertDialog.Builder builder = new Builder(MainActivity.this);  
  157.         builder.setMessage("确认移除已添加图片吗?");  
  158.         builder.setTitle("提示");  
  159.         builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {  
  160.             @Override  
  161.             public void onClick(DialogInterface dialog, int which) {  
  162.                 dialog.dismiss();  
  163.                 imageItem.remove(position);  
  164.                 simpleAdapter.notifyDataSetChanged();  
  165.             }  
  166.         });  
  167.         builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {  
  168.             @Override  
  169.             public void onClick(DialogInterface dialog, int which) {  
  170.                 dialog.dismiss();  
  171.                 }  
  172.             });  
  173.         builder.create().show();  
  174.     }  
  175.   
  176. }  
    同时需要在AndroidMainfest.xml中添加权限操作SD卡和网络上传至服务器.
[html] view plaincopy技术分享技术分享
 
 
  1. <!-- 申明网络权限  -->  
  2. <uses-permission android:name="android.permission.INTERNET" />  
  3. <!-- 申明权限 操作SD卡 -->    
  4. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  

五. 总结

    
    该文章需要注意一个地方:在使用SimpleAdapter适配器加载bmp图片时,可能在GridView中不显示.即HashMap中map.put("itemImage",bmp)不显示图片,而使用put装入R.drawable.img却能显示.
    这时有两种解决方法,一种是自定义继承BaseAdapter的适配器实现;另一种方法则是如上所示通过ViewBinder()接口实现,感谢博主dmin_提供的方法.
    下载地址:http://download.csdn.net/detail/eastmount/8237429

Android图片上传,可以选择多张图片,缩放预览,拍照上传等

标签:

原文地址:http://www.cnblogs.com/duanxz/p/4437800.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!