Odd behavior in adding items to listview - android

I have a custom photo gallery and I need to get the selected images and display their filenames in a listview
This is my custom gallery:
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
public class CustomPhotoGallery extends Activity {
private GridView grdImages;
private Button btnSelect;
private ImageAdapter imageAdapter;
private String[] arrPath;
private boolean[] thumbnailsselection;
private int ids[];
private int count;
String filename;
/**
* Overrides methods
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_gallery);
grdImages= (GridView) findViewById(R.id.grdImages);
btnSelect= (Button) findViewById(R.id.btnSelect);
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
#SuppressWarnings("deprecation")
Cursor imagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy);
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
this.count = imagecursor.getCount();
this.arrPath = new String[this.count];
ids = new int[count];
this.thumbnailsselection = new boolean[this.count];
for (int i = 0; i < this.count; i++) {
imagecursor.moveToPosition(i);
ids[i] = imagecursor.getInt(image_column_index);
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
arrPath[i] = imagecursor.getString(dataColumnIndex);
}
imageAdapter = new ImageAdapter();
grdImages.setAdapter(imageAdapter);
imagecursor.close();
btnSelect.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final int len = thumbnailsselection.length;
int cnt = 0;
String selectImages = "";
for (int i = 0; i < len; i++) {
if (thumbnailsselection[i]) {
cnt++;
selectImages = selectImages + arrPath[i] + "|";
}
}
if (cnt == 0) {
Toast.makeText(getApplicationContext(), "Please select at least one image", Toast.LENGTH_LONG).show();
} else {
Log.d("SelectedImages", selectImages);
Intent i = new Intent();
i.putExtra("data", selectImages);
setResult(Activity.RESULT_OK, i);
finish();
}
}
});
}
#Override
public void onBackPressed() {
setResult(Activity.RESULT_CANCELED);
super.onBackPressed();
}
/**
* Class method
*/
/**
* This method used to set bitmap.
* #param iv represented ImageView
* #param id represented id
*/
private void setBitmap(final ImageView iv, final int id) {
new AsyncTask<Void, Void, Bitmap>() {
#Override
protected Bitmap doInBackground(Void... params) {
return MediaStore.Images.Thumbnails.getThumbnail(getApplicationContext().getContentResolver(), id, MediaStore.Images.Thumbnails.MICRO_KIND, null);
}
#Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
iv.setImageBitmap(result);
}
}.execute();
}
/**
* List adapter
* #author tasol
*/
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ImageAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.custom_gallery_item, null);
holder.imgThumb = (ImageView) convertView.findViewById(R.id.imgThumb);
holder.chkImage = (CheckBox) convertView.findViewById(R.id.chkImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.chkImage.setId(position);
holder.imgThumb.setId(position);
holder.chkImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
int id = cb.getId();
if (thumbnailsselection[id]) {
cb.setChecked(false);
thumbnailsselection[id] = false;
} else {
cb.setChecked(true);
thumbnailsselection[id] = true;
}
}
});
holder.imgThumb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int id = holder.chkImage.getId();
if (thumbnailsselection[id]) {
holder.chkImage.setChecked(false);
thumbnailsselection[id] = false;
} else {
holder.chkImage.setChecked(true);
thumbnailsselection[id] = true;
}
}
});
try {
setBitmap(holder.imgThumb, ids[position]);
} catch (Throwable e) {
}
holder.chkImage.setChecked(thumbnailsselection[position]);
holder.id = position;
return convertView;
}
}
/**
* Inner class
* #author tasol
*/
class ViewHolder {
ImageView imgThumb;
CheckBox chkImage;
int id;
}
}
Now the result based on selection goes to main activity where I need to display all the selected images in a listview.
This is what I'm trying to do
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if(requestCode == PICK_IMAGE_MULTIPLE){
imagesPathList = new ArrayList<String>();
String[] imagesPath = data.getStringExtra("data").split("\\|");
try{
lnrImages.removeAllViews();
}catch (Throwable e){
e.printStackTrace();
}
for (int i=0;i<imagesPath.length;i++){
//Toast.makeText(MainActivity.this,String.valueOf(imagesPath.length),Toast.LENGTH_LONG).show();
imagesPathList.add(imagesPath[i]);
//Toast.makeText(MainActivity.this,imagesPathList.toString(),Toast.LENGTH_LONG).show();
yourbitmap = BitmapFactory.decodeFile(imagesPath[i]);
ImageView imageView = new ImageView(this);
imageView.setImageBitmap(yourbitmap);
imageView.setAdjustViewBounds(true);
//lnrImages.addView(imageView);
Uri uri = (Uri.fromFile(new File(imagesPath[i])));
filename = uri.getLastPathSegment();
//Toast.makeText(MainActivity.this,filename,Toast.LENGTH_LONG).show();
Uri selectedImage = data.getData();
encodedImage= bitmapToBase64(yourbitmap);
//Toast.makeText(MainActivity.this,encodedImage,Toast.LENGTH_LONG).show();
myStringList.add(filename);
myStringArray = myStringList.toArray();
lv.setAdapter(null);
Toast.makeText(MainActivity.this,(String)myStringArray[i],Toast.LENGTH_LONG).show();
arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
myStringList );
lv.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
}
}
}
}
Now my problem is:
1) If I select one single image it's showing me the toast with the filename but not adding it to the listview
2)If I select two images its adding to the list view as normal
3) If I select more than two its appending to the old list(ie..adding the newly selected images to the previously selected images) though I have set the adapter to 'NULL'
4) Selecting large number of images takes time consumption to adding it to the listview crashing the app due to memory.
So can anyone suggest me the best way of handling this ?
Thanks in Advance!

There are multiple problems in your code. As in Java everything is reference, so instead of creating the adapter again and again, you should only set the new list to the adapter and then should call notifyDataSetChanged. For example, your ImageAdapter should be like this
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<String> imagesList = new ArrayList<>();
public void updateImagesList(ArrayList<Strinf> newImages) {
if(newImages != null && newImages.size() > 0) {
this.imagesList.clear();
this.imagesList.addAll(newImages);
}
}
}
and then you should create global Adapter variable which we will be using for future list updates
and in your onActivityResult, you should populate the strings list in a loop and then after the loop you should update the adapter list as
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == PICK_IMAGE_MULTIPLE) {
for (int i = 0; i < imagesPath.length; i++) {
//Toast.makeText(MainActivity.this,String.valueOf(imagesPath.length),Toast.LENGTH_LONG).show();
imagesPathList.add(imagesPath[i]);
//Toast.makeText(MainActivity.this,imagesPathList.toString(),Toast.LENGTH_LONG).show();
yourbitmap = BitmapFactory.decodeFile(imagesPath[i]);
ImageView imageView = new ImageView(this);
imageView.setImageBitmap(yourbitmap);
imageView.setAdjustViewBounds(true);
//lnrImages.addView(imageView);
Uri uri = (Uri.fromFile(new File(imagesPath[i])));
filename = uri.getLastPathSegment();
//Toast.makeText(MainActivity.this,filename,Toast.LENGTH_LONG).show();
Uri selectedImage = data.getData();
encodedImage = bitmapToBase64(yourbitmap);
//Toast.makeText(MainActivity.this,encodedImage,Toast.LENGTH_LONG).show();
myStringList.add(filename);
Toast.makeText(MainActivity.this, (String) myStringArray[i], Toast.LENGTH_LONG).show();
}
arrayAdapter.updateImages(myStringList);
arrayAdapter.notifyDataSetChanged();
}
}
}
Apart the above two changes, there is great performance impact because you are all doing I/O operations on Ui Thread as you are decoding Bitmap files on Main thread which is not the good approach as It will cause OutOfMemoryError as well as It will slow down your application. You should do bitmap operations or displaying using Universal Image Loader https://github.com/nostra13/Android-Universal-Image-Loader
Hope this helps.

Related

android from app select multiple photos, but not gallery images

From the many previous posts I can now have my app launch the Intent which will allow the users to select multiple images.
But the Intent presents both Gallery Images and Photos when I want Photos only.
The Gallery Images are of no use and merely confuse the user.
So my question is how to limit the images presented to the user for selection to Only the Photos and Not the Gallery.
The code I am currently using is as follows:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.galleryimagepicker);
lnrImages = (LinearLayout)findViewById(R.id.lnrImages);
btnAddPhotos = (Button)findViewById(R.id.btnAddPhotos);
btnAddPhotos.setOnClickListener(this);
btnSaveImages = (Button)findViewById(R.id.btnSaveImages);
btnSaveImages.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnAddPhotos:
/*Intent intent = new Intent(GalleryImagePicker.this,CustomPhotoGalleryActivity.class);
startActivityForResult(intent,PICK_IMAGE_MULTIPLE);*/
Intent intent = new Intent(GalleryImagePicker.this,CustomPhotoGalleryActivity.class);
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent,PICK_IMAGE_MULTIPLE);
break;
case R.id.btnSaveImages:
if(imagesPathList != null)
{
if(imagesPathList.size() >= 1) {
//Toast.makeText(GalleryImagePicker.this, imagesPathList.size() + " no of images are selected", Toast.LENGTH_SHORT).show();
String[] stringArray = imagesPathList.toArray(new String[0]);
for (int i = 0; i < stringArray.length; i++){
String ThisImgPath = stringArray[i];
String PicName = ThisImgPath;
if (SystemIOFile.exists(PicName)) {
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 4;
Bitmap bm = BitmapFactory.decodeFile(ThisImgPath, bmOptions);
Uri ThisUri = getImageUri(cntxt, bm);
bm = decodeBitmap(cntxt, ThisUri, bmOptions.inSampleSize);
Boolean HOLD = true;
}
Boolean HOLD = true;
}
}else{
Toast.makeText(GalleryImagePicker.this, imagesPathList.size() + " no of image are selected", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(GalleryImagePicker.this," no images are selected", Toast.LENGTH_SHORT).show();
}
break;
}
}
EDIT: CustomPhotoGalleryActivity code:
public class CustomPhotoGalleryActivity extends Activity {
private GridView grdImages;
private Button btnSelect;
private ImageAdapter imageAdapter;
private String[] arrPath;
private boolean[] thumbnailsselection;
private int ids[];
private int count;
/**
* Overrides methods
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_gallery);
grdImages = (GridView) findViewById(R.id.grdImages);
btnSelect = (Button) findViewById(R.id.btnSelect);
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
#SuppressWarnings("deprecation")
Cursor imagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy);
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
this.count = imagecursor.getCount();
this.arrPath = new String[this.count];
ids = new int[count];
this.thumbnailsselection = new boolean[this.count];
for (int i = 0; i < this.count; i++) {
imagecursor.moveToPosition(i);
ids[i] = imagecursor.getInt(image_column_index);
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
arrPath[i] = imagecursor.getString(dataColumnIndex);
}
imageAdapter = new ImageAdapter();
grdImages.setAdapter(imageAdapter);
imagecursor.close();
btnSelect.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final int len = thumbnailsselection.length;
int cnt = 0;
String selectImages = "";
for (int i = 0; i < len; i++) {
if (thumbnailsselection[i]) {
cnt++;
selectImages = selectImages + arrPath[i] + "|";
}
}
if (cnt == 0) {
Toast.makeText(getApplicationContext(), "Please select at least one image", Toast.LENGTH_LONG).show();
} else {
Log.d("SelectedImages", selectImages);
Intent i = new Intent();
i.putExtra("data", selectImages);
setResult(Activity.RESULT_OK, i);
finish();
}
}
});
}
#Override
public void onBackPressed() {
setResult(Activity.RESULT_CANCELED);
super.onBackPressed();
}
/**
* Class method
*/
/**
* This method used to set bitmap.
* #param iv represented ImageView
* #param id represented id
*/
private void setBitmap(final ImageView iv, final int id) {
new AsyncTask<Void, Void, Bitmap>() {
#Override
protected Bitmap doInBackground(Void... params) {
return MediaStore.Images.Thumbnails.getThumbnail(getApplicationContext().getContentResolver(), id, MediaStore.Images.Thumbnails.MICRO_KIND, null);
}
#Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
iv.setImageBitmap(result);
}
}.execute();
}
/**
* List adapter
* #author tasol
*/
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ImageAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.custom_gallery_item, null);
holder.imgThumb = (ImageView) convertView.findViewById(R.id.imgThumb);
holder.chkImage = (CheckBox) convertView.findViewById(R.id.chkImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.chkImage.setId(position);
holder.imgThumb.setId(position);
holder.chkImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
int id = cb.getId();
if (thumbnailsselection[id]) {
cb.setChecked(false);
thumbnailsselection[id] = false;
} else {
cb.setChecked(true);
thumbnailsselection[id] = true;
}
}
});
holder.imgThumb.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int id = holder.chkImage.getId();
if (thumbnailsselection[id]) {
holder.chkImage.setChecked(false);
thumbnailsselection[id] = false;
} else {
holder.chkImage.setChecked(true);
thumbnailsselection[id] = true;
}
}
});
try {
setBitmap(holder.imgThumb, ids[position]);
} catch (Throwable e) {
}
holder.chkImage.setChecked(thumbnailsselection[position]);
holder.id = position;
return convertView;
}
}
/**
* Inner class
* #author tasol
*/
class ViewHolder {
ImageView imgThumb;
CheckBox chkImage;
int id;
}
}
Thanks

Performance Issue while loading all images from android device

I have seen other SO questions and already tried their solution's but didn't help so i am myself asking
I am facing a problem while loading all images from device in a 'GridView', I am using 'MediaStore' to fetch images and i am quite successfull in that and also i have divided all the images folder wise
My phone is having 1 GB ram so i am able load images without difficulties,
But the problem i am facing is that when i use it another device with less then my phone's ram it takes more time to load the images and sometimes it just don't load any image i have already tried using 'AsyncTask' but its not helping.
Below is what i have done so far:
public class DirectoryImagesActivity extends Activity {
private ImageAdapter imageAdapter;
public static String[] arrPath;
private boolean[] thumbnailsselection;
private int ids[];
private int count;
Bundle dirBundle;
String dirName;
public static Bitmap[] imageThumb;
ArrayList<Bitmap> dirBitmap = new ArrayList<Bitmap>();
/**
* Overrides methods
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photo_gallery);
dirBundle = getIntent().getExtras();
dirName = dirBundle.getString("dir_name");
String[] projectionFolder = { MediaStore.Images.Media._ID,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
MediaStore.Images.Media.DATA };
String selection = MediaStore.Images.Media.BUCKET_DISPLAY_NAME + " = ?";
String[] selectionArgs = new String[] { dirName };
#SuppressWarnings("deprecation")
Cursor mImageCursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projectionFolder,
selection, selectionArgs, null);
int image_column_index = mImageCursor
.getColumnIndex(MediaStore.Images.Media._ID);
this.count = mImageCursor.getCount();
arrPath = new String[this.count];
imageThumb = new Bitmap[this.count];
ids = new int[count];
this.thumbnailsselection = new boolean[this.count];
for (int i = 0; i < this.count; i++) {
mImageCursor.moveToPosition(i);
ids[i] = mImageCursor.getInt(image_column_index);
int dataColumnIndex = mImageCursor
.getColumnIndex(MediaStore.Images.Media.DATA);
arrPath[i] = mImageCursor.getString(dataColumnIndex);
int id = mImageCursor.getInt(mImageCursor
.getColumnIndex(MediaStore.MediaColumns._ID));
dirBitmap.add(MediaStore.Images.Thumbnails.getThumbnail(
getContentResolver(), id,
MediaStore.Images.Thumbnails.MICRO_KIND, null));
}
GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
imageAdapter = new ImageAdapter(this);
imagegrid.setAdapter(imageAdapter);
// imagecursor.close();
final Button selectBtn = (Button) findViewById(R.id.selectBtn);
selectBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final int len = thumbnailsselection.length;
int cnt = 0;
String selectImages = "";
for (int i = 0; i < len; i++) {
if (thumbnailsselection[i]) {
cnt++;
selectImages = selectImages + arrPath[i] + "|";
}
}
if (cnt == 0) {
Toast.makeText(getApplicationContext(),
"Please select at least one image",
Toast.LENGTH_LONG).show();
} else {
Log.d("SelectedImages", selectImages);
Intent i = new Intent();
i.putExtra("data", selectImages);
setResult(Activity.RESULT_OK, i);
finish();
}
}
});
}
#Override
public void onBackPressed() {
setResult(Activity.RESULT_CANCELED);
super.onBackPressed();
}
/**
* Class method
*/
/**
* This method used to set bitmap.
*
* #param iv
* represented ImageView
* #param id
* represented id
*/
public void setBitmap(final ImageView iv, final Bitmap id) {
new AsyncTask<Void, Void, Bitmap>() {
#Override
protected Bitmap doInBackground(Void... params) {
return id;
}
#Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
iv.setImageBitmap(result);
}
}.execute();
}
/**
* List adapter
*
* #author tasol
*/
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ImageLoader imageLoader;
Context context;
public ImageAdapter(Context context) {
this.context = context;
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = new ImageLoader(context);
}
public int getCount() {
return dirBitmap.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView,
ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.layout_improved_grid,
null);
holder.imageview = (SquareImageView) convertView
.findViewById(R.id.thumbImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.imageview.setId(position);
// holder.imageview.setImageBitmap(imageThumb[position]);
holder.imageview.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(DirectoryImagesActivity.this,
"Item Clicked", Toast.LENGTH_LONG).show();
Intent i = new Intent(getApplicationContext(),
FullImageActivity.class); // passing array index
i.putExtra("id", position);
startActivity(i);
}
});
try {
setBitmap(holder.imageview, dirBitmap.get(position));
} catch (Throwable e) {
}
holder.id = position;
return convertView;
}
}
/**
* Inner class
*
* #author tasol
*/
class ViewHolder {
SquareImageView imageview;
int id;
}
public static Bitmap convertBitmap(String path) {
Bitmap bitmap = null;
BitmapFactory.Options bfOptions = new BitmapFactory.Options();
bfOptions.inDither = false; // Disable Dithering mode
bfOptions.inPurgeable = true; // Tell to gc that whether it needs free
// memory, the Bitmap can be cleared
bfOptions.inInputShareable = true; // Which kind of reference will be
// used to recover the Bitmap data
// after being clear, when it will
// be used in the future
bfOptions.inTempStorage = new byte[32 * 1024];
File file = new File(path);
FileInputStream fs = null;
try {
fs = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
if (fs != null) {
bitmap = BitmapFactory.decodeFileDescriptor(fs.getFD(), null,
bfOptions);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fs != null) {
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return bitmap;
}
}
If anybody can help me with this thank you in advance
Try this is working solution
1)PhotoGalaryActivity.java
public class PhotoGalaryActivity extends Activity {
private ImageAdapter imageAdapter;
private String[] arrPath;
private boolean[] thumbnailsselection;
private int ids[];
private int count;
/**
* Overrides methods
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photo_gallery);
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
#SuppressWarnings("deprecation")
Cursor imagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy);
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
this.count = imagecursor.getCount();
this.arrPath = new String[this.count];
ids = new int[count];
this.thumbnailsselection = new boolean[this.count];
for (int i = 0; i < this.count; i++) {
imagecursor.moveToPosition(i);
ids[i] = imagecursor.getInt(image_column_index);
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
arrPath[i] = imagecursor.getString(dataColumnIndex);
}
GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
imageAdapter = new ImageAdapter();
imagegrid.setAdapter(imageAdapter);
imagecursor.close();
final Button selectBtn = (Button) findViewById(R.id.selectBtn);
selectBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final int len = thumbnailsselection.length;
int cnt = 0;
String selectImages = "";
for (int i = 0; i < len; i++) {
if (thumbnailsselection[i]) {
cnt++;
selectImages = selectImages + arrPath[i] + "|";
}
}
if (cnt == 0) {
Toast.makeText(getApplicationContext(), "Please select at least one image", Toast.LENGTH_LONG).show();
} else {
Log.d("SelectedImages", selectImages);
Intent i = new Intent();
i.putExtra("data", selectImages);
setResult(Activity.RESULT_OK, i);
finish();
}
}
});
}
#Override
public void onBackPressed() {
setResult(Activity.RESULT_CANCELED);
super.onBackPressed();
}
/**
* Class method
*/
/**
* This method used to set bitmap.
* #param iv represented ImageView
* #param id represented id
*/
private void setBitmap(final ImageView iv, final int id) {
new AsyncTask<Void, Void, Bitmap>() {
#Override
protected Bitmap doInBackground(Void... params) {
return MediaStore.Images.Thumbnails.getThumbnail(getApplicationContext().getContentResolver(), id, MediaStore.Images.Thumbnails.MICRO_KIND, null);
}
#Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
iv.setImageBitmap(result);
}
}.execute();
}
/**
* List adapter
* #author tasol
*/
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ImageAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.photo_gallery_item, null);
holder.imageview = (ImageView) convertView.findViewById(R.id.thumbImage);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.itemCheckBox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.checkBox.setId(position);
holder.imageview.setId(position);
holder.checkBox.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
int id = cb.getId();
if (thumbnailsselection[id]) {
cb.setChecked(false);
thumbnailsselection[id] = false;
} else {
cb.setChecked(true);
thumbnailsselection[id] = true;
}
}
});
holder.imageview.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int id = holder.checkBox.getId();
if (thumbnailsselection[id]) {
holder.checkBox.setChecked(false);
thumbnailsselection[id] = false;
} else {
holder.checkBox.setChecked(true);
thumbnailsselection[id] = true;
}
}
});
try {
setBitmap(holder.imageview, ids[position]);
} catch (Throwable e) {
}
holder.checkBox.setChecked(thumbnailsselection[position]);
holder.id = position;
return convertView;
}
}
/**
* Inner class
* #author tasol
*/
class ViewHolder {
ImageView imageview;
CheckBox checkBox;
int id;
}
}
2)photo_gallery.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView android:id="#+id/thumbImage" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_centerInParent="true"
/>
<CheckBox android:id="#+id/itemCheckBox" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
3)photo_gallery_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView android:id="#+id/thumbImage" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_centerInParent="true"
/>
<CheckBox android:id="#+id/itemCheckBox" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
As there are many images you are loading you have to implement lazy loading in-order to increase app's performance for other low memory devices.
You can refer this library https://github.com/nostra13/Android-Universal-Image-Loader.
I hope this helps.

Image From sd card to galleryview

In my app i want to get the Images from SD card to a specific position and i need to display in gallery,
The code i used is attaching
private Gallery gallery;
private ImageView imgView;
int position;
private byte[] data = { };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview);
try{
String filepath = "/sdcard/data/Crak/";
File imagefile = new File(filepath +"abcd.jpg" );
FileInputStream fis = new FileInputStream(imagefile);
Bitmap bi = BitmapFactory.decodeStream(fis);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
data = baos.toByteArray();
System.out.println("cnvrtn");
}
catch(Exception e) {
e.printStackTrace();
}
final Bitmap bitmapimage = BitmapFactory.decodeByteArray(data, 0, data.length);
position = 0;
imgView = (ImageView) findViewById(R.id.ImageView01);
imgView.setImageBitmap(bitmapimage);
gallery = (Gallery) findViewById(R.id.examplegallery);
gallery.setAdapter(new AddImgAdp(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,
long id) {
imgView.setImageBitmap(bitmapimage);
DisplayImage.this.position = position;
}
});
System.out.println("Enter the activity//////////");
imgView.setOnClickListener(new View.OnClickListener() {
#SuppressWarnings("deprecation")
#Override
public void onClick(View paramView) {
// TODO Auto-generated method stub
}
});
}
public class AddImgAdp extends BaseAdapter {
int GalItemBg;
private Context cont;
#SuppressWarnings("null")
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 data.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 = new ImageView(cont);
final Bitmap bitmapimage = BitmapFactory.decodeByteArray(data, 0, data.length);
imgView.setImageBitmap(bitmapimage);;
imgView.setLayoutParams(new Gallery.LayoutParams(100, 100));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setBackgroundResource(GalItemBg);
return imgView;
}
}
The problems are
1)How can i get all image in the specific folder in SD card
2) GalleryView is indefiniite or a long one without end
here is the UPDATED code to get images from specific path...
package com.example.gall;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import com.example.gall.R;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
public class gall extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this, ReadSDCard()));
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,
View v, int position, long id) {
}
});
}
private List<String> ReadSDCard()
{
List<String> tFileList = new ArrayList<String>();
//It have to be matched with the directory in SDCard
File f = new File("/android/sdcard2/");
File[] files=f.listFiles();
for(int i=0; i<files.length; i++)
{
File file = files[i];
/*It's assumed that all file in the path are in supported type*/
tFileList.add(file.getPath());
}
return tFileList;
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private List<String> FileList;
public ImageAdapter(Context c, List<String> fList) {
mContext = c;
FileList = fList;
TypedArray a = obtainStyledAttributes(R.styleable.gall);
mGalleryItemBackground = a.getResourceId(
R.styleable.gall_android_galleryItemBackground,0);
a.recycle();
}
public int getCount() {
return FileList.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView,
ViewGroup parent) {
ImageView i = new ImageView(mContext);
Bitmap bm = BitmapFactory.decodeFile(
FileList.get(position).toString());
i.setImageBitmap(bm);
i.setLayoutParams(new Gallery.LayoutParams(150, 100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
}
public TypedArray obtainStyledAttributes(int theme) {
// TODO Auto-generated method stub
return null;
}
}
Getting all the images will cause a memory overflow the best way is to keep a list of paths in string form and supply that to the gallery. If you are making a custom gallery you will have to create bitmaps and garbage collect them.
This is a good way to access sd-card:
String path = Environment.getExternalStorageDirectory().getPath()+ "/yourFolder/"
This is how to get the list
File directory = new File(path);
if(directory.isDirectory)
{
if(directory.list().length > 0 /*&& check if its an img etc*/)
{
for(String s: directory.list())
{
Log.d(TAG, "adding " + s);
fileNames.add(s);
}
}
}
you should make sure to check sd-card is loaded
//YOUR CODE HERE...
String[] projection = new String[]{
MediaStore.Images.Media._ID,
MediaStore.Images.Media.DATA, // add DATA column
MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
MediaStore.Images.Media.DATE_TAKEN,
MediaStore.Images.Media.TITLE,
};
// Get the base URI for the People table in the Contacts content provider.
Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Log.i("URI", images.toString());
// Make the query.
Cursor cur = managedQuery(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Which rows to return (all rows)
null, // Selection arguments (none)
MediaStore.Images.Thumbnails.IMAGE_ID // Ordering
);
Log.i("ListingImages"," query count="+cur.getCount());
if (cur.moveToFirst()) {
String bucket;
String date;
String name;
int bucketColumn = cur.getColumnIndex(
MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
int dateColumn = cur.getColumnIndex(
MediaStore.Images.Media.DATE_TAKEN);
int nameColumn = cur.getColumnIndex(
MediaStore.Images.Media.TITLE);
// Get the field values
bucket = cur.getString(bucketColumn);
date = cur.getString(dateColumn);
name = cur.getString(nameColumn);
int columnIndex = cur.getColumnIndex(MediaStore.Images.Media.DATA);
String picPath = cur.getString(columnIndex);
imageView.setImageBitmap(BitmapFactory.decodeFile(picPath));
// Do something with the values.
Log.i("ListingImages", " bucket=" + bucket
+ " name_taken=" + name);
}
}

Slow scrolling when populating gridview of images with AsyncTask

good day, i am having a bit of a problem here. i am using and async task to display images from external or internal storage.Now it works but the problem is, it is very slow and slightly jerky in scrolling. I have no idea why? please any solution or an idea how to do this.
import java.io.IOException;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Debug;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class Wallpaper extends Activity implements OnItemClickListener{
/*Instance variables*/
private GridView grid;
private ImageAdapter imageAdapter;
private Display display;
Cursor cursor;
boolean inInternalStorage = false;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.wallpaper_images);
display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
setupViews();
setProgressBarIndeterminateVisibility(true);
loadImages();
}
#Override
protected void onStop(){
super.onStop();
}
/*methods called from AsyncTask as appropriate when its querying and processing the images from the MediaStore*/
private void loadImages() {
final Object data = getLastNonConfigurationInstance();
if(data == null){
new LoadImagesFromSDCard().execute();
}else {
final LoadedImage[] photos = (LoadedImage[])data;
if(photos.length == 0){
new LoadImagesFromSDCard().execute();
}
for(LoadedImage photo:photos){
addImage(photo);
}
}
}
private void addImage(LoadedImage... value) {
for(LoadedImage photo: value){
imageAdapter.addPhotos(photo);
imageAdapter.notifyDataSetChanged();
}
}
private void setupViews() {
grid = (GridView)findViewById(R.id.gridview);
grid.setNumColumns(display.getWidth()/95);
grid.setClipToPadding(false);
imageAdapter = new ImageAdapter(getApplicationContext());
grid.setAdapter(imageAdapter);
grid.setOnItemClickListener(this);
}
protected void onDestroy() {
super.onDestroy();
final GridView gridview = grid;
final int count = grid.getChildCount();
ImageView v = null;
for (int i = 0; i < count; i++) {
v = (ImageView) grid.getChildAt(i);
((BitmapDrawable) v.getDrawable()).setCallback(null);
}
unbindDrawables(findViewById(R.id.gridview));
System.gc();
}
/*public Object onRetainNonConfigurationInstance(){
final GridView gridview = grid;
final int count = grid.getChildCount();
final LoadedImage[] list = new LoadedImage[count];
for(int i = 0; i < count; i++){
final ImageView v = (ImageView)grid.getChildAt(i);
list[i] = new LoadedImage(((BitmapDrawable) v.getDrawable()).getBitmap());
}
return list;
}*/
/*utility method called that prevents screen from crashing when screen orientation changes*/
private void unbindDrawables(View view){
if(view.getBackground() != null){
view.getBackground().setCallback(null);
}
if(view instanceof ViewGroup){
for(int i = 0; i < ((ViewGroup) view).getChildCount(); i++){
unbindDrawables(((ViewGroup)view).getChildAt(i));
}
try{
((ViewGroup)view).removeAllViews();
}catch(Exception e){
e.printStackTrace();
}
}
}
/*AsyncTask thats queries the MediaStore for images and creates thumbnail images from bitmaps*/
class LoadImagesFromSDCard extends AsyncTask<Object, LoadedImage, Object> {
#Override
protected Object doInBackground(Object... params) {
Cursor cursor;
Bitmap bitmap = null;
Bitmap newbitmap = null;
Uri uri = null;
String [] img = {MediaStore.Images.Media._ID};
String state = Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state)){
cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, img,
MediaStore.Images.Media.DATA + " like ? " , new String[]{ "%dcim%"}, null);
} else {
cursor = getContentResolver().query(MediaStore.Images.Media.INTERNAL_CONTENT_URI, img, null, null, null);
inInternalStorage = true;
}
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
int size = cursor.getCount();
if(size == 0){
Toast.makeText(getApplicationContext(), "There are no Images on the sdcard", Toast.LENGTH_SHORT).show();
}else {
}
for(int i = 0; i < size; i++){
cursor.moveToPosition(i);
int ImageId = cursor.getInt(column_index);
if(inInternalStorage == true){
uri = Uri.withAppendedPath(MediaStore.Images.Media.INTERNAL_CONTENT_URI, "" + ImageId);
}else {
uri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + ImageId);
}
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize=4;
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);
if(bitmap != null){
newbitmap = Bitmap.createScaledBitmap(bitmap, 100, 100, true);
bitmap.recycle();
}
if(newbitmap != null){
publishProgress(new LoadedImage(newbitmap));
}
}catch(IOException e){
}
}
cursor.close();
return null;
}
#Override
public void onProgressUpdate(LoadedImage... value){
addImage(value);
}
#Override
protected void onPostExecute(Object result) {
setProgressBarIndeterminateVisibility(false);
}
}
private static class LoadedImage {
Bitmap mBitmap;
LoadedImage(Bitmap bitmap) {
mBitmap = bitmap;
}
public Bitmap getBitmap() {
return mBitmap;
}
}
/*Image Adapter to populate grid view of images*/
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<LoadedImage> photos = new ArrayList<LoadedImage>();
public ImageAdapter(Context context){
this.mContext = context;
}
public void addPhotos(LoadedImage photo){
photos.add(photo);
}
#Override
public int getCount() {
return photos.size();
}
#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) {
System.gc();
ImageView image;
ViewHolder holder;
if(convertView == null){
LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.image_list,null);
holder = new ViewHolder();
holder.image = (ImageView)convertView.findViewById(R.id.image_list_id);
convertView.setTag(holder);
} else{
holder = (ViewHolder)convertView.getTag();
}
holder.image.setLayoutParams(new GridView.LayoutParams(100, 100));
holder.image.setImageBitmap(photos.get(position).getBitmap());
return convertView;
}
}
static class ViewHolder {
ImageView image;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor cursor = null;
int image_column_index = 0;
String[] proj = {MediaStore.Images.Media.DATA};
String state = Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state)){
cursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,proj, MediaStore.Images.Media.DATA + " like ? ", new String[]{"%dcim%"},null);
}else{
cursor = managedQuery(MediaStore.Images.Media.INTERNAL_CONTENT_URI,proj,null, null,null);
}
cursor.moveToPosition(position);
image_column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
String info = cursor.getString(image_column_index);
Intent imageviewer = new Intent(getApplicationContext(), ViewImage.class);
imageviewer.putExtra("pic_name", info);
startActivity(imageviewer);
}
}
i think what i am after is a way to allow doBackground() to run for a while before i call publishProgress(). just like how the gallery app in android does.
Any ideas on why this could be slow or how to solve this problem will be greatly appreciated as i have been stuck on how to improve the perfomance for a while now.
The biggest bottleneck will be accessing the images from the device (internal or external). Therefore, you'll want to have as many images in memory as possible (within reason of course). You can do this in a number of ways:
Load perhaps 18 images first (as you said, prebuffer before displaying). Check to make sure the number of images doesn't exceed whatever number you choose though.
Create a 'thumbs.db' type file which will store 100x100 px thumbnails of the bitmaps. This will allow much faster reading since you'd only need to read in one file then extract each bitmap. If the user clicks an image, then request it from the storage. This method requires more work, but will be able to load the thumbnails really fast. You'd probably have to design your own simple file headers, such as:
<file header (size of file, number of images)>
<image header (img id, size in bytes)>
<image bitmap data>
<image header (img id, size in bytes)>
<image bitmap data>
<image header (img id, size in bytes)>
<image bitmap data>
...

Android VeiwFlow load images from SdCard

I'm using an implementation of ViewFlow example for my application where I have to swipe images from SdCard. With the code I'm using I can swipe images, but it's showing only one and i'm trying to set the whole images from my SdCard specific folder in the ViewFlow, but that's the problem that I have. I'm getting the path to the image by an ID which I get from sqlite database and depending on that I want to add these images to the view. So this is the code which I'm using for now :
My Cards.class :
package com.stampii.stampii.cards;
import java.util.ArrayList;
import java.util.HashMap;
import com.stampii.stampii.R;
import com.stampii.stampii.comm.rpc.UserDatabaseHelper;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
public class Cards extends Activity {
public Cursor cursor;
int position;
int indexxx;
Bitmap b;
int objectId;
int cardsId;
ArrayList<HashMap<Integer, String>> imgpaths;
ArrayList<Integer> ids;
String path;
int mediaType=5001;
private ViewFlow viewFlow;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.circle_layout);
UserDatabaseHelper userDbHelper = new UserDatabaseHelper(this, null, 1);
userDbHelper.initialize(this);
imgpaths = new ArrayList<HashMap<Integer, String>>();
HashMap<Integer, String> hm = new HashMap<Integer, String>();
ids = new ArrayList<Integer>();
final int cardId = getIntent().getIntExtra("card_id",0);
Log.i("Card Id ","Card Id : "+cardId);
final int collId = getIntent().getIntExtra("collection_id",0);
Log.i("Collection Id ","Collection Id : "+collId);
position = getIntent().getIntExtra("position",0);
Log.i("position","position : "+position);
String cardSQL = "SELECT cm.cardId, cm.objectId "+
"FROM cardmedias AS cm "+
"INNER JOIN cards AS cd "+
"ON (cm.cardId = cd.objectId) "+
"WHERE cd.collectionId="+collId +" AND cm.mediaType="+mediaType;
Cursor cards = userDbHelper.executeSQLQuery(cardSQL);
if (cards.getCount() == 0) {
Log.i("", "No Image file");
cards.close();
} else if (cards.getCount() > 0) {
for (cards.move(0); cards.moveToNext(); cards.isAfterLast()) {
cardsId = Integer.parseInt(cards.getString(cards
.getColumnIndex("objectId")));
//Log.i("", "cards objectId : " + cardsId);
String path = Environment.getExternalStorageDirectory()
+ "/.Stampii/MediaCard/" + cardsId + ".png";
//Log.i("", "path : " + path);
ids.add(cardsId);
hm.put(cardsId, path);
}
}
String sql = "SELECT objectId FROM cardmedias WHERE cardId=" + cardId
+ " LIMIT 1";
Cursor cursor = userDbHelper.executeSQLQuery(sql);
if (cursor.getCount() == 0) {
Log.i("", "No Image file");
cursor.close();
} else if (cursor.getCount() > 0) {
cursor.moveToFirst();
objectId = Integer.parseInt(cursor.getString(cursor
.getColumnIndex("objectId")));
Log.i("", "objectId : " + objectId);
path = hm.get(objectId);
Log.i("","path : "+hm.get(objectId));
Log.i("","hm size : "+hm.size());
}
Button info = (Button) findViewById(R.id.info_button);
info.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Cards.this, SingleCardInfo.class);
intent.putExtra("card_id", cardId);
intent.putExtra("collection_id", collId);
startActivity(intent);
}
});
Button back = (Button) findViewById(R.id.back_button);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
final ArrayList<Bitmap> images = new ArrayList<Bitmap>();
for(int i=0;i<=2;i++){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[16*1024];
Bitmap b = BitmapFactory.decodeFile(hm.get(objectId), options);
Log.i("","path : "+hm.get(objectId));
images.add(b);
}
viewFlow = (ViewFlow) findViewById(R.id.viewflow);
viewFlow.setAdapter(new ImageAdapter(this, images),position);
ImageButton prevBtn = (ImageButton) findViewById(R.id.previous_button);
prevBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
indexxx = viewFlow.getSelectedItemPosition()-1;
if (indexxx>=0) {
viewFlow.setAdapter(new ImageAdapter(Cards.this, images),indexxx);
viewFlow.setSelectedItemPosition(indexxx);
Log.i("indexxx", "indexxx : " + indexxx);
}
}
});
ImageButton nextBtn = (ImageButton) findViewById(R.id.next_button);
nextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
indexxx = viewFlow.getSelectedItemPosition()+1;
if (indexxx<images.size()) {
viewFlow.setAdapter(new ImageAdapter(Cards.this, images),indexxx);
viewFlow.setSelectedItemPosition(indexxx);
Log.i("indexxx", "indexxx : " + indexxx);
}
}
});
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
viewFlow.onConfigurationChanged(newConfig);
}
}
And my ImageAdapter class :
package com.stampii.stampii.cards;
import java.util.ArrayList;
import com.stampii.stampii.R;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<Bitmap> ids = new ArrayList<Bitmap>();
//private Bitmap bitmap;
public ImageAdapter(Context context, ArrayList<Bitmap> images) {
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ids = images;
}
#Override
public int getCount() {
return ids.size();
}
#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) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.image_item, null);
}
((ImageView) convertView.findViewById(R.id.imgView)).setImageBitmap(ids.get(position));
return convertView;
}
}
So my question is how can I add all images from my SdCard folder to this ViewFlow example when I had the path to these images. I've tried to load all of them, but in some cases the images are too much and it's throwing me an outofmemoryException, that's why I think it's better just to set the paths to them. So any help or suggestions are welcomed!
Thanks in advance!
Here is code of ViewFlow.class : ViewFlow
You can try something like this :
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<String> ids = new ArrayList<String>();
//private Bitmap bitmap;
public ImageAdapter(Context context, ArrayList<String> images) {
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ids = images;
}
#Override
public int getCount() {
return ids.size();
}
#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) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.image_item, null);
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[8*1024];
Bitmap ops = BitmapFactory.decodeFile(ids.get(position), options);
((ImageView) convertView.findViewById(R.id.imgView)).setImageBitmap(ops);
return convertView;
}
}
And :
public class Cards extends Activity {
public Cursor cursor;
int position;
int indexxx;
Bitmap b;
int objectId;
int cardsId;
ArrayList<Integer> ids;
String path;
int mediaType=5001;
ArrayList<String> images;
int card;
private ViewFlow viewFlow;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.circle_layout);
UserDatabaseHelper userDbHelper = new UserDatabaseHelper(this, null, 1);
userDbHelper.initialize(this);
//imgpaths = new ArrayList<HashMap<Integer, String>>();
HashMap<Integer, String> hm = new HashMap<Integer, String>();
ids = new ArrayList<Integer>();
images = new ArrayList<String>();
final int cardId = getIntent().getIntExtra("card_id",0);
Log.i("Card Id ","Card Id : "+cardId);
final int collId = getIntent().getIntExtra("collection_id",0);
Log.i("Collection Id ","Collection Id : "+collId);
position = getIntent().getIntExtra("position",0);
Log.i("position","position : "+position);
String cardSQL = "SELECT cm.cardId, cm.objectId "+
"FROM cardmedias AS cm "+
"INNER JOIN cards AS cd "+
"ON (cm.cardId = cd.objectId) "+
"WHERE cd.collectionId="+collId +" AND cm.mediaType="+mediaType;
Cursor cards = userDbHelper.executeSQLQuery(cardSQL);
if (cards.getCount() == 0) {
Log.i("", "No Image file");
cards.close();
} else if (cards.getCount() > 0) {
for (cards.move(0); cards.moveToNext(); cards.isAfterLast()) {
cardsId = Integer.parseInt(cards.getString(cards
.getColumnIndex("objectId")));
card = Integer.parseInt(cards.getString(cards
.getColumnIndex("cardId")));
String path = Environment.getExternalStorageDirectory()
+ "/.Stampii/MediaCard/" + cardsId + ".png";
ids.add(card);
hm.put(cardsId, path);
path = hm.get(cardsId);
Log.i("","path : "+hm.get(cardsId));
Log.i("","hm size : "+hm.size());
images.add(path);
}
}
Button back = (Button) findViewById(R.id.back_button);
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
viewFlow = (ViewFlow) findViewById(R.id.viewflow);
viewFlow.setAdapter(new ImageAdapter(this, images),position);
ImageButton prevBtn = (ImageButton) findViewById(R.id.previous_button);
prevBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
indexxx = viewFlow.getSelectedItemPosition()-1;
if (indexxx>=0) {
viewFlow.setAdapter(new ImageAdapter(Cards.this, images),indexxx);
viewFlow.setSelectedItemPosition(indexxx);
Log.i("indexxx", "indexxx : " + indexxx);
}
}
});
ImageButton nextBtn = (ImageButton) findViewById(R.id.next_button);
nextBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
indexxx = viewFlow.getSelectedItemPosition()+1;
if (indexxx<images.size()) {
viewFlow.setAdapter(new ImageAdapter(Cards.this, images),indexxx);
viewFlow.setSelectedItemPosition(indexxx);
Log.i("indexxx", "indexxx : " + indexxx);
}
}
});
Button info = (Button) findViewById(R.id.info_button);
info.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Cards.this, SingleCardInfo.class);
intent.putExtra("card_id", ids.get(position));
intent.putExtra("collection_id", collId);
startActivity(intent);
}
});
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
viewFlow.onConfigurationChanged(newConfig);
}
}
That should do the trick!

Categories

Resources