1. 程式人生 > >android列出目錄下的所有圖片

android列出目錄下的所有圖片


public class GalleryTestActivity extends Activity {
  File photos[];
  private static String TAG = "GalleryTest";
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         File f = new File("/sdcard/pics1");
         photos = f.listFiles(new FilenameFilter() {  
    @Override
    public boolean accept(File dir, String filename) {
     return filename.endsWith("jpg");
    }
   });
         String fileList = "";      
         for (File aFile : photos) {
          fileList += aFile.getName() + "\n";
         }
         TextView v = (TextView)findViewById(R.id.FileList);
         v.setText(fileList);

         Gallery gallery = (Gallery)findViewById(R.id.Gallery);
         gallery.setAdapter(new AddImgAdp(this));
     }

     public class AddImgAdp extends BaseAdapter {
         int GalItemBg;
         private Context cont;

         public AddImgAdp(Context c) {
             cont = c;
             TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
             GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
             typArray.recycle();
         }

         public int getCount() {
             return photos.length;
         }

         public Object getItem(int position) {
             return position;
         }

         public long getItemId(int position) {
             return position;
         }

         public View getView(int position, View convertView, ViewGroup parent) {
          ImageView imgView;
          if (convertView == null)
           imgView = new ImageView(cont);
          else
           imgView = (ImageView)convertView;

             Bitmap bMap = BitmapFactory.decodeFile(filename);
             imgView.setImageBitmap(bMap);

             imgView.setLayoutParams(new Gallery.LayoutParams(120, 90));
             imgView.setScaleType(ImageView.ScaleType.FIT_XY);
             imgView.setBackgroundResource(GalItemBg);

             return imgView;
         }
     }
 }

http://wang-peng1.iteye.com/blog/835426


//將sd卡的圖片一張張顯示出來,需要先全部載入然後在顯示,如果載入一張顯一張比較慢
public class Pics extends Activity {
private ImageView image;
private String[] mFiles;
private int mFilePosition = 0;
private ArrayList<Bitmap> bitArray;
private Bitmap b;
private int i;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    image = (ImageView) findViewById(R.id.image);
    bitArray = new ArrayList<Bitmap>();
    File images = Environment
            .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    try {
        images.createNewFile();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Log.e("file path", images.getAbsolutePath());
    File[] imagelist = images.listFiles(new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return ((name.endsWith(".jpg")) || (name.endsWith(".png")));
        }
    });
    mFiles = new String[imagelist.length];
    Log.e("size", imagelist.length + "");
    for (int i = 0; i < imagelist.length; i++) {
        mFiles[i] = imagelist[i].toString();
    }
    getImageFromSDCard();
}

public void getImageFromSDCard() {

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 8;
    try {
        for (int j = 0; j < mFiles.length; j++) {
            Log.d("", mFiles[mFilePosition]);
            b = BitmapFactory.decodeFile(mFiles[j], null);
            bitArray.add(b);
        }

    } catch (Exception e) {
        // Log error
    }
}
}