Last active
August 29, 2015 13:57
-
-
Save CoffeeCode/9529258 to your computer and use it in GitHub Desktop.
V. 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.util.ArrayList; | |
import static android.graphics.BitmapFactory.decodeFile; | |
/** | |
* 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; | |
private String[] imagePaths; | |
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); | |
Log.d("and1", "element : " + i); | |
ViewHolder holder; | |
if(view == null){ | |
LayoutInflater inflater = (LayoutInflater) context | |
.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
view = inflater.inflate(R.layout.item_collection_row, null); | |
holder = new ViewHolder(); | |
holder.name = (TextView) view.findViewById(R.id.textViewName); | |
holder.date = (TextView) view.findViewById(R.id.textViewDate); | |
holder.location = (TextView) view.findViewById(R.id.textViewLocation); | |
holder.active = (TextView) view.findViewById(R.id.textViewActiveCollection); | |
holder.count = (TextView) view.findViewById(R.id.textViewImageCount); | |
holder.imageView[0] = (ImageView) view.findViewById(R.id.imageViewThumb0); | |
holder.imageView[1] = (ImageView) view.findViewById(R.id.imageViewThumb1); | |
holder.imageView[2] = (ImageView) view.findViewById(R.id.imageViewThumb2); | |
holder.imageView[3] = (ImageView) view.findViewById(R.id.imageViewThumb3); | |
holder.position = i; | |
view.setTag(holder); | |
Log.d("and1", "holder if"); | |
} | |
else{ | |
holder = (ViewHolder) view.getTag(); | |
Log.d("and1", "holder else"); | |
} | |
holder.name.setText(collection.getName()); | |
holder.date.setText(collection.getDate()); | |
holder.location.setText(collection.getLocation()); | |
holder.count.setText(String.valueOf(getImageCount((int) collection.getId()))); | |
new ThumbnailTask(i, holder).execute(imagePaths); | |
//highlighten um active collection vorzuheben | |
if( ((int)collection.getId()) == activeCollectionId){ | |
holder.active.setVisibility(View.VISIBLE); | |
view.setBackgroundColor(context.getResources().getColor(R.color.bg_gray_highlight)); | |
} | |
else{ | |
holder.active.setVisibility(View.INVISIBLE); | |
view.setBackgroundColor(0x00000000); | |
} | |
return view; | |
} | |
private int getImageCount(int collectionId){ | |
String folderPath = SDCardRoot.getAbsolutePath() + savePath + collectionId + "/"; //TODO single var file path | |
imagePaths = getFilePaths(folderPath); | |
File collectionFolder = new File(folderPath); | |
int imageCount = 0; | |
if(collectionFolder.exists()) | |
imageCount = collectionFolder.list().length; | |
return imageCount; | |
} | |
private class ThumbnailTask extends AsyncTask<String, Void, Bitmap[]> { | |
private ViewHolder viewHolder; | |
private Bitmap thumb[] = new Bitmap[4]; | |
private int position; | |
public ThumbnailTask(int position, ViewHolder holder){ | |
super(); | |
this.position = position; | |
this.viewHolder = holder; | |
} | |
@Override | |
protected Bitmap[] doInBackground(String... args) { | |
//Lade Die Bilder als Bitmap wenn welche existieren | |
File imageFile; | |
File thumbFile; | |
int j = args.length; | |
if (j >4) j=4; | |
for(int i=0; i<j; i++){ | |
imageFile = new File(args[i]); | |
thumbFile = new File(context.getFilesDir().getAbsolutePath() + "/thumbs/" + imageFile.getName()); | |
if(!thumbFile.exists()){ | |
thumb[i] = createThumb(args[i]); | |
} | |
else{ | |
thumb[i] = BitmapFactory.decodeFile(thumbFile.getAbsolutePath()); | |
} | |
} | |
return null; | |
} | |
@Override | |
protected void onPostExecute(Bitmap[] result) { | |
if(viewHolder.position == this.position) { | |
for (int i = 0; i < 4; i++) { | |
if(thumb[i] != null) | |
viewHolder.imageView[i].setImageBitmap(thumb[i]); | |
else | |
viewHolder.imageView[i].setImageResource(R.drawable.ic_action_new); | |
} | |
} | |
} | |
@Override | |
protected void onPreExecute() {} | |
@Override | |
protected void onProgressUpdate(Void... values) {} | |
public Bitmap createThumb(String filePath){ | |
File imageFile = new File(filePath); | |
File thumbFile = new File(context.getFilesDir().getAbsolutePath() + "/thumbs/" + imageFile.getName()); | |
Bitmap thumbX = ThumbnailUtils.extractThumbnail(decodeFile(filePath), 200, 200); | |
//saveThumb(thumb, thumbFile); | |
return thumbX; | |
} | |
//TODO | |
public void saveThumb(Bitmap thumb, File thumbFile) { | |
FileOutputStream out = null; | |
try { | |
out = new FileOutputStream(thumbFile.getAbsolutePath()); | |
} catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} | |
thumb.compress(Bitmap.CompressFormat.JPEG, 85, out); | |
} | |
//TODO | |
public void loadThumb(String filePath){ | |
} | |
} | |
public class ViewHolder | |
{ | |
public ImageView imageView[] = new ImageView[4]; | |
public TextView date, location, name, active, count; | |
public int position; | |
} | |
public String[] getFilePaths(String folderPath){ | |
[...] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment