Hi all,
I have developed a very simple gallery (it started as the hello
gallery example) where I have added code to get the images from the
content provider. It should return all images from the SD card but it
is returning no images, my gallery view is blank. I am a fairly new
developer, just working my way around the tutorials online. Here is
the code.
package com.tutorials;
import java.util.ArrayList;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.provider.MediaStore.Images.Thumbnails;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
public class HelloGallery 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));
g.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView parent, View v, int
position, long id){
Toast.makeText(HelloGallery.this,"" + position,
Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter{
int mGalleryItemBackground;
private Context mContext;
//get images from phone
public ArrayList<Uri> getImageTable(){
ArrayList<Uri> arrayList = new ArrayList<Uri>();
try{
ContentResolver cr = getContentResolver();
String imageId = Media._ID;
Toast toast =
Toast.makeText(mContext,imageId,Toast.LENGTH_LONG);
toast.show();
String [] images = new String[]{imageId};
//Get Uri of ContentProvider MediaStore.Images.ImageColumns
Uri mediaUri = Thumbnails.EXTERNAL_CONTENT_URI;
Cursor managedCursor = managedQuery(mediaUri, images, null,
null, null);
startManagingCursor(managedCursor);
int columnIndex = managedCursor.getColumnIndexOrThrow(imageId);
for(int i=0;i<managedCursor.getCount();i++){
managedCursor.moveToPosition(i);
int id = managedCursor.getInt(columnIndex);
arrayList.add(Uri.withAppendedPath(mediaUri, "" +
imageId));
}
}catch(Exception e){
Toast toast =
Toast.makeText(mContext,e.toString(),Toast.LENGTH_LONG);
toast.show();
}
return arrayList;
}
public ImageAdapter(Context c){
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = a.getResourceId(
R.styleable.HelloGallery_android_galleryItemBackground, 0);
a.recycle();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup
parent) {
// TODO Auto-generated method stub
ImageView i = new ImageView(mContext);
//i.setImageResource(mImageIds[position]);
i.setImageURI(getImageTable().get(position));
i.setLayoutParams(new Gallery.LayoutParams(150, 100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en