/[projects]/android/Side9/src/dk/thoerup/side9/ImageAdapter.java
ViewVC logotype

Annotation of /android/Side9/src/dk/thoerup/side9/ImageAdapter.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 912 - (hide annotations) (download)
Sat Jun 26 08:21:22 2010 UTC (13 years, 11 months ago) by torben
File size: 2440 byte(s)
Enable use of CheckUpdates
PictureOverview: don't crash if /sdcard/Side9 folder couldn't be found
1 torben 790 package dk.thoerup.side9;
2    
3     import java.io.File;
4     import java.util.ArrayList;
5    
6     import android.content.Context;
7     import android.graphics.Bitmap;
8     import android.graphics.BitmapFactory;
9     import android.os.Environment;
10     import android.view.View;
11     import android.view.ViewGroup;
12     import android.widget.BaseAdapter;
13     import android.widget.GridView;
14     import android.widget.ImageView;
15 torben 912 import android.widget.Toast;
16 torben 790
17     public class ImageAdapter extends BaseAdapter {
18     //int mGalleryItemBackground;
19     private Context mContext;
20    
21 torben 791 ArrayList<String> mBitmapPaths = new ArrayList<String>();
22     ArrayList<Bitmap> mBitmaps = new ArrayList<Bitmap>();
23 torben 790
24     public ImageAdapter(Context c) {
25     mContext = c;
26    
27     String path = Environment.getExternalStorageDirectory().getPath() + "/Side9" ;
28    
29 torben 912
30    
31 torben 790 File root = new File(path);
32 torben 912
33     if (root.exists() == false) {
34     Toast.makeText(mContext, "Side9 folder not found on sdcard", Toast.LENGTH_LONG).show();
35     return;
36     }
37    
38 torben 790 File files[] = root.listFiles();
39 torben 791 for (File f : files) {
40     Bitmap bmp = BitmapFactory.decodeFile( f.getPath() );
41     Bitmap scaled = Bitmap.createScaledBitmap(bmp, bmp.getWidth()/4, bmp.getHeight()/4, true);
42    
43     mBitmapPaths.add( f.getPath() );
44     mBitmaps.add(scaled);
45 torben 790 }
46     }
47    
48     public int getCount() {
49     return mBitmaps.size();
50     }
51    
52     public Object getItem(int position) {
53     return position;
54     }
55    
56     public long getItemId(int position) {
57     return position;
58     }
59    
60     public String getImagePath(int position) {
61 torben 791 return mBitmapPaths.get(position);
62 torben 790 }
63    
64     public View getView(int position, View convertView, ViewGroup parent) {
65     ImageView imageView;
66     if (convertView == null) { // if it's not recycled, initialize some attributes
67     imageView = new ImageView(mContext);
68     imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
69     imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
70     imageView.setAdjustViewBounds(true);
71     imageView.setPadding(4, 4, 4, 4);
72     } else {
73     imageView = (ImageView) convertView;
74     }
75    
76 torben 791
77     imageView.setImageBitmap( mBitmaps.get(position) );
78 torben 790 return imageView;
79    
80     }
81     }

  ViewVC Help
Powered by ViewVC 1.1.20