I'm using Gallery in my project. The features I want are listed below:
There should be 5 images visible at a time on the screen.
Number of images can increase or decrease dynamically.
The center image or we can say the selected image should be larger than rest of the four images.(0,1,[2],3,4).
The neighbouring images of center one should be smaller than the selected image but lagrer than the images at the corners.(0,[1],2,[3],4).
The images at the corners of the screen should be smaller than from rest of the images.
I don't know how to implenent this. I have channged the size of the center ImageView but I dont know how to set the size of rest of the four images. Please provide solution to this problem.
Here's my code for changing the size of ImageView
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
public class InfiniteGalleryResourceAdapter extends BaseAdapter {
/** The width of each child image */
private static final int G_ITEM_WIDTH = 60;
/** The height of each child image */
private static final int G_ITEM_HEIGHT = 250;
/** The context your gallery is running in (usually the activity) */
private Context mContext;
private int imageWidth;
private int imageHeight;
/** The array of resource ids to draw */
private final int[] imageIds;
public InfiniteGalleryResourceAdapter(Context c, int[] imageIds ) {
this.mContext = c;
this.imageIds = imageIds;
}
/**
* The count of how many items are in this Adapter
* This will return the max number as we want it to scroll as much as possible
*/
#Override
public int getCount() {
return imageIds.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// convertView is always null in android.widget.Gallery
ImageView i = getImageView();
int itemPos = (position % imageIds.length);
try {
// first we calculate the item position in your list, because we have said the adapters size is Integer.MAX_VALUE
// the position getView gives us is not use-able in its current form, we have to use the modulus operator
// to work out what number in our 'array of paths' this actually equals
Log.d("item position", String.valueOf(itemPos));
i.setImageResource(imageIds[itemPos]);
((BitmapDrawable) i.getDrawable()).setAntiAlias(true); // Make sure we set anti-aliasing otherwise we get jaggies (non-smooth lines)
} catch (OutOfMemoryError e) {
// a 'just in case' scenario
Log.e("InfiniteGalleryResourceAdapter", "Out of memory creating imageview. Using empty view.", e);
}
return i;
}
/**
* Retrieve an ImageView to be used with the Gallery
* #return an ImageView with width and height set to DIP values
*/
private ImageView getImageView() {
setImageDimensions();
ImageView i = new ImageView(mContext);
i.setLayoutParams(new Gallery.LayoutParams(imageWidth, imageHeight));
i.setScaleType(ScaleType.CENTER_INSIDE);
return i;
}
/**
* Sets the dimensions for each View that is used in the gallery
* lazily initialized so that we don't have to keep converting over and over
*/
private void setImageDimensions() {
if (imageWidth == 0 || imageHeight == 0) {
imageWidth = AndroidUtils.convertToPix(mContext, G_ITEM_WIDTH);
imageHeight = AndroidUtils.convertToPix(mContext, G_ITEM_HEIGHT);
}
}
}
Related
I was following this Android tutorial to fix an "out of memory" error I kept getting when using Bitmap images in my GridView. The tutorial fixed this problem, but now when I click on other tabs in my activity, then return to my photo tab, the photos that are there, double. So for example, when I first load the tab, it pulls 3 photos from my SD card, and fills the rest with placeholder images. But then when I browse other tabs (4 altogether), I come back to find 6 images in my photo tab GridView, and the rest are placeholders.
Background info: each time my tab is visible, it loads images from the SD card, changes them into bitmaps, then my adapter puts those into each ImageView inside the GridView. Before I had the memory problem, it loaded fine, and did not double the images when I browsed other tabs then came back to the photo tab. It did however eventually crash with the memory problem, so I had to fix that.
How can I not load double upon a refresh to tab? Thanks for your help.
PhotoTab.java
package org.azurespot.cutecollection;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import org.azurespot.R;
import java.io.File;
import java.util.ArrayList;
/**
* Created by mizu on 2/8/15.
*/
public class PhotoTab extends Fragment {
private GridView gridView;
File[] files;
ArrayList<PhotoGridItem> photoList = new ArrayList<>();
ArrayAdapter<PhotoGridItem> adapter;
Bitmap bitmap;
public PhotoTab() {
super();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.photo_tab, container, false);
// with fragments, make sure you include the rootView when finding id
gridView = (GridView) v.findViewById(R.id.photo_grid);
adapter = new GridViewPhotoAdapter(getActivity(), R.layout.photo_grid_item);
// Set the Adapter to GridView
gridView.setAdapter(adapter);
adapter.addAll(loadSDCard());
// add the default icons remaining, to GridView, if less than 24 files on SD card
for (int i = 0; i < (24 - photoList.size()); i++) {
adapter.add(new PhotoGridItem(BitmapFactory.decodeResource(getResources(),
R.drawable.ic_photo_placeholder)));
}
adapter.notifyDataSetChanged();
return v;
}
private ArrayList<PhotoGridItem> loadSDCard() {
try {
// gets directory CutePhotos from sd card
File cutePhotosDir = new File(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_PICTURES), "CutePhotos");
// lists all files in CutePhotos, loads in Files[] array
files = cutePhotosDir.listFiles();
for (File singleFile : files) {
String filePath = singleFile.getAbsolutePath();
bitmap = decodeSampledBitmap(filePath, 270, 270);
photoList.add(new PhotoGridItem(bitmap));
}
} catch (Exception e) {
e.printStackTrace();
}
return photoList;
}
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
public static Bitmap decodeSampledBitmap(String path, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}
}
GridViewPhotoAdapter
package org.azurespot.cutecollection;
/**
* Created by mizu on 2/5/15.
*/
// package org.azurespot.cutecollection;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import org.azurespot.R;
/**
* Created by mizu on 2/5/15.
*/
public class GridViewPhotoAdapter extends ArrayAdapter<PhotoGridItem> {
public Context context;
private int resourceId;
Bitmap bm;
public GridViewPhotoAdapter(Context context, int layoutResourceId) {
super(context, layoutResourceId);
this.context = context;
this.resourceId = layoutResourceId;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
final ViewHolder holder;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(resourceId, parent, false);
holder = new ViewHolder();
holder.imageView = (ImageView) row.findViewById(R.id.photo_grid_view);
// stores holder with view
row.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
PhotoGridItem photoGridItem = getItem(position);
if (photoGridItem != null) {
bm = photoGridItem.getImage();
holder.imageView.setImageBitmap(bm);
// positioning the image in the GridView slot
holder.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
holder.imageView.setLayoutParams(new LinearLayout.LayoutParams
(270, 270));
}
return row;
}
public class ViewHolder{
ImageView imageView;
}
}
Try this:
adap = null;
if (adap == null) {
adap = new Grid_View_Adatper(activity, arrayList);
gridView.setAdapter(adap);
}
adap.notifyDataSetChanged();
Infinite looping viewpagers rely on an illusion where the count of items is increased to an arbitrarily large number, which are mapped via modulus to index positions in a list.
The problem with this being that indicators such as Circle page indicators get the arbitrarily long count of numbers, because they use the getCount() method of the host PagerAdapter, completely breaking down the illusion of infinite looping.
Ie. You have 3 items you want to loop through, you set the pageradapter count to 1000, when the user gets to item 3 and swipes to item "4", the item 1 shows again. But the indicator shows you are at item 4 and that there are hundreds of other items to swipe to. Instead of just looping between 3 indicator selections.
Is there a solution for this?
I didn't want to spend time with new libraries. So, I code a simple OnPageChangeListener for infinite viewpagers with circle indicator (ImageView). It works well. AutoScrollViewPager is used as infinite viewPager. Indicator changes when viewpager scrolled half of the page not when scrolling is over.
Screenshot (circles are images is):
Usage:
bannerPager.setOnPageChangeListener(new OnPageChangeListenerForInfiniteIndicator(getActivity(), bannerList, bannerPager.getCurrentItem()));
xml:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="#dimen/home_banner_indicator_container_height">
<LinearLayout
android:id="#+id/container_home_page_indicator"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_marginBottom="#dimen/home_banner_indicator_container_inner_margin_vertical"
android:layout_marginTop="#dimen/home_banner_indicator_container_inner_margin_vertical"
android:orientation="horizontal">
</LinearLayout>
</RelativeLayout>
The OnPageChangeListenerForInfiniteIndicator:
import android.app.Activity;
import android.support.v4.view.ViewPager;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import com.kartaca.rbtpicker.R;
import com.kartaca.rbtpicker.model.Banner;
import java.util.ArrayList;
import java.util.List;
/**
* Created by amadeus on 9/18/15.
*/
public class OnPageChangeListenerForInfiniteIndicator implements ViewPager.OnPageChangeListener {
private Activity activity;
private List<ImageView> pageIndicatorList = new ArrayList<ImageView>();
private List<Banner> bannerList;
private LinearLayout containerIndicator;
private int viewPagerActivePosition;
private int positionToUse = 0;
private int actualPosition;
public OnPageChangeListenerForInfiniteIndicator(Activity activity, List<Banner> bannerList, int currentItem) {
this.activity = activity;
this.bannerList = bannerList;
this.actualPosition = currentItem;
this.viewPagerActivePosition = currentItem;
loadIndicators();
}
private void loadIndicators() {
containerIndicator = (LinearLayout) activity.findViewById(R.id.container_home_page_indicator);
if (pageIndicatorList.size() < 1) {
for (Banner banner : bannerList) {
ImageView imageView = new ImageView(activity);
imageView.setImageResource(R.drawable.banner_pagination_normal);// normal indicator image
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setLayoutParams(new ViewGroup.LayoutParams(activity.getResources().getDimensionPixelOffset(R.dimen.home_banner_indicator_width),
ViewGroup.LayoutParams.MATCH_PARENT));
pageIndicatorList.add(imageView);
}
}
containerIndicator.removeAllViews();
for (int x = 0; x < pageIndicatorList.size(); x++) {
ImageView imageView = pageIndicatorList.get(x);
imageView.setImageResource(x == positionToUse ? R.drawable.banner_pagination_active :
R.drawable.banner_pagination_normal); // active and notactive indicator
containerIndicator.addView(imageView);
}
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
actualPosition = position;
int positionToUseOld = positionToUse;
if (actualPosition < viewPagerActivePosition && positionOffset < 0.5f) {
positionToUse = actualPosition % bannerList.size();
} else {
if (positionOffset > 0.5f) {
positionToUse = (actualPosition + 1) % bannerList.size();
} else {
positionToUse = actualPosition % bannerList.size();
}
}
if (positionToUseOld != positionToUse) {
loadIndicators();
}
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
if (state == 0) {
viewPagerActivePosition = actualPosition;
positionToUse = viewPagerActivePosition % bannerList.size();
loadIndicators();
}
}
}
Take a look at InfinitePageIndicator project, maybe it is something that you need.
I'm placing ImageView's in a RelativeLayout. I'm setting them with LayoutParams and using setMargins() to set the location of each picture. The max number of Images that will be placed on top of the first one will only reach 8. Their are 5 diffident Images and 8 positions on the screen where they can be placed. I would like to create the Images as their corresponding buttons are pressed and to be able to set that Image into the RelativeLayout and display the change. I would like a way to clear all the Images off the screen except for the main/ background ImageView. I don't like to populate 8 X 5 = 40 Images and then hide them all then change their view to Visible when i need them to show. I need something that will populate as need be but able to destory or remove when I clear it out.
Thanks,
Zelda
aButton.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
noteNumber++;
if(noteNumber <= 8){
note n = new note(getBaseContext());
n.setNoteNumber(noteNumber);
n.setHeight(85);
images.add(n); //ArrayList()
}
populate();
}
});
}
public void populate(){
//if(noteNumber < 9){
for(note a : images){
//note a = images.get(noteNumber-1); //images is of type ArrayList<ImageView>()
if(a != null && a.getMasterImage() != null){
int number = a.getNoteNumber();
imageParams.setMargins(25+45*number, a.getHeight(), 20, 360);
frame.addView(a.getMasterImage(),imageParams);
}
}
}
}
public class note {
private int noteNumber;
private int height;
private ImageView masterImage;
public note(Context c){
masterImage = new ImageView(c);
masterImage.setImageResource(R.raw.zelda);
this.noteNumber = 1;
height = 0;
}
/**
* #return the masterImage
*/
public ImageView getMasterImage() {
return masterImage;
}
/**
* #param masterImage the masterImage to set
*/
public void setMasterImage(ImageView masterImage) {
this.masterImage = masterImage;
}
/**
* #return the noteNumber
*/
public int getNoteNumber() {
return noteNumber;
}
/**
* #param noteNumber the noteNumber to set
*/
public void setNoteNumber(int noteNumber) {
this.noteNumber = noteNumber;
}
/**
* #return the height
*/
public int getHeight() {
return height;
}
/**
* #param height the height to set
*/
public void setHeight(int height) {
this.height = height;
}
}
Im working on a app where im loading an image into the GridView... My problem now is
when im using the imageView.setPadding(10,10,10,10) its working fine for HTC Hero but in case of Motorola Droid they are overlapping each other...
How to make the padding to be common for all mobile...
Here is my code..
package com.android.sampleDesign1;
import android.content.Context;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
public class TicTacToeAdapter extends BaseAdapter {
public ImageView imageView;
private Context mContext;
private Integer mThumbIds = R.drawable.images;
private Integer image;
public TicTacToeAdapter(Context c) {
mContext = c;
}
public int getCount() {
return 9;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
image = mThumbIds;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(image);
imageView.setPadding(10,10,10,10);
} else {
imageView = (ImageView) convertView;
imageView.setImageResource(image);
}
return imageView;
}
}
I have also tried to use the
int w = gridView.getWidth();
int myViewWidth = Math.round(W * .12f);
and also i tried with the
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
lp.setMargins(left, top, right, bottom);
imageView.setLayoutParams(lp);
So is there anyother way to do.. Or am i wrong anywhere..
Help me out..
Thanks in advance.
Rather defining the padding in pixels, you need to define it in dips (density independent pixels) and then convert the dips to pixels at run time.
So you would need to do something similar to
private static final float PADDING_IN_DP = 10.0f; // 1 dip = 1 pixel on an MDPI device
private final int mPaddingInPixels;
public TicTacToeAdapter(Context context) {
....
// Convert the dps to pixels
final float scale = context.getResources().getDisplayMetrics().density;
mPaddingInPixels = (int) (PADDING_IN_DP * scale + 0.5f);
}
...
public View getView(int position, View convertView, ViewGroup parent) {
...
if (convertView == null) {
...
imageView.setPadding(mPaddingInPixels, mPaddingInPixels, mPaddingInPixels, mPaddingInPixels);
}
...
}
...
The scale you get in your constructor will differ depending on the density of the screen of the device your app is running on.
(Note: dip and dp are the same thing)
There are two things I want to achieve with the Gallery view:
1) when a user makes a selection within the Gallery view I do not want the images to move (i.e. I do not want the selected image to move to the center)
2) when a user makes a selection within the Gallery view I want the selected image to be highlighted orange on ACTION_DOWN, and the highlight removed on ACTION_UP - just like on Buttons.
How is that done ?
Solved it myself ...
the following features are included in this sample code
apparently-endless horizontal
scrolling
selection detection without using
the GODAWFUL
Gallery.setOnItemSelectedListener().
highlighting the selection
(although this needs more work -
namely storing the selection because
the Gallery will lose the
highlighting after it's scrolled
off-screen)
public class ActivityMine extends Activity implements OnTouchListener
{
//UI references
private Gallery mGallery;
//member variables
private float fX, fY;
private ImageBuffer mImageBuffer;//just an array of images
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.filter);
//get references to UI components
mGallery = (Gallery) findViewById(R.id.filter_gallery);
//make widgets touch sensitive
mGallery.setOnTouchListener(this);
//connect Gallery with adapter
mGallery.setAdapter(new GalleryAdapter(this, mImageBuffer));
//auto-select first image (position 1073741820)
final int iTemp = (int)( Integer.MAX_VALUE / 2 ) - ( ( Integer.MAX_VALUE / 2 ) % mImageBuffer.getCount() );
mGallery.setSelection(iTemp);
}
#Override
protected void onDestroy()
{
super.onDestroy();
}
public boolean onTouch(View v, MotionEvent event)
{
final int actionPerformed = event.getAction();
final int widgetID = v.getId();
if (actionPerformed == MotionEvent.ACTION_DOWN)
{
if (widgetID == R.id.filter_gallery)
{
fX=event.getRawX();
fY=event.getRawY();
}
}
if (actionPerformed == MotionEvent.ACTION_UP)
{
switch (widgetID)
{
case R.id.filter_gallery:
{
final float posX = event.getRawX();
final float posY = event.getRawY();
final float MAXPRESSRANGE = 10.0f;
//detect if user performed a simple press
if ((posX < fX+MAXPRESSRANGE) && (posX > fX-MAXPRESSRANGE))
{
if ((posY < fY+MAXPRESSRANGE) && (posY > fY-MAXPRESSRANGE))
{
//valid press detected!
//convert gallery coordinates to a position
final int iPosition = mGallery.pointToPosition((int)event.getX(), (int)event.getY());
if (iPosition != AdapterView.INVALID_POSITION)
{
//this is necessary to obtain the index of the view currently visible and pressed
final int iVisibleViewIndex = iPosition - mGallery.getFirstVisiblePosition();
//get a reference to the child and modify the border
View child = mGallery.getChildAt(iVisibleViewIndex);
if (child != null)
{
RelativeLayout borderImg = (RelativeLayout)child;
borderImg.setBackgroundColor(0xFFFFFFFF);
}
}
// consume event
return true;
}
}
}//end of: case R.id.filter_gallery
}//end of: switch()
}
return false;//do not consume event otherwise the UI widget touched won't receive the event
}
public class GalleryAdapter extends BaseAdapter
{
//member variables
private Context mContext;
private ImageBuffer mImageBuffer;
public GalleryAdapter(Context context, ImageBuffer imageBuffer)
{
mContext = context;
mImageBuffer = imageBuffer;
}
//get count of images in the gallery
public int getCount()
{
return Integer.MAX_VALUE;
}
public Object getItem(int position)
{
if (position >= mImageBuffer.getCount())
{
position = position % mImageBuffer.getCount();
}
return position;
}
public long getItemId(int position)
{
if (position >= mImageBuffer.getCount())
{
position = position % mImageBuffer.getCount();
}
return position;
}
//returns the individual images to the widget as it requires them
public View getView(int position, View convertView, ViewGroup parent)
{
final ImageView imgView = new ImageView(mContext);
if (position >= mImageBuffer.getCount())
{
position = position % mImageBuffer.getCount();
}
imgView.setImageBitmap(mImageBuffer.getBitmapFromBuffer(position));
//put black borders around the image
final RelativeLayout borderImg = new RelativeLayout(mContext);
borderImg.setPadding(5, 5, 5, 5);
borderImg.setBackgroundColor(0xff000000);
borderImg.addView(imgView);
return borderImg;
}
}
}
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
public class ImageBuffer
{
//member variables
private Activity mActivity;
private Map<String,Integer> mIndexMap;
private List<Bitmap> mBitmapList;
private int mImageCount;
public ImageBuffer(Activity activity)
{
mActivity = activity;
mIndexMap = new HashMap<String,Integer>();
mBitmapList = new ArrayList<Bitmap>();
mImageCount=0;
}
public int getCount()
{
return mImageCount;
}
public int putAssetBitmapInBuffer(String assetfilename, String overrideFilename)
{
int iValueToReturn = -1;
try
{
Bitmap bitmap = getBitmapFromAsset(assetfilename);
if (bitmap != null)
{
final int iIndex = mBitmapList.size();
if (overrideFilename == null)
{
mIndexMap.put(assetfilename, iIndex);
}
else
{
mIndexMap.put(overrideFilename, iIndex);
}
mBitmapList.add(bitmap);
iValueToReturn = iIndex;
mImageCount++;
}
}
catch (IOException e)
{
e.printStackTrace();
}
return iValueToReturn;
}
/**
* put an image from filesystem folder into buffer
* #param filename name of file, i.e. "image.png"
* #param strSubFolder subfolder, i.e. "/products"
* #param bGetFromSDCARD when true the image is read from SDCARD, otherwise main memory
* #return on success: index of image in buffer. on error: -1
*/
public int putLocalFSbitmapInBuffer(String filename, String strSubFolder, boolean bGetFromSDCARD)
{
int iValueToReturn = -1;
Bitmap bitmap = getBitmapFromLocalFS(filename, strSubFolder, bGetFromSDCARD);
if (bitmap != null)
{
final int iIndex = mBitmapList.size();
mIndexMap.put(filename, iIndex);
mBitmapList.add(bitmap);
iValueToReturn = iIndex;
mImageCount++;
}
return iValueToReturn;
}
public Bitmap getBitmapFromBuffer(String assetfilename)
{
Bitmap bitmap = null;
if (mIndexMap.containsKey(assetfilename))
{
final int iIndex = mIndexMap.get(assetfilename);
bitmap = mBitmapList.get(iIndex);
}
return bitmap;
}
public Bitmap getBitmapFromBuffer(int position)
{
Bitmap bitmap = null;
if (position < mBitmapList.size() && position > -1)
{
bitmap = mBitmapList.get(position);
}
return bitmap;
}
/**
* get bitmap from assets folder in project
* #param assetfilename name of asset, i.e. "image.png"
* #return Bitmap
* #exception IOException when file is not found
*/
private Bitmap getBitmapFromAsset(String assetfilename) throws IOException
{
AssetManager assetManager = mActivity.getAssets();
InputStream istr = assetManager.open(assetfilename);
Bitmap bitmap = BitmapFactory.decodeStream(istr);
istr.close();
return bitmap;
}
/**
* get bitmap from file system
* #param filename name of image, i.e. "image.png"
* #param strSubFolder subfolder, i.e. "/products"
* #param bGetFromSDCARD when true the image is read from SDCARD, otherwise main memory
* #return Bitmap
*/
private Bitmap getBitmapFromLocalFS(String filename, String strSubFolder, boolean bGetFromSDCARD)
{
File file;
if (bGetFromSDCARD == true)
{
//store in SDCARD
file = new File(Environment.getExternalStorageDirectory()+strSubFolder, filename);
}
else
{
//store in main memory
file = new File(mActivity.getFilesDir().getAbsolutePath()+strSubFolder, filename);
}
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
return bitmap;
}
}