最近做开发需要解决一个模仿微信朋友圈附加图片的功能,具体要求如下:
(1)从手机中最多选择3张图片,可拍照上传;
(2)选择的图片可以点击移除然后再添加新图片;
(3)可以在手机包含图片的各个文件夹中随意选择等。
本博客主要实现的是以上功能,其他诸如主界面布局、各种控件添加和提交功能等旨在说明问题,只做了简单处理,重点在图片选择添加部分。该功能的实现主要引用了一个图片加载的开源框架universal-image-loader。
其中截图如下:
为了不过于冗余,过滤了部分布局文件和资源文件,在这里提供源码下载地址,有兴趣的读者可以下载观看(注:我使用的 AndroID Studio 1.4,SdkVersion 为23 ,在小米note【AndroID 4.4 】上调试)。
下面开始我们的主要内容:
主界面布局activity_main.xml (主要使用一个GrIDVIEw实现):
<?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:orIEntation="vertical"> <relativeLayout androID:layout_wIDth="fill_parent" androID:background="#f7614d" androID:padding@R_419_6823@="37px" androID:layout_height="70dp"> <TextVIEw androID:layout_centerInParent="true" androID:text="附图案例" androID:textSize="25sp" androID:textcolor="#ffffff" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" /> </relativeLayout> <VIEw androID:layout_wIDth="fill_parent" androID:layout_height="20dp" androID:background="#00000000"/> <linearLayout androID:padding@R_419_6823@="10dp" androID:paddingRight="10dp" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:orIEntation="vertical"> <TextVIEw androID:ID="@+ID/tv11" androID:layout_alignParent@R_419_6823@="true" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="文字描述" androID:textSize="23sp" /> <VIEw androID:layout_wIDth="fill_parent" androID:layout_height="10dp" androID:background="#00000000"/> <EditText androID:paddingtop="20dp" androID:background="@drawable/bg_et" androID:ID="@+ID/editText" androID:inputType="textMultiline" androID:gravity="@R_419_6823@|top" androID:padding="5dp" androID:maxLength="300" androID:hint="客官,写点评论吧..." androID:textcolorHint="#808080" androID:textSize="20sp" androID:layout_wIDth="fill_parent" androID:layout_height="150dp" /> <VIEw androID:layout_wIDth="fill_parent" androID:layout_height="20dp" androID:background="#00000000"/> <TextVIEw androID:ID="@+ID/tv" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="附加图片" androID:textSize="23sp" /> <VIEw androID:layout_wIDth="fill_parent" androID:layout_height="10dp" androID:background="#00000000"/> <GrIDVIEw androID:ID="@+ID/grIDvIEw" androID:layout_wIDth="fill_parent" androID:layout_height="200dp" androID:horizontalSpacing="2dp" androID:verticalSpacing="5dp" androID:scrollbars="none" androID:ListSelector="#00000000" androID:numColumns="3" > </GrIDVIEw> <button androID:ID="@+ID/submit_btn" androID:layout_wIDth="wrap_content" androID:layout_height="50dp" androID:layout_centerInParent="true" androID:background="@drawable/bg_submit" androID:text="提交" androID:textSize="23sp" androID:textcolor="#FFFFFF"/> </linearLayout></linearLayout>
图片选择界面布局activity_select_picture.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:orIEntation="vertical" > <relativeLayout androID:layout_wIDth="match_parent" androID:layout_height="48dp" androID:background="#c000" > <button androID:ID="@+ID/btn_back" androID:layout_wIDth="24dp" androID:layout_height="24dp" androID:layout_alignParent@R_419_6823@="true" androID:layout_centerVertical="true" androID:layout_margin="8dp" androID:background="@drawable/ic_back_light" androID:onClick="back" /> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_centerVertical="true" androID:layout_margin@R_419_6823@="8dp" androID:layout_toRightOf="@+ID/btn_back" androID:text="图片" androID:textcolor="#ffffff" androID:textSize="16sp" /> <button androID:ID="@+ID/btn_ok" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParentRight="true" androID:layout_centerVertical="true" androID:layout_marginRight="8dp" androID:background="@null" androID:gravity="center" androID:onClick="ok" androID:text="完成" androID:textcolor="#fff" androID:textSize="14sp" /> </relativeLayout> <relativeLayout androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:background="#d000" > <GrIDVIEw androID:ID="@+ID/grIDvIEw" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:layout_above="@+ID/bottom" androID:horizontalSpacing="3dp" androID:numColumns="3" androID:scrollbars="none" androID:verticalSpacing="5dp" > </GrIDVIEw> <relativeLayout androID:ID="@+ID/bottom" androID:layout_wIDth="match_parent" androID:layout_height="48dp" androID:layout_alignParentBottom="true" androID:background="#c000" > <button androID:ID="@+ID/btn_select" androID:layout_wIDth="wrap_content" androID:layout_height="match_parent" androID:layout_alignParent@R_419_6823@="true" androID:layout_centerVertical="true" androID:layout_margin="8dp" androID:background="@drawable/spinner_bg" androID:onClick="select" androID:text="所有图片" androID:textcolor="#fff" androID:textSize="14sp" /> </relativeLayout> <ListVIEw androID:ID="@+ID/ListvIEw" androID:layout_wIDth="match_parent" androID:layout_height="360dp" androID:layout_above="@ID/bottom" androID:background="#fff" androID:visibility="gone" > </ListVIEw> </relativeLayout></linearLayout>
主界面Java代码MainActivity.java :
package com.custompictureselect;import androID.app.Activity;import androID.content.Context;import androID.content.Intent;import androID.graphics.BitmapFactory;import androID.os.Bundle;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.vIEw.WindowManager;import androID.Widget.BaseAdapter;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.GrIDVIEw;import androID.Widget.ImageVIEw;import com.nostra13.universalimageloader.cache.disc.naming.Md5filenameGenerator;import com.nostra13.universalimageloader.core.ImageLoader;import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;import com.nostra13.universalimageloader.core.assist.QueueProcessingType;import java.util.ArrayList;public class MainActivity extends Activity { private GrIDVIEw grIDvIEw; private EditText editText ; private GrIDAdapter grIDAdapter; private Context context ; private static final int REQUEST_PICK = 0; //存放所有选择的照片 private ArrayList<String> allSelectedPicture = new ArrayList<String>(); //存放从选择界面选择的照片 private ArrayList<String> selectedPicture = new ArrayList<String>(); @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); context = this ; //设置在activity启动的时候输入法默认是不开启的 getwindow().setSoftinputMode(WindowManager.LayoutParams.soFT_input_STATE_ALWAYS_HIDDEN); ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this) .threadPriority(Thread.norM_PRIORITY - 2)//设置当前线程的优先级 .denyCacheImageMultipleSizesInMemory() .diskCachefilenameGenerator(new Md5filenameGenerator())//使用MD5对UIL进行加密命名 .diskCacheSize(100 * 1024 * 1024)//50 Mb sd卡(本地)缓存的最大值 .diskCachefileCount(300)// 可以缓存的文件数量 .tasksProcessingOrder(QueueProcessingType.liFO)//后进先出 .build(); //初始化 *** 作 ImageLoader.getInstance().init(config); initVIEw(); } /** * 初始化视图 */ private voID initVIEw(){ editText = (EditText) findVIEwByID(R.ID.editText); //显示图片 grIDvIEw = (GrIDVIEw) findVIEwByID(R.ID.grIDvIEw); grIDAdapter = new GrIDAdapter(); grIDvIEw.setAdapter(grIDAdapter); } /** * 展示图片的GrIDVIEw的适配器 */ class GrIDAdapter extends BaseAdapter { public LayoutInflater layoutInflater = LayoutInflater.from(context); @OverrIDe public int getCount() { return allSelectedPicture.size()+ 1; } @OverrIDe public Object getItem(int position) { return position; } @OverrIDe public long getItemID(int position) { return position; } @OverrIDe public VIEw getVIEw(final int position,VIEw convertVIEw,VIEwGroup parent) { VIEwHolder holder = null ; if (convertVIEw == null) { holder = new VIEwHolder(); convertVIEw = layoutInflater.inflate(R.layout.childgrID_item,null); holder.image = (ImageVIEw) convertVIEw.findVIEwByID(R.ID.child_iv); holder.btn = (button) convertVIEw.findVIEwByID(R.ID.child_delete); holder.image.setScaleType(ImageVIEw.ScaleType.CENTER_CROP); convertVIEw.setTag(holder); } else{ holder = (VIEwHolder) convertVIEw.getTag(); } if (position == allSelectedPicture.size()) { holder.image.setimageBitmap(BitmapFactory.decodeResource( context.getResources(),R.drawable.icon_addpic)); holder.btn.setVisibility(VIEw.GONE); holder.image.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { selectClick(); } }); if (position == 3) { holder.image.setVisibility(VIEw.GONE); } } else { ImageLoader.getInstance().displayImage("file://" + allSelectedPicture.get(position),holder.image); holder.btn.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { //点击后移除图片 allSelectedPicture.remove(position); //更新UI grIDvIEw.setAdapter(grIDAdapter); } }); } return convertVIEw; } } public class VIEwHolder { public ImageVIEw image; public button btn ; } private voID selectClick(){ Intent intent = new Intent(); intent.setClass(MainActivity.this,SelectPictureActivity.class); Bundle bundle = new Bundle(); bundle.putStringArrayList("allSelectedPicture",allSelectedPicture); intent.putExtras(bundle); if(allSelectedPicture.size()< 3){ startActivityForResult(intent,REQUEST_PICK); } } @SuppressWarnings("unchecked") @OverrIDe protected voID onActivityResult(int requestCode,int resultCode,Intent data) { if (resultCode == RESulT_OK) { selectedPicture = (ArrayList) data.getSerializableExtra(SelectPictureActivity.INTENT_SELECTED_PICTURE); for(String str : selectedPicture){ if(!allSelectedPicture.contains(str)){ allSelectedPicture.add(str); grIDvIEw.setAdapter(grIDAdapter); } } } }}
选择图片界面Java代码SelectPictureActivity.java :
package com.custompictureselect;import androID.app.Activity;import androID.content.ContentResolver;import androID.content.Context;import androID.content.Intent;import androID.database.Cursor;import androID.graphics.Bitmap;import androID.net.Uri;import androID.os.Bundle;import androID.os.Environment;import androID.provIDer.MediaStore;import androID.util.Log;import androID.vIEw.VIEw;import androID.vIEw.VIEw.OnClickListener;import androID.vIEw.VIEwGroup;import androID.vIEw.Window;import androID.vIEw.animation.Animation;import androID.vIEw.animation.Animation.AnimationListener;import androID.vIEw.animation.TranslateAnimation;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.ListVIEw;import androID.Widget.TextVIEw;import androID.Widget.Toast;import com.nostra13.universalimageloader.core.displayImageOptions;import com.nostra13.universalimageloader.core.ImageLoader;import com.nostra13.universalimageloader.core.assist.ImageScaleType;import java.io.file;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.HashMap;import java.util.List;public class SelectPictureActivity extends Activity { // 最多选择图片的个数 private static int MAX_NUM = 3; private static final int TAKE_PICTURE = 520; public static final String INTENT_MAX_NUM = "intent_max_num"; public static final String INTENT_SELECTED_PICTURE = "intent_selected_picture"; private Context context; private GrIDVIEw grIDvIEw; private PictureAdapter adapter; /** * 临时的辅助类,用于防止同一个文件夹的多次扫描 */ private HashMap<String,Integer> tmpDir = new HashMap<String,Integer>(); private ArrayList<ImageFloder> mDirPaths = new ArrayList<ImageFloder>(); /** * 具体下载图片,缓存图片,显示图片的具体执行类,它有两个具体的方法displayImage(...)、loadImage(...), */ private ImageLoader loader; /** * 用于指导每一个Imageloader根据网络图片的状态(空白、下载错误、正在下载)显示对应的图片, * 是否将缓存加载到磁盘上,下载完后对图片进行怎么样的处理。 */ private displayImageOptions options; private ContentResolver mContentResolver; //选择按钮和完成按钮 private button btn_select,btn_ok; //图片文件的列表 private ListVIEw ListvIEw; private FolderAdapter folderAdapter; private ImageFloder imageAll,currentimageFolder; //已选择的图片 private ArrayList<String> selectedPicture = new ArrayList<String>(); private String cameraPath = null; private int isExisted ; private ArrayList<String> existedPictureList = new ArrayList<String>(); @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestwindowFeature(Window.FEATURE_NO_Title); setContentVIEw(R.layout.activity_select_picture); MAX_NUM = getIntent().getIntExtra(INTENT_MAX_NUM,3); Bundle bundle = getIntent().getExtras(); existedPictureList = bundle.getStringArrayList("allSelectedPicture"); isExisted = existedPictureList.size(); context = this; mContentResolver = getContentResolver(); loader = ImageLoader.getInstance(); options = new displayImageOptions.Builder() .showImageOnLoading(R.drawable.ic_launcher)//设置图片在下载期间显示的图片 .showImageForEmptyUri(R.drawable.ic_launcher)//设置图片Uri为空或是错误的时候显示的图片 .showImageOnFail(R.drawable.ic_launcher)//设置图片加载/解码过程中错误时候显示的图片 .cacheInMemory(true)//设置下载的图片是否缓存在内存中 .cacheOndisk(true) //设置下载的图片是否缓存在SD卡中 .consIDerExifParams(true)//启用EXIF和JPEG图像格式 .imageScaleType(ImageScaleType.EXACTLY)//图像将完全按比例缩小的目标大小 .bitmapConfig(Bitmap.Config.RGB_565)//设置图片的解码类型 .build(); initVIEw(); } /** * 点击"所有图片"按钮事件 * @param v */ public voID select(VIEw v) { if (ListvIEw.getVisibility() == VIEw.VISIBLE) { hIDeListAnimation(); } else { ListvIEw.setVisibility(VIEw.VISIBLE); showListAnimation(); folderAdapter.notifyDataSetChanged(); } } /** * 设置ListVIEw的移动的动画效果 */ public voID showListAnimation() { TranslateAnimation ta = new TranslateAnimation(1,0f,1,1f,0f); ta.setDuration(200); ListvIEw.startAnimation(ta); } /** * 隐藏ListVIEw的移动的动画效果 */ public voID hIDeListAnimation() { TranslateAnimation ta = new TranslateAnimation(1,1f); ta.setDuration(200); ListvIEw.startAnimation(ta); ta.setAnimationListener(new AnimationListener() { @OverrIDe public voID onAnimationStart(Animation animation) { } @OverrIDe public voID onAnimationRepeat(Animation animation) { } @OverrIDe public voID onAnimationEnd(Animation animation) { ListvIEw.setVisibility(VIEw.INVISIBLE); } }); } /** * 点击完成按钮 */ public voID ok(VIEw v) { Intent data = new Intent(); data.putExtra(INTENT_SELECTED_PICTURE,selectedPicture); setResult(RESulT_OK,data); this.finish(); } /** * 视图初始化 */ private voID initVIEw() { imageAll = new ImageFloder(); imageAll.setDir(getResources().getString(R.string.dir_all_pictures)); currentimageFolder = imageAll; mDirPaths.add(imageAll); btn_ok = (button) findVIEwByID(R.ID.btn_ok); btn_ok.setText(getResources().getString(R.string.complete) + isExisted + "/" + MAX_NUM); btn_select = (button) findVIEwByID(R.ID.btn_select); grIDvIEw = (GrIDVIEw) findVIEwByID(R.ID.grIDvIEw); adapter = new PictureAdapter(); grIDvIEw.setAdapter(adapter); grIDvIEw.setonItemClickListener(new OnItemClickListener() { @OverrIDe public voID onItemClick(AdapterVIEw<?> parent,VIEw vIEw,int position,long ID) { if (position == 0) { goCamare(); } } }); ListvIEw = (ListVIEw) findVIEwByID(R.ID.ListvIEw); folderAdapter = new FolderAdapter(); ListvIEw.setAdapter(folderAdapter); ListvIEw.setonItemClickListener(new OnItemClickListener() { @OverrIDe public voID onItemClick(AdapterVIEw<?> parent,long ID) { currentimageFolder = mDirPaths.get(position); hIDeListAnimation(); adapter.notifyDataSetChanged(); btn_select.setText(currentimageFolder.getname()); } }); getthumbnail(); } /** * 使用相机拍照 */ protected voID goCamare() { if (isExisted + selectedPicture.size() + 1 > MAX_NUM) { Toast.makeText(context,getResources().getString(R.string.chose_at_most) + MAX_NUM + getResources().getString(R.string.sheet),Toast.LENGTH_SHORT).show(); return; } Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); Uri imageUri = getoutputMediafileUri(); openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri); startActivityForResult(openCameraIntent,TAKE_PICTURE); } /** * 用于拍照时获取输出的Uri */ protected Uri getoutputMediafileUri() { file mediaStorageDir = new file( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"Night"); if (!mediaStorageDir.exists()) { if (!mediaStorageDir.mkdirs()) { Log.d("MyCameraApp","Failed to create directory"); return null; } } String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); file mediafile = new file(mediaStorageDir.getPath() + file.separator + "img_" + timeStamp + ".jpg"); cameraPath = mediafile.getabsolutePath(); return Uri.fromfile(mediafile); } @OverrIDe protected voID onActivityResult(int requestCode,Intent data) { if (resultCode == RESulT_OK && cameraPath != null) { selectedPicture.add(cameraPath); Intent data2 = new Intent(); data2.putExtra(INTENT_SELECTED_PICTURE,selectedPicture); setResult(RESulT_OK,data2); this.finish(); } } public voID back(VIEw v) { onBackpressed(); } class PictureAdapter extends BaseAdapter { @OverrIDe public int getCount() { return currentimageFolder.images.size() + 1; } @OverrIDe public Object getItem(int position) { return null; } @OverrIDe public long getItemID(int position) { return 0; } @OverrIDe public VIEw getVIEw(int position,VIEwGroup parent) { //自定义的一个类用来缓存convertvIEw VIEwHolder holder = null; if (convertVIEw == null) { convertVIEw = VIEw.inflate(context,R.layout.grID_item_picture,null); holder = new VIEwHolder(); holder.iv = (ImageVIEw) convertVIEw.findVIEwByID(R.ID.iv); holder.checkBox = (button) convertVIEw.findVIEwByID(R.ID.check); convertVIEw.setTag(holder); } else { holder = (VIEwHolder) convertVIEw.getTag(); } if (position == 0) {//拍摄图片 holder.iv.setimageResource(R.drawable.pickphotos_to_camera_normal); holder.checkBox.setVisibility(VIEw.INVISIBLE); } else { position = position - 1; holder.checkBox.setVisibility(VIEw.VISIBLE); final ImageItem item = currentimageFolder.images.get(position); //显示图片 loader.displayImage("file://" + item.path,holder.iv,options); //是否选中 boolean isSelected = (selectedPicture.contains(item.path) || existedPictureList.contains(item.path)); holder.checkBox.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) { //若选中的图片多于所设置的上限,不再加入 if (!v.isSelected() && isExisted + selectedPicture.size() + 1 > MAX_NUM) { Toast.makeText(context,Toast.LENGTH_SHORT).show(); return; } //二次选择,移除 if (selectedPicture.contains(item.path)|| existedPictureList.contains(item.path)) { selectedPicture.remove(item.path); } else { //加入数组 selectedPicture.add(item.path); } btn_ok.setEnabled(selectedPicture.size()>0); btn_ok.setText(getResources().getString(R.string.complete) + (selectedPicture.size()+ isExisted) + "/" + MAX_NUM); v.setSelected(selectedPicture.contains(item.path)|| existedPictureList.contains(item.path)); } }); holder.checkBox.setSelected(isSelected); } return convertVIEw; } } //自定义的一个类用来缓存convertvIEw class VIEwHolder { ImageVIEw iv; button checkBox; } /** * 所有图片 */ class FolderAdapter extends BaseAdapter { @OverrIDe public int getCount() { return mDirPaths.size(); } @OverrIDe public Object getItem(int position) { return null; } @OverrIDe public long getItemID(int position) { return 0; } @OverrIDe public VIEw getVIEw(int position,VIEwGroup parent) { FolderVIEwHolder holder = null; if (convertVIEw == null) { convertVIEw = VIEw.inflate(context,R.layout.List_dir_item,null); holder = new FolderVIEwHolder(); holder.ID_dir_item_image = (ImageVIEw) convertVIEw.findVIEwByID(R.ID.ID_dir_item_image); holder.ID_dir_item_name = (TextVIEw) convertVIEw.findVIEwByID(R.ID.ID_dir_item_name); holder.ID_dir_item_count = (TextVIEw) convertVIEw.findVIEwByID(R.ID.ID_dir_item_count); holder.choose = (ImageVIEw) convertVIEw.findVIEwByID(R.ID.choose); convertVIEw.setTag(holder); } else { holder = (FolderVIEwHolder) convertVIEw.getTag(); } ImageFloder item = mDirPaths.get(position); loader.displayImage("file://" + item.getFirstimagePath(),holder.ID_dir_item_image,options); holder.ID_dir_item_count.setText(item.images.size() + getResources().getString(R.string.sheet)); holder.ID_dir_item_name.setText(item.getname()); holder.choose.setVisibility(currentimageFolder == item ? VIEw.VISIBLE : VIEw.GONE); return convertVIEw; } } //自定义的一个类用来缓存convertvIEw class FolderVIEwHolder { ImageVIEw ID_dir_item_image; ImageVIEw choose; TextVIEw ID_dir_item_name; TextVIEw ID_dir_item_count; } /** * 获取缩略图 */ private voID getthumbnail() { Cursor mCursor = mContentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,new String[] { MediaStore.Images.ImageColumns.DATA },"",null,MediaStore.MediaColumns.DATE_ADDED + " DESC"); if (mCursor.movetoFirst()) { int _date = mCursor.getColumnIndex(MediaStore.Images.Media.DATA); do { // 获取图片的路径 String path = mCursor.getString(_date); imageAll.images.add(new ImageItem(path)); // 获取该图片的父路径名 file parentfile = new file(path).getParentfile(); if (parentfile == null) { continue; } ImageFloder imageFloder = null; String dirPath = parentfile.getabsolutePath(); if (!tmpDir.containsKey(dirPath)) { // 初始化imageFloder imageFloder = new ImageFloder(); imageFloder.setDir(dirPath); imageFloder.setFirstimagePath(path); mDirPaths.add(imageFloder); tmpDir.put(dirPath,mDirPaths.indexOf(imageFloder)); } else { imageFloder = mDirPaths.get(tmpDir.get(dirPath)); } imageFloder.images.add(new ImageItem(path)); } while (mCursor.movetoNext()); } mCursor.close(); tmpDir = null; } class ImageFloder { /** * 图片的文件夹路径 */ private String dir; /** * 第一张图片的路径 */ private String firstimagePath; /** * 文件夹的名称 */ private String name; public List<ImageItem> images = new ArrayList<ImageItem>(); public String getDir() { return dir; } public voID setDir(String dir) { this.dir = dir; int lastIndexOf = this.dir.lastIndexOf("/"); this.name = this.dir.substring(lastIndexOf); } public String getFirstimagePath() { return firstimagePath; } public voID setFirstimagePath(String firstimagePath) { this.firstimagePath = firstimagePath; } public String getname() { return name; } } class ImageItem { String path; public ImageItem(String p) { this.path = p; } }}
最后还要在清单文件AndroIDManifest.xml中设置权限:
<uses-permission androID:name="androID.permission.INTERNET"/> <uses-feature androID:name="androID.harDWare.camera" /> <uses-feature androID:name="androID.harDWare.camera.autofocus" /> <uses-permission androID:name="androID.permission.READ_EXTERNAL_STORAGE" /> <uses-permission androID:name="androID.permission.WRITE_EXTERNAL_STORAGE" />
下载地址:
Android自定义仿微信图片选择器.zip
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android仿微信朋友圈图片选择器全部内容,希望文章能够帮你解决Android仿微信朋友圈图片选择器所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)