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);
}
}
Related
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.
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>
...
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!
this is my gallery program,in which i am displaying the content with static files,how to modify this program to show the photos in the SDcard,if there are no photos or SD card give an alert
package DisplayViewsExample.com;
import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.content.res.TypedArray;
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.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class DisplayViewsExampleActivity extends Activity {
Integer[] imageIDs = {
R.drawable.sample_1,
R.drawable.sample_2,
R.drawable.sample_3
};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView parent,
View v, int position, long id)
{
/* Toast.makeText(getBaseContext(),
"pic" + (position + 1) + " selected",
Toast.LENGTH_SHORT).show();*/
ImageView imageView = (ImageView) findViewById(R.id.image1);
imageView.setImageResource(imageIDs[position]);
}
});
}
public class ImageAdapter extends BaseAdapter
{
private Context context;
private int itemBackground;
public ImageAdapter(Context c)
{
context = c;
//---setting the style---
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
itemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
//---returns the number of images---
public int getCount() {
return imageIDs.length;
}
//---returns the ID of an item---
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(imageIDs[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
}
You should set permission in your Manifest first:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Then, write the function which recursively fetches all your image files from SD:
final static String[] IMAGE_FORMATS = new String[] {
".JPEG", ".GIF", ".PNG", ".BMP"};
public static ArrayList<File> getImageFilesFromSD() {
File dir = Environment.getExternalStorageDirectory();
if (dir == null || !dir.exists())
return null;
ArrayList<File> images = getImageFilesFromDir(dir);
return images;
}
private static ArrayList<File> getImageFilesFromDir(File dir) {
File[] files = dir.listFiles();
ArrayList<File> result = new ArrayList<File>();
if (files != null)
for (File file : files) {
if (file.isDirectory()) {
result.addAll(getImageFilesFromDir(file));
} else {
final String fileName = file.getName();
final String fileNameLwr = fileName.toLowerCase();
// filtering images by extension
boolean contains = false;
for (int i = 0; i < IMAGE_FORMATS.length; i++) {
final String formatLwr = IMAGE_FORMATS[i].toLowerCase();
if (fileNameLwr.endsWith(formatLwr)) {
contains = true;
break;
}
}
if (contains) {
result.add(file);
}
}
}
return result;
}
After that in your list adapter:
imageView.setImageURI(Uri.fromFile(file));
where file is the element of ArrayList returned by getImageFilesFromSD()function.
Good luck.
The
Environment.getExternalStoragePublicDirectory(String type)
function gives you access to the external extorage. There are even contants for the most usual standard folder names.
Don't forget to put the permission to access it on the manifest.
I am new to coding and never worked with it before! We are working with Android and Java eclipse, it's a school project. Now I am working with a band application were I am supposed to create the gallery. This I managed, but to create a function were the user can set an image to a wallpaper, I couldn't. I need some help with this! I believe I found good code, but I don't know how to implement it in my work.
Does anyone know how I should connect these to each other? Really thankful for help,
Sandra
My Gallery code:
package com.Lavin;
import com.Lavin.R;
import java.io.IOException;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.WallpaperManager;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
public class Lavin extends Activity implements
AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
mSwitcher.setFactory(this);
mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView parent, View v, int position, long id) {
mSwitcher.setImageResource(mImageIds[position]);
}
public void onNothingSelected(AdapterView parent) {
}
public View makeView() {
ImageView i = new ImageView(this);
i.setBackgroundColor(0xFF000000);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
return i;
}
private ImageSwitcher mSwitcher;
public class ImageAdapter extends BaseAdapter {
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.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 i = new ImageView(mContext);
i.setImageResource(mThumbIds[position]);
i.setAdjustViewBounds(true);
i.setLayoutParams(new Gallery.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
i.setBackgroundResource(R.drawable.picture_frame);
return i;
}
private Context mContext;
}
private Integer[] mThumbIds = {
R.drawable.lavin_thumb_0, R.drawable.lavin_thumb_1,
R.drawable.lavin_thumb_2, R.drawable.lavin_thumb_3,
R.drawable.lavin_thumb_4, R.drawable.lavin_thumb_5,
R.drawable.lavin_thumb_6, R.drawable.lavin_thumb_7,
R.drawable.lavin_thumb_8, R.drawable.lavin_thumb_9,
R.drawable.lavin_thumb_10, R.drawable.lavin_thumb_11,
R.drawable.lavin_thumb_12};
private Integer[] mImageIds = {
R.drawable.lavin_0, R.drawable.lavin_1, R.drawable.lavin_2,
R.drawable.lavin_3, R.drawable.lavin_4, R.drawable.lavin_5,
R.drawable.lavin_6, R.drawable.lavin_7, R.drawable.lavin_8,
R.drawable.lavin_9, R.drawable.lavin_10,
R.drawable.lavin_11, R.drawable.lavin_12};
}
My Wallpaper code:
package com.Lavin;
import android.app.Activity;
import android.os.Bundle;
import java.io.IOException;
import android.app.Activity;
import android.app.WallpaperManager;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class Wallpaper extends Activity {
final static private int[] mColors =
{Color.BLUE, Color.GREEN, Color.RED, Color.LTGRAY, Color.MAGENTA, Color.CYAN,
Color.YELLOW, Color.WHITE};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
final Drawable wallpaperDrawable = wallpaperManager.getDrawable();
final ImageView imageView = (ImageView) findViewById(R.id.imageview);
imageView.setDrawingCacheEnabled(true);
imageView.setImageDrawable(wallpaperDrawable);
Button randomize = (Button) findViewById(R.id.randomize);
randomize.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
int mColor = (int) Math.floor(Math.random() * mColors.length);
wallpaperDrawable.setColorFilter(mColors[mColor], PorterDuff.Mode.MULTIPLY);
imageView.setImageDrawable(wallpaperDrawable);
imageView.invalidate();
}
});
Button setWallpaper = (Button) findViewById(R.id.setwallpaper);
setWallpaper.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
try {
wallpaperManager.setBitmap(imageView.getDrawingCache());
finish();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
public class LoadImagesFromSDCardActivity extends Activity implements
OnItemClickListener {
/**
* Grid view holding the images.
*/
private GridView sdcardImages;
/**
* Image adapter for the grid view.
*/
private ImageAdapter imageAdapter;
/**
* Display used for getting the width of the screen.
*/
private Display display;
/**
* Creates the content view, sets up the grid, the adapter, and the click listener.
*
* #see android.app.Activity#onCreate(android.os.Bundle)
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Request progress bar
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.sdcard);
display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
setupViews();
setProgressBarIndeterminateVisibility(true);
loadImages();
}
/**
* Free up bitmap related resources.
*/
protected void onDestroy() {
super.onDestroy();
final GridView grid = sdcardImages;
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);
}
}
/**
* Setup the grid view.
*/
private void setupViews() {
sdcardImages = (GridView) findViewById(R.id.sdcard);
sdcardImages.setNumColumns(display.getWidth()/95);
sdcardImages.setClipToPadding(false);
sdcardImages.setOnItemClickListener(LoadImagesFromSDCardActivity.this);
imageAdapter = new ImageAdapter(getApplicationContext());
sdcardImages.setAdapter(imageAdapter);
}
/**
* Load images.
*/
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);
}
}
}
/**
* Add image(s) to the grid view adapter.
*
* #param value Array of LoadedImages references
*/
private void addImage(LoadedImage... value) {
for (LoadedImage image : value) {
imageAdapter.addPhoto(image);
imageAdapter.notifyDataSetChanged();
}
}
/**
* Save bitmap images into a list and return that list.
*
* #see android.app.Activity#onRetainNonConfigurationInstance()
*/
#Override
public Object onRetainNonConfigurationInstance() {
final GridView grid = sdcardImages;
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;
}
/**
* Async task for loading the images from the SD card.
*
* #author Mihai Fonoage
*
*/
class LoadImagesFromSDCard extends AsyncTask<Object, LoadedImage, Object> {
/**
* Load images from SD Card in the background, and display each image on the screen.
*
* #see android.os.AsyncTask#doInBackground(Params[])
*/
#Override
protected Object doInBackground(Object... params) {
//setProgressBarIndeterminateVisibility(true);
Bitmap bitmap = null;
Bitmap newBitmap = null;
Uri uri = null;
// Set up an array of the Thumbnail Image ID column we want
String[] projection = {MediaStore.Images.Thumbnails._ID};
// Create the cursor pointing to the SDCard
Cursor cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null,
null);
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
int size = cursor.getCount();
// If size is 0, there are no images on the SD Card.
if (size == 0) {
//No Images available, post some message to the user
}
int imageID = 0;
for (int i = 0; i < size; i++) {
cursor.moveToPosition(i);
imageID = cursor.getInt(columnIndex);
uri = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID);
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
if (bitmap != null) {
newBitmap = Bitmap.createScaledBitmap(bitmap, 70, 70, true);
bitmap.recycle();
if (newBitmap != null) {
publishProgress(new LoadedImage(newBitmap));
}
}
} catch (IOException e) {
//Error fetching image, try to recover
}
}
cursor.close();
return null;
}
/**
* Add a new LoadedImage in the images grid.
*
* #param value The image.
*/
#Override
public void onProgressUpdate(LoadedImage... value) {
addImage(value);
}
/**
* Set the visibility of the progress bar to false.
*
* #see android.os.AsyncTask#onPostExecute(java.lang.Object)
*/
#Override
protected void onPostExecute(Object result) {
setProgressBarIndeterminateVisibility(false);
}
}
/**
* Adapter for our image files.
*
* #author Mihai Fonoage
*
*/
class ImageAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<LoadedImage> photos = new ArrayList<LoadedImage>();
public ImageAdapter(Context context) {
mContext = context;
}
public void addPhoto(LoadedImage photo) {
photos.add(photo);
}
public int getCount() {
return photos.size();
}
public Object getItem(int position) {
return photos.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
final ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
} else {
imageView = (ImageView) convertView;
}
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setPadding(8, 8, 8, 8);
imageView.setImageBitmap(photos.get(position).getBitmap());
return imageView;
}
}
/**
* A LoadedImage contains the Bitmap loaded for the image.
*/
private static class LoadedImage {
Bitmap mBitmap;
LoadedImage(Bitmap bitmap) {
mBitmap = bitmap;
}
public Bitmap getBitmap() {
return mBitmap;
}
}
/**
* When an image is clicked, load that image as a puzzle.
*/
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
int columnIndex = 0;
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection,
null,
null,
null);
if (cursor != null) {
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToPosition(position);
String imagePath = cursor.getString(columnIndex);
FileInputStream is = null;
BufferedInputStream bis = null;
try {
is = new FileInputStream(new File(imagePath));
bis = new BufferedInputStream(is);
Bitmap bitmap = BitmapFactory.decodeStream(bis);
Bitmap useThisBitmap = Bitmap.createScaledBitmap(bitmap, parent.getWidth(), parent.getHeight(), true);
bitmap.recycle();
//Display bitmap (useThisBitmap)
}
catch (Exception e) {
//Try to recover
}
finally {
try {
if (bis != null) {
bis.close();
}
if (is != null) {
is.close();
}
cursor.close();
projection = null;
} catch (Exception e) {
}
}
}
}
}
The sdcard.xml file:
<GridView
android:id="#+id/sdcard"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center" />