Back to project page MediaPicker.
The source code is released under:
Apache License
If you think the Android project MediaPicker listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.urza.mediapicker; /*from w w w. ja v a 2s . c o m*/ import android.content.Context; import android.graphics.Bitmap; import android.provider.MediaStore; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import java.util.ArrayList; import java.util.HashMap; import java.util.List; /** * Created by urza on 26.7.2014. */ public class MediaEntityAdapter extends BaseAdapter implements MediaEntityBaseAdapter { private Context mContext; private int loaderType; private LayoutInflater mLayoutInflater; private List<MediaEntityWrapper> mEntityList; private ImageLoader imageLoader = ImageLoader.getInstance(); private DisplayImageOptions displayImageOptions; private HashMap<String, List<MediaEntityWrapper>> selection; private HashMap<Long, Integer> idToPositionMap; public MediaEntityAdapter(Context context, int loaderType) { this.loaderType = loaderType; mContext = context; mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mEntityList = new ArrayList<MediaEntityWrapper>(); idToPositionMap = new HashMap<Long, Integer>(); displayImageOptions = new DisplayImageOptions.Builder() .showImageOnLoading(R.drawable.empty_photo) .cacheInMemory(true) .cacheOnDisk(true) .considerExifParams(true) .bitmapConfig(Bitmap.Config.RGB_565) .build(); } @Override public void setData(List<MediaEntityWrapper> data) { mEntityList = data; if (mEntityList != null) { for (int i = 0; i <= mEntityList.size() - 1; i++) { idToPositionMap.put(Long.parseLong(mEntityList.get(i).masterId), i); } } notifyDataSetChanged(); } public int getPositionForId(long id) { return idToPositionMap.get(id); } @Override public int getCount() { if (mEntityList == null) { return 0; } else { return mEntityList.size(); } } @Override public Object getItem(int position) { return mEntityList.get(position); } @Override public long getItemId(int position) { return Long.parseLong(mEntityList.get(position).masterId); } //Adapter has stable Ids but overriding this method causes error on //AbsListView.confirmCheckedPositionsById() /*public boolean hasStableIds() { return true; }*/ @Override public View getView(int position, View convertView, ViewGroup parent) { final EntityHolder viewHolder; if (convertView == null) { convertView = mLayoutInflater.inflate(R.layout.item_media_entity, parent, false); viewHolder = new EntityHolder(); viewHolder.mThumb = (ImageView) convertView.findViewById(R.id.entityThumb); viewHolder.mId = (TextView) convertView.findViewById(R.id.entityId); viewHolder.mMime = (TextView) convertView.findViewById(R.id.entityMimeType); viewHolder.mContentUri = (TextView) convertView.findViewById(R.id.entityContentUri); viewHolder.mData = (TextView) convertView.findViewById(R.id.entityData); viewHolder.mThumbId = (TextView) convertView.findViewById(R.id.thumbId); viewHolder.mThumbData = (TextView) convertView.findViewById(R.id.thumbData); convertView.setTag(viewHolder); } else { viewHolder = (EntityHolder) convertView.getTag(); } viewHolder.mId.setText("Id: " + mEntityList.get(position).masterId); viewHolder.mMime.setText("MimeType: " + mEntityList.get(position).mimeType); viewHolder.mContentUri.setText("ContentUri: " + MediaStore.Files.getContentUri("external").toString() + "/" + mEntityList.get(position).masterId); viewHolder.mData.setText("Data: " + mEntityList.get(position).masterDataPath); viewHolder.mThumbId.setText("Thumb id: " + mEntityList.get(position).thumbId); viewHolder.mThumbData.setText("Thumb data: " + mEntityList.get(position).thumbDataPath); switch (loaderType) { case MultiPicker.IMAGE_LOADER: { renderImage(position, viewHolder.mThumb); break; } case MultiPicker.VIDEO_LOADER: { renderVideo(position, viewHolder.mThumb); break; } } return convertView; } private void renderImage(int position, ImageView imageView) { if (mEntityList.get(position).thumbId != null) { imageLoader.displayImage( MediaStore.Images.Thumbnails.getContentUri("external").toString() + "/" + mEntityList.get(position).thumbId, imageView, displayImageOptions ); } else imageLoader.displayImage( MediaStore.Images.Media.getContentUri("external").toString() + "/" + mEntityList.get(position).masterId, imageView, displayImageOptions ); } private void renderVideo(int position, ImageView imageView) { imageLoader.displayImage( MediaStore.Video.Thumbnails.getContentUri("external").toString() + "/" + mEntityList.get(position).thumbId, imageView, displayImageOptions ); } public void removeAndRedraw(int position, AbsListView absListView){ mEntityList.remove(position); //We click on item to remove it from AbsListView data //and so it changes the checked state of item on position to false absListView.setItemChecked(position,true); notifyDataSetChanged(); } public void setSelection(HashMap<String, List<MediaEntityWrapper>> selection) { this.selection = selection; } private class EntityHolder { ImageView mThumb; TextView mId; TextView mMime; TextView mContentUri; TextView mData; TextView mThumbId; TextView mThumbData; } }