Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.ap.wificam.collection;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.os.AsyncTask;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.ap.wificam.R;
import com.ap.wificam.model.Collection;
import java.io.File;
import java.util.ArrayList;
/**
* Created by AND1 on 07.03.14.
*/
public class CollectionListAdapter extends BaseAdapter {
private Context context;
private ArrayList<Collection> collectionList;
private int activeCollectionId;
private File SDCardRoot = Environment.getExternalStorageDirectory();
private SharedPreferences prefs;
private String savePath;
public CollectionListAdapter(Context context, ArrayList<Collection> collectionList, int activeCollectionId) {
this.context = context;
this.collectionList = collectionList;
this.activeCollectionId = activeCollectionId;
prefs = PreferenceManager.getDefaultSharedPreferences(context);
savePath = prefs.getString("folder_name_text", "default_no");
}
@Override
public int getCount() {
return collectionList.size();
}
@Override
public Object getItem(int i) {
return collectionList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
Collection collection = collectionList.get(i);
if(view == null){
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.item_collection_row, null);
}
TextView textViewDate = (TextView) view.findViewById(R.id.textViewDate);
textViewDate.setText(collection.getDate());
TextView textViewLocation = (TextView) view.findViewById(R.id.textViewLocation);
textViewLocation.setText(collection.getLocation());
TextView textViewName = (TextView) view.findViewById(R.id.textViewName);
textViewName.setText(collection.getName());
TextView textViewActiveCollection = (TextView) view.findViewById(R.id.textViewActiveCollection);
//set and remove the highlight and text
if( ((int)collection.getId()) == activeCollectionId){
textViewActiveCollection.setVisibility(View.VISIBLE);
view.setBackgroundColor(context.getResources().getColor(R.color.bg_gray_highlight));
}
else{
textViewActiveCollection.setVisibility(View.INVISIBLE);
view.setBackgroundColor(0x00000000);
}
String folderPath = SDCardRoot.getAbsolutePath() + savePath + collection.getId() + "/"; //TODO single var file path
File collectionFolder = new File(folderPath);
int imageCount = 0;
if(collectionFolder.exists()){
imageCount = collectionFolder.list().length;
}
TextView textViewImageCount = (TextView) view.findViewById(R.id.textViewImageCount);
textViewImageCount.setText(String.valueOf(imageCount));
//TODO use pre created thumbs
//genereate ImageView with thumbs
ImageView imageViewThumb0 = (ImageView) view.findViewById(R.id.imageViewThumb0);
ImageView imageViewThumb1 = (ImageView) view.findViewById(R.id.imageViewThumb1);
ImageView imageViewThumb2 = (ImageView) view.findViewById(R.id.imageViewThumb2);
ImageView imageViewThumb3 = (ImageView) view.findViewById(R.id.imageViewThumb3);
ViewHolder holder = new ViewHolder(i, imageViewThumb0, imageViewThumb1, imageViewThumb2, imageViewThumb3);
holder.position = i;
if(imageCount>0){
String[] imageFilePaths = getFilePaths(folderPath);
new ThumbnailTask(i, holder, imageFilePaths).execute("");
}
return view;
}
private class ThumbnailTask extends AsyncTask<String, Void, String> { //TODO better Classname
private ViewHolder viewHolder;
private String[] filePaths;
private Bitmap thumb0, thumb1, thumb2, thumb3;
private int position;
public ThumbnailTask(int position, ViewHolder holder, String[] filePaths){
super();
this.position = position;
this.viewHolder = holder;
this.filePaths = filePaths;
}
@Override
protected String doInBackground(String... params) {
//TODO make thumbs if not existing
if(filePaths.length>0){
thumb0 = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(filePaths[0]), 200, 200);
if(filePaths.length>1){
thumb1 = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(filePaths[1]), 200, 200);
if(filePaths.length>2){
thumb2 = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(filePaths[2]), 200, 200);
if(filePaths.length>3){
thumb3 = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(filePaths[3]), 200, 200);
}
}
}
}
return "Executed";
}
@Override
protected void onPostExecute(String result) {
if(viewHolder.position == this.position){
if(filePaths.length>0){
viewHolder.imageView0.setImageBitmap(thumb0);
if(filePaths.length>1){
viewHolder.imageView1.setImageBitmap(thumb1);
if(filePaths.length>2){
viewHolder.imageView2.setImageBitmap(thumb2);
if(filePaths.length>3){
viewHolder.imageView3.setImageBitmap(thumb3);
}
}
}
}
}
}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(Void... values) {}
}
public class ViewHolder
{
public ImageView imageView0;
public ImageView imageView1;
public ImageView imageView2;
public ImageView imageView3;
public int position;
public ViewHolder(int position, ImageView imageView0, ImageView imageView1, ImageView imageView2, ImageView imageView3){
this.position = position;
this.imageView0 = imageView0;
this.imageView1 = imageView1;
this.imageView2 = imageView2;
this.imageView3 = imageView3;
}
}
public String[] getFilePaths(String folderPath){
ArrayList<String> resultIAV = new ArrayList<String>();
File imageDir = new File(folderPath); //TODO these are variable file patha. dublicate code except for file://
String[] imgArray;
File[] imageList = imageDir.listFiles();
//TODO catch if no files are found
if(imageDir.exists()){
for (File imagePath : imageList) {
try {
if(imagePath.isDirectory()) {
imageList = imagePath.listFiles();
}
if ( imagePath.getName().contains(".jpg")|| imagePath.getName().contains(".JPG")
|| imagePath.getName().contains(".jpeg")|| imagePath.getName().contains(".JPEG")
|| imagePath.getName().contains(".png") || imagePath.getName().contains(".PNG")
|| imagePath.getName().contains(".gif") || imagePath.getName().contains(".GIF")
|| imagePath.getName().contains(".bmp") || imagePath.getName().contains(".BMP"))
{
String path = imagePath.getAbsolutePath();
resultIAV.add(path);
Log.d("and1 : ", path);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
else {
imgArray = new String[0];
}
imgArray = resultIAV.toArray(new String[0]);
return imgArray;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment