Skip to content

Commit

Permalink
add images from directory
Browse files Browse the repository at this point in the history
  • Loading branch information
rajasharan committed Sep 12, 2015
1 parent 60b15a1 commit f624c64
Showing 1 changed file with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -77,6 +78,30 @@ public ScrollGalleryView addImage(Bitmap image) {
return this;
}

public ScrollGalleryView addImages(File fromDir) {
File[] files = fromDir.listFiles();
BitmapFactory.Options options = new BitmapFactory.Options();

for (int i=0; files != null && i < files.length; i++) {
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(files[i].getAbsolutePath(), options);
int imgWidth = options.outWidth;
int imgHeight = options.outHeight;

int maxWidth = displayProps.x;
int maxHeight = displayProps.y;

/* calculate appropriate inSampleSize for high-res images from SDCARD */
options.inSampleSize = calculateInSampleSize(imgWidth, imgHeight, maxWidth, maxHeight);
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(files[i].getAbsolutePath(), options);
images.add(bitmap);

This comment has been minimized.

Copy link
@hasnain-ahmad

hasnain-ahmad Dec 6, 2019

how to resolve the error on images object in the code?

addThumbnail(bitmap);
}
pagerAdapter.notifyDataSetChanged();
return this;
}

public ScrollGalleryView setThumbnailSize(int thumbnailSize) {
this.thumbnailSize = thumbnailSize;
return this;
Expand Down Expand Up @@ -148,4 +173,12 @@ private void scroll(View thumbnail) {

horizontalScrollView.smoothScrollBy(-thumbnailDelta, 0);
}

private int calculateInSampleSize(int imgWidth, int imgHeight, int maxWidth, int maxHeight) {
int inSampleSize = 1;
while (imgWidth / inSampleSize > maxWidth || imgHeight / inSampleSize > maxHeight) {
inSampleSize *= 2;
}
return inSampleSize;
}
}

0 comments on commit f624c64

Please sign in to comment.