How to get all images from sd-card (android-gallery) ? - android

In my app I need to implement an option for the user to select images from sd-card or phone memory. I have used custom cover-flow to animate the images. I have written a code to retrieve images from sd-card using cursor. But I get only few images, not all. And these images are displayed multiple times in my cover-flow. Each image is displaying twice.
If there are no images on sd-card then app crashes.
Here is my code, Please somebody help
public class CoverFlowActivityMain extends Activity {
private Cursor cursor;
private int columnIndex;
private File file;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CoverFlow coverFlow;
coverFlow = new CoverFlow(this);
file = new File("/sdcard/");
// I am using this file to check iamges on sd-card,
// but this does not search files in subdirectories.
File[] allFiles = file.listFiles();
for(int i=0; i<allFiles.length; i++) {
Log.v("File: "+i, ""+allFiles[i].getName().toString());
}
// 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 = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null,
MediaStore.Images.Thumbnails.IMAGE_ID);
// Get the column index of the Thumbnails Image ID
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
coverFlow.setAdapter(new ImageAdapter(this));
ImageAdapter coverImageAdapter = new ImageAdapter(this);
coverFlow.setAdapter(coverImageAdapter);
coverFlow.setSpacing(-5);
coverFlow.setSelection(0, true);
coverFlow.setAnimationDuration(1500);
setContentView(coverFlow);
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return cursor.getCount();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
//-- Use this code if you want to load from sdcard --\\
ImageView picturesView;
if (convertView == null) {
picturesView = new ImageView(mContext);
// Move cursor to current position
cursor.moveToPosition(position);
// Get the current value for the requested column
int imageID = cursor.getInt(columnIndex);
// Set the content of the image based on the provided URI
picturesView.setImageURI(Uri.withAppendedPath(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));
picturesView.setLayoutParams(new CoverFlow.LayoutParams(380, 450));
picturesView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
//Make sure we set anti-aliasing otherwise we get jaggies
BitmapDrawable drawable = (BitmapDrawable) picturesView.getDrawable();
drawable.setAntiAlias(true);
}
else {
picturesView = (ImageView)convertView;
}
return picturesView;
}
}
}

Related

Using lazy list in gallery

I'm trying to display some pictures by extracting the picture name from a sqlite database and then using that picture name to find that particular picture in the drawable folder and display it in a gallery widget in my app. It works but now the problem is that I have too many pictures, so it doesn't make sense to store everything in the drawable folder as it will make the app too big. I am thinking of placing the pictures online and store the individual url into the sqlite database instead. In this way, I can get the url from the database and using that url, I can download the image and display it in the gallery widget. I read about Lazy List (kudos for the great work!) but I'm having problem implementing in my app. Below is the current code I am using for the gallery and I'm not too sure how to modify it to make use of Lazy List to download the images from online. Any help is greatly appreciated! =)
protected String pic1, pic2, pic3;
protected int Id, resID1, resID2, resID3;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result_details);
Id = getIntent().getIntExtra("ID", 0);
SQLiteDatabase db = (new DatabaseHelper(this)).getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT pic1, pic2, pic3 FROM database WHERE _id = ?",
new String[]{""+Id});
pic1 = cursor.getString(cursor.getColumnIndex("pic1"));
pic2 = cursor.getString(cursor.getColumnIndex("pic2"));
pic3 = cursor.getString(cursor.getColumnIndex("pic3"));
resID1 = getResources().getIdentifier(pic1 , "drawable", getPackageName());
resID2 = getResources().getIdentifier(pic2 , "drawable", getPackageName());
resID3 = getResources().getIdentifier(pic3 , "drawable", getPackageName());
Gallery g = (Gallery) findViewById(R.id.photobar);
g.setAdapter(new ImageAdapter(this));
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Integer[] mImageIds = {
resID1,
resID2,
resID3
};
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.Theme);
mGalleryItemBackground = a.getResourceId(
R.styleable.Theme_android_galleryItemBackground,
0);
a.recycle();
}
public int getCount() {
return mImageIds.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(mImageIds[position]);
i.setLayoutParams(new Gallery.LayoutParams(150, 100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
return i;
}
}
Use ImageLoader() from the Lazylist coding, and pass ImageView and Imageurl to ImageLoader
like this
imageLoader.DisplayImage(ImageUrl, imageview);
in the adapter getView() method.

Custom Camera/Gallery app not showing sdcard/folder accurately

I have built a custom camera app that saves pictures to a specific folder on my sdcard and has its own gallery view for viewing the photos in that folder "/sdcard/myApp/images"
I have been testing it on two different tablets, Galaxy Tab-1 10 inch and Galaxy Tab-2 10 inch. The camera works on both tablets just fine, taking pictures and saving them to the app created folder. But the gallery View on the Tab1, doesn't show all the photos in that folder. The tab-2 always shows all the photos currently in the folder but the tab-1 often only shows the images in that folder from the last time the tablet was rebooted.
So on the Tab-1 if i have no photos in that folder and then take two pictures with the camera and switch to the gallery view, it shows no photos. If i then reboot, and go to the gallery view i see both of the photos, but if i switch to the camera and take another photo the gallery only shows the original two until i reboot.
works on android 3.2 + but 3.1 nope!
Any ideas?
cursor sent to adapter in constructor of galleryView:
// Set up an array of the Thumbnail Image ID column we want
String[] projection = {MediaStore.Images.Media._ID};
// Create the cursor pointing to the SDCard
cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
MediaStore.Images.Media.DATA + " like ? ",
new String[] {"%LC/images%"},
MediaStore.Images.Media._ID + " DESC");
// Get the column index of the Thumbnails Image ID
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
ga.setAdapter(new GallAdapter(this,cursor,columnIndex));
addapter:
public class GallAdapter extends BaseAdapter {
public Cursor cursor;
private int columnIndex;
private Context context;
int imageBackground;
String url;
Uri uri;
int originalImageId;
int imageID;
int columnData;
ViewGroup myp;
ImageView d;
public GallAdapter(Context ctx, Cursor cur, int cIn ) {
context = ctx;
columnIndex = cIn;
cursor = cur;
Log.v("GallAdapter", "COUNT:"+getCount());
}
#Override
public int getCount() {
return cursor.getCount();
}
#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) {
myp = parent;
View v;
if(convertView ==null){
v = LayoutInflater.from(context).inflate(R.layout.galitem, parent, false);
}else{
v = convertView;
}
ImageView photo = (ImageView) v.findViewById(R.id.imageView);
ImageView border = (ImageView) v.findViewById(R.id.borderView);
d = (ImageView) v.findViewById(R.id.delView);
// Move cursor to current position
cursor.moveToPosition(position);
// Get the current value for the requested column
imageID = cursor.getInt(columnIndex);
// obtain the image URI
uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(imageID) );
url = uri.toString();
// Set the content of the image based on the image URI
originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));
Bitmap b = MediaStore.Images.Thumbnails.getThumbnail(context.getContentResolver(),
originalImageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
photo.setImageBitmap(b);
photo.setScaleType(ImageView.ScaleType.FIT_CENTER);
d.setTag(uri);
d.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
String path = getRealPathFromURI((Uri) v.getTag());
alertBox("Warning!", "Are you sure you want to delete this photo?", path, v);
}
});
return v;
}
again this works on tab-2 (os 4.0) and tab-1 now that the firm ware was updated on the tab-1 and the os from 3.1 to 3.2 but why was it not working..
Try this in order to refresh your media files.
this._context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + Environment.getExternalStorageDirectory() + "/Hugg/" + name)));

Is there a way to sort images by date in gallery android

I have a working custom camera and gallery view in my android app. My camera saves pictures with a timestamp essentially as their name in a specific folder which i have successfully got showing in my gallery. It seems that it sorts these images by default from oldest to newest or alphabetically, not sure.. but i would like them to display in the opposite order.. can someone point me in the right direction? is it possible? do i change my BaseAdapter or my oncreate of my gallery activity...
my gallery activity:
public class GalleryView extends Activity{
ImageView imageView;
Cursor cursor;
private int columnIndex;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
Gallery ga = (Gallery)findViewById(R.id.Gallery01);
// Set up an array of the Thumbnail Image ID column we want
String[] projection = {MediaStore.Images.Media._ID};
// Create the cursor pointing to the SDCard
cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
MediaStore.Images.Media.DATA + " like ? ",
new String[] {"%LC/images%"},
null);
// Get the column index of the Thumbnails Image ID
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
ga.setAdapter(new GallImageAdapter(this,cursor,columnIndex));
}
}
my custom adapter:
public class GallImageAdapter extends BaseAdapter {
public Cursor cursor;
private int columnIndex;
private Context context;
int imageBackground;
public GallImageAdapter(Context ctx, Cursor cur, int cIn) {
context = ctx;
columnIndex = cIn;
cursor = cur;
}
#Override
public int getCount() {
return cursor.getCount();
}
#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) {
View v;
if(convertView ==null){
v = LayoutInflater.from(context).inflate(R.layout.galitem, parent, false);
}else{
v = convertView;
}
ImageView photo = (ImageView) v.findViewById(R.id.imageView);
ImageView border = (ImageView) v.findViewById(R.id.borderView);
ImageView d = (ImageView) v.findViewById(R.id.delView);
// Move cursor to current position
cursor.moveToPosition(position);
// Get the current value for the requested column
int imageID = cursor.getInt(columnIndex);
// obtain the image URI
Uri uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(imageID) );
String url = uri.toString();
// Set the content of the image based on the image URI
int originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));
Bitmap b = MediaStore.Images.Thumbnails.getThumbnail(context.getContentResolver(),
originalImageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
photo.setImageBitmap(b);
photo.setScaleType(ImageView.ScaleType.FIT_CENTER);
return v;
}
}
The last parameter is for describing the order of URI in interest.
Try this approach to sort images in descending order
final String orderBy = MediaStore.Images.Media._ID;
cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
MediaStore.Images.Media.DATA + " like ? ",
new String[] {"%LC/images%"},
orderBy + " DESC");

Load images from sd card folder

I'm trying to load images from the specific folder in sd card.i saved images to folder in the sd card. now i want to load images from that folder.try to do this using online tutorials. but didn't work any thing.when i try to use below code i received blank screen.
What am I doing wrong? Many thanks for any help.
public class F6Activity extends softActivity
{
private Cursor cursor;
private int columnIndex;
Button next;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_f6);
Gallery g = (Gallery) findViewById(R.id.gallery);
//request only the image ID to be returned
String[] projection = {MediaStore.Images.Media._ID};
//Create the cursor pointing to the SDCard
cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
MediaStore.Images.Media.DATA + " like ? ",
new String[] {"%ereports%"},
null);
//Get the column index of the image ID
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
g.setAdapter(new ImageAdapter(this));
}
private class ImageAdapter extends BaseAdapter {
private Context context;
public ImageAdapter(Context localContext)
{
context = localContext;
}
public int getCount()
{
return cursor.getCount();
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(context);
// Move cursor to current position
cursor.moveToPosition(position);
// Get the current value for the requested column
int imageID = cursor.getInt(columnIndex);
// obtain the image URI
Uri uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
Integer.toString(imageID) );
String url = uri.toString();
// Set the content of the image based on the image URI
int originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1,
url.length()));
Bitmap b = MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(),
originalImageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
i.setImageBitmap(b);
i.setLayoutParams(new Gallery.LayoutParams(150, 100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
int mGalleryItemBackground = 0;
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
}
}
I am getting confused with code, Now I can't say the problem in your code, it will take time for me. I suggest to use the code available at below link. It is working fine. I will try to find the problem in your code afterwards. Now check the link below.
Display images from SD card

Android: Displaying Images From SDcard

I have an app on the market that displays all photos from the user's SDcard in a gallery. Initially I had out of memory issues, but alleviated those by increasing the inSampleSize in the BitmapFactory options. However, I still have intermittent reports of images not displaying, in some cases none at all. There seems to be a larger proportion of Droid Eris users having problems. Here is the method that loads all photos and the accompanying ImageAdapter:
private void loadAllPhotos() {
setContentView(R.layout.add_pictures);
String[] projection = {MediaStore.Images.Thumbnails._ID};
cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection,
null,
null,
MediaStore.Images.Thumbnails.IMAGE_ID);
column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
int size = cursor.getCount();
if (size == 0) {
Toast.makeText(thisContext, "Can't find pics on SDcard.", Toast.LENGTH_SHORT).show();
}
g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
}
The ImageAdapter:
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
mGalleryItemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount() {
int cursorCount = mCursor.getCount();
return cursorCount;
}
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);
System.gc();
if (convertView == null) {
try {
String [] proj={MediaStore.Images.Media.DATA};
mCursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,proj, null, null, null);
column_index = mCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
mCursor.moveToPosition(position);
filename = mCursor.getString(column_index);
if (filename.endsWith(".jpg") || filename.endsWith(".png")) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 15;
Bitmap bm = BitmapFactory.decodeFile(filename, options);
i.setImageBitmap(bm);
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setLayoutParams(new Gallery.LayoutParams(150, 150));
i.setBackgroundResource(mGalleryItemBackground);
System.gc();
}
} catch (Exception e) {
}
}
return i;
}
}
If you have not done so already, integrate Flurry or DroidDrop or something to collect exceptions, as they may shed some light on your problem.
Also:
Please recycle your Views! You are creating a new ImageView on every getView() call, which is not helping your GC situation.
You are calling the same managedQuery() on every getView() call, which is horrible. Do that once, please.
You seem to be accessing mCursor in getCount() before it is initialized in getView().
column_index should always be 0 in the way you are calling managedQuery(), so you should not need to use getColumnIndexOrThrow(). Even if you want to use that, only do that once, when you make your single managedQuery() call.
Whether any of that will clear up your Droid Eris problems, though, I cannot say.
I tried the code above, it works well only if you have just .png and .jpg on the SD. Otherwise it can get some strange path out of the mCursor and in that case will show nothing.

Categories

Resources