仿照微信,朋友圈分享圖片功能 。能夠進行圖片的多張選擇,拍照添加圖片,以及進行圖片的預覽,預覽時能夠進行縮放,而且能夠刪除選中狀態的圖片 。很不錯的源碼,你們有須要能夠下載看看 。html
微信java
微信android
微信windows
微信服務器
下載地址 : 微信上傳圖片源碼微信
不少網友不知道怎麼獲取圖片路徑,這裏貼出來:網絡
String path = Bimp.tempSelectBitmap.get(position).getImagePath();app
//部分代碼以下ide
- package com.king.photo.activity;
-
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Color;
- import android.graphics.drawable.BitmapDrawable;
- import android.graphics.drawable.ColorDrawable;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.provider.MediaStore;
- import android.util.Log;
- import android.view.Gravity;
- import android.view.KeyEvent;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup;
- import android.view.ViewGroup.LayoutParams;
- import android.view.animation.AnimationUtils;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.BaseAdapter;
- import android.widget.Button;
- import android.widget.GridView;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.PopupWindow;
- import android.widget.RelativeLayout;
-
- import com.king.photo.R;
- import com.king.photo.util.Bimp;
- import com.king.photo.util.FileUtils;
- import com.king.photo.util.ImageItem;
- import com.king.photo.util.PublicWay;
- import com.king.photo.util.Res;
-
-
-
-
-
- public class MainActivity extends Activity {
-
- private GridView noScrollgridview;
- private GridAdapter adapter;
- private View parentView;
- private PopupWindow pop = null;
- private LinearLayout ll_popup;
- public static Bitmap bimap ;
-
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- Res.init(this);
- bimap = BitmapFactory.decodeResource(
- getResources(),
- R.drawable.icon_addpic_unfocused);
- PublicWay.activityList.add(this);
- parentView = getLayoutInflater().inflate(R.layout.activity_selectimg, null);
- setContentView(parentView);
- Init();
- }
-
- public void Init() {
-
- pop = new PopupWindow(MainActivity.this);
-
- View view = getLayoutInflater().inflate(R.layout.item_popupwindows, null);
-
- ll_popup = (LinearLayout) view.findViewById(R.id.ll_popup);
-
- pop.setWidth(LayoutParams.MATCH_PARENT);
- pop.setHeight(LayoutParams.WRAP_CONTENT);
- pop.setBackgroundDrawable(new BitmapDrawable());
- pop.setFocusable(true);
- pop.setOutsideTouchable(true);
- pop.setContentView(view);
-
- RelativeLayout parent = (RelativeLayout) view.findViewById(R.id.parent);
- Button bt1 = (Button) view
- .findViewById(R.id.item_popupwindows_camera);
- Button bt2 = (Button) view
- .findViewById(R.id.item_popupwindows_Photo);
- Button bt3 = (Button) view
- .findViewById(R.id.item_popupwindows_cancel);
- parent.setOnClickListener(new OnClickListener() {
-
- @Override
- public void onClick(View v) {
-
- pop.dismiss();
- ll_popup.clearAnimation();
- }
- });
- bt1.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- photo();
- pop.dismiss();
- ll_popup.clearAnimation();
- }
- });
- bt2.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- Intent intent = new Intent(MainActivity.this,
- AlbumActivity.class);
- startActivity(intent);
- overridePendingTransition(R.anim.activity_translate_in, R.anim.activity_translate_out);
- pop.dismiss();
- ll_popup.clearAnimation();
- }
- });
- bt3.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- pop.dismiss();
- ll_popup.clearAnimation();
- }
- });
-
- noScrollgridview = (GridView) findViewById(R.id.noScrollgridview);
- noScrollgridview.setSelector(new ColorDrawable(Color.TRANSPARENT));
- adapter = new GridAdapter(this);
- adapter.update();
- noScrollgridview.setAdapter(adapter);
- noScrollgridview.setOnItemClickListener(new OnItemClickListener() {
-
- public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
- long arg3) {
- if (arg2 == Bimp.tempSelectBitmap.size()) {
- Log.i("ddddddd", "----------");
- ll_popup.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.activity_translate_in));
- pop.showAtLocation(parentView, Gravity.BOTTOM, 0, 0);
- } else {
- Intent intent = new Intent(MainActivity.this,
- GalleryActivity.class);
- intent.putExtra("position", "1");
- intent.putExtra("ID", arg2);
- startActivity(intent);
- }
- }
- });
-
- }
-
- @SuppressLint("HandlerLeak")
- public class GridAdapter extends BaseAdapter {
- private LayoutInflater inflater;
- private int selectedPosition = -1;
- private boolean shape;
-
- public boolean isShape() {
- return shape;
- }
-
- public void setShape(boolean shape) {
- this.shape = shape;
- }
-
- public GridAdapter(Context context) {
- inflater = LayoutInflater.from(context);
- }
-
- public void update() {
- loading();
- }
-
- public int getCount() {
- if(Bimp.tempSelectBitmap.size() == 9){
- return 9;
- }
- return (Bimp.tempSelectBitmap.size() + 1);
- }
-
- public Object getItem(int arg0) {
- return null;
- }
-
- public long getItemId(int arg0) {
- return 0;
- }
-
- public void setSelectedPosition(int position) {
- selectedPosition = position;
- }
-
- public int getSelectedPosition() {
- return selectedPosition;
- }
-
- public View getView(int position, View convertView, ViewGroup parent) {
- ViewHolder holder = null;
- if (convertView == null) {
- convertView = inflater.inflate(R.layout.item_published_grida,
- parent, false);
- holder = new ViewHolder();
- holder.image = (ImageView) convertView
- .findViewById(R.id.item_grida_image);
- convertView.setTag(holder);
- } else {
- holder = (ViewHolder) convertView.getTag();
- }
-
- if (position ==Bimp.tempSelectBitmap.size()) {
- holder.image.setImageBitmap(BitmapFactory.decodeResource(
- getResources(), R.drawable.icon_addpic_unfocused));
- if (position == 9) {
- holder.image.setVisibility(View.GONE);
- }
- } else {
- holder.image.setImageBitmap(Bimp.tempSelectBitmap.get(position).getBitmap());
- }
-
- return convertView;
- }
-
- public class ViewHolder {
- public ImageView image;
- }
-
- Handler handler = new Handler() {
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case 1:
- adapter.notifyDataSetChanged();
- break;
- }
- super.handleMessage(msg);
- }
- };
-
- public void loading() {
- new Thread(new Runnable() {
- public void run() {
- while (true) {
- if (Bimp.max == Bimp.tempSelectBitmap.size()) {
- Message message = new Message();
- message.what = 1;
- handler.sendMessage(message);
- break;
- } else {
- Bimp.max += 1;
- Message message = new Message();
- message.what = 1;
- handler.sendMessage(message);
- }
- }
- }
- }).start();
- }
- }
-
- public String getString(String s) {
- String path = null;
- if (s == null)
- return "";
- for (int i = s.length() - 1; i > 0; i++) {
- s.charAt(i);
- }
- return path;
- }
-
- protected void onRestart() {
- adapter.update();
- super.onRestart();
- }
-
- private static final int TAKE_PICTURE = 0x000001;
-
- public void photo() {
- Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- startActivityForResult(openCameraIntent, TAKE_PICTURE);
- }
-
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- switch (requestCode) {
- case TAKE_PICTURE:
- if (Bimp.tempSelectBitmap.size() < 9 && resultCode == RESULT_OK) {
-
- String fileName = String.valueOf(System.currentTimeMillis());
- Bitmap bm = (Bitmap) data.getExtras().get("data");
- FileUtils.saveBitmap(bm, fileName);
-
- ImageItem takePhoto = new ImageItem();
- takePhoto.setBitmap(bm);
- Bimp.tempSelectBitmap.add(takePhoto);
- }
- break;
- }
- }
-
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_BACK) {
- for(int i=0;i<PublicWay.activityList.size();i++){
- if (null != PublicWay.activityList.get(i)) {
- PublicWay.activityList.get(i).finish();
- }
- }
- System.exit(0);
- }
- return true;
- }
-
- }
-
-
下載地址 : 微信上傳圖片源碼函數
備份下載地址: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實現.
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/container"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.example.suishoupaipublish.MainActivity"
- tools:ignore="MergeRootFrame" >
-
-
- <RelativeLayout
- android:id="@+id/Layout_top"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="40dp"
- android:layout_marginTop="5dp"
- android:layout_alignParentTop="true"
- android:gravity="center">
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textSize="25sp"
- android:gravity="center"
- android:text="發佈信息" />
- </RelativeLayout>
-
- <RelativeLayout
- android:id="@+id/Layout_bottom"
- android:layout_alignParentBottom="true"
- android:layout_width="fill_parent"
- android:layout_height="50dp"
- android:gravity="center" >
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:textSize="20sp"
- android:text="發佈拍拍" />
- <Button
- android:id="@+id/button2"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_toRightOf="@+id/button1"
- android:textSize="20sp"
- android:text="取消發佈" />
- </RelativeLayout>
-
- <RelativeLayout
- android:id="@+id/Content_Layout"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_above="@id/Layout_bottom"
- android:layout_below="@id/Layout_top"
- android:gravity="center">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- android:layout_alignParentBottom="true" >
-
- <EditText
- android:id="@+id/editText1"
- android:layout_height="120dp"
- android:layout_width="fill_parent"
- android:textColor="#000000"
- android:layout_margin="12dp"
- android:textSize="20sp"
- android:hint="隨手說出你此刻的心聲..."
- android:maxLength="500"
- android:singleLine="false"
- android:background="@drawable/editview_shape" />
-
- <GridView
- android:id="@+id/gridView1"
- android:layout_width="fill_parent"
- android:layout_height="200dp"
- android:layout_margin="10dp"
- android:background="#EFDFDF"
- android:horizontalSpacing="5dp"
- android:verticalSpacing="5dp"
- android:numColumns="4"
- android:columnWidth="90dp"
- android:stretchMode="columnWidth"
- android:gravity="center" >
- </GridView>
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="(友情提示:只能添加9張圖片,長按圖片能夠刪除已添加圖片)"
- android:gravity="center" />
- </LinearLayout>
- </RelativeLayout>
-
- </RelativeLayout>
2.顯示ImageView圖片佈局griditem_addpic.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:gravity="center"
- android:descendantFocusability="blocksDescendants"
- android:orientation="vertical" >
- <RelativeLayout
- android:layout_gravity="center"
- android:layout_width="80dp"
- android:layout_height="80dp"
- android:orientation="vertical" >
- <ImageView
- android:layout_marginTop="10dp"
- android:layout_marginRight="10dp"
- android:id="@+id/imageView1"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:scaleType="fitXY"
- android:src="@drawable/gridview_addpic" />
- </RelativeLayout>
- </LinearLayout>
3.設置EditView控件圓角和顏色 editview_shape.xml
- <?xml version="1.0" encoding="utf-8"?>
- <shape xmlns:android="http://schemas.android.com/apk/res/android"
- android:shape="rectangle"
- android:padding="10dp">
-
- <soild android:color="#ffffff"/>
-
- <corners
- android:radius="15dp"
- android:bottomRightRadius="15dp"
- android:bottomLeftRadius="15dp"
- android:topLeftRadius="15dp"
- android:topRightRadius="15dp"/>
- <stroke
- android:color="#32CD32"
- android:width="4px" />
- </shape>
四. 代碼詳解
它主要是思想以下:
1.經過SimpleAdapter適配器實現實現加載圖片,在gridView1.setOnItemClickListener()點擊函數中響應不一樣操做.
2.當點擊加號圖片(+)時,調用本地相冊經過Intent實現獲取圖片路徑存於字符串pathImage.
3.獲取圖片路徑後在onResume中刷新圖片,經過GridView的setAdapter()和notifyDataSetChanged()()函數刷新加載圖片.
4.點擊圖片時會獲取其position,經過dialog()函數彈出對話框提示是否刪除,經過remove實現刪除.
具體代碼以下所示:
- public class MainActivity extends Activity {
-
- private GridView gridView1;
- private Button buttonPublish;
- private final int IMAGE_OPEN = 1;
- private String pathImage;
- private Bitmap bmp;
- private ArrayList<HashMap<String, Object>> imageItem;
- private SimpleAdapter simpleAdapter;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- getWindow().setSoftInputMode(WindowManager.LayoutParams.
- SOFT_INPUT_ADJUST_PAN);
-
- setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
- setContentView(R.layout.activity_main);
-
- gridView1 = (GridView) findViewById(R.id.gridView1);
-
-
-
- bmp = BitmapFactory.decodeResource(getResources(), R.drawable.gridview_addpic);
- imageItem = new ArrayList<HashMap<String, Object>>();
- HashMap<String, Object> map = new HashMap<String, Object>();
- map.put("itemImage", bmp);
- imageItem.add(map);
- simpleAdapter = new SimpleAdapter(this,
- imageItem, R.layout.griditem_addpic,
- new String[] { "itemImage"}, new int[] { R.id.imageView1});
-
- simpleAdapter.setViewBinder(new ViewBinder() {
- @Override
- public boolean setViewValue(View view, Object data,
- String textRepresentation) {
-
- if(view instanceof ImageView && data instanceof Bitmap){
- ImageView i = (ImageView)view;
- i.setImageBitmap((Bitmap) data);
- return true;
- }
- return false;
- }
- });
- gridView1.setAdapter(simpleAdapter);
-
-
- gridView1.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View v, int position, long id)
- {
- if( imageItem.size() == 10) {
- Toast.makeText(MainActivity.this, "圖片數9張已滿", Toast.LENGTH_SHORT).show();
- }
- else if(position == 0) {
- Toast.makeText(MainActivity.this, "添加圖片", Toast.LENGTH_SHORT).show();
-
- Intent intent = new Intent(Intent.ACTION_PICK,
- android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
- startActivityForResult(intent, IMAGE_OPEN);
-
- }
- else {
- dialog(position);
-
-
- }
- }
- });
- }
-
-
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
-
- if(resultCode==RESULT_OK && requestCode==IMAGE_OPEN) {
- Uri uri = data.getData();
- if (!TextUtils.isEmpty(uri.getAuthority())) {
-
- Cursor cursor = getContentResolver().query(
- uri,
- new String[] { MediaStore.Images.Media.DATA },
- null,
- null,
- null);
-
- if (null == cursor) {
- return;
- }
-
- cursor.moveToFirst();
- pathImage = cursor.getString(cursor
- .getColumnIndex(MediaStore.Images.Media.DATA));
- }
- }
- }
-
-
- @Override
- protected void onResume() {
- super.onResume();
- if(!TextUtils.isEmpty(pathImage)){
- Bitmap addbmp=BitmapFactory.decodeFile(pathImage);
- HashMap<String, Object> map = new HashMap<String, Object>();
- map.put("itemImage", addbmp);
- imageItem.add(map);
- simpleAdapter = new SimpleAdapter(this,
- imageItem, R.layout.griditem_addpic,
- new String[] { "itemImage"}, new int[] { R.id.imageView1});
- simpleAdapter.setViewBinder(new ViewBinder() {
- @Override
- public boolean setViewValue(View view, Object data,
- String textRepresentation) {
-
- if(view instanceof ImageView && data instanceof Bitmap){
- ImageView i = (ImageView)view;
- i.setImageBitmap((Bitmap) data);
- return true;
- }
- return false;
- }
- });
- gridView1.setAdapter(simpleAdapter);
- simpleAdapter.notifyDataSetChanged();
-
- pathImage = null;
- }
- }
-
-
- protected void dialog(final int position) {
- AlertDialog.Builder builder = new Builder(MainActivity.this);
- builder.setMessage("確認移除已添加圖片嗎?");
- builder.setTitle("提示");
- builder.setPositiveButton("確認", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- imageItem.remove(position);
- simpleAdapter.notifyDataSetChanged();
- }
- });
- builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- }
- });
- builder.create().show();
- }
-
- }
同時須要在AndroidMainfest.xml中添加權限操做SD卡和網絡上傳至服務器.
- <uses-permission android:name="android.permission.INTERNET" />
- <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