Album art does not show in custom simplecursoradpter
i have succeeded showing all the albums names in the listview but unable to display album art etc
while every thing works fine for the text and animation the album art just doesnot appear in the list view
here is the code i am trying
public class othercustom extends ListActivity {
//define source of MediaStore.Images.Media, internal or external storage
//final Uri sourceUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
final Uri sourceUri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
//final Uri thumbUri = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI;
final String thumbUri = android.provider.MediaStore.Audio.Albums.ALBUM_ART;
final String thumb_DATA = android.provider.MediaStore.Audio.Albums.ALBUM;
final String thumb_IMAGE_ID = android.provider.MediaStore.Audio.Albums._ID;
//SimpleCursorAdapter mySimpleCursorAdapter;
MyAdapter mySimpleCursorAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
String[] from = {android.provider.MediaStore.Audio.Albums.ALBUM};
int[] to = {android.R.id.text1};
CursorLoader cursorLoader = new CursorLoader(
this,
sourceUri,
null,
null,
null,
android.provider.MediaStore.Audio.Albums.ALBUM);
Cursor cursor = cursorLoader.loadInBackground();
mySimpleCursorAdapter = new MyAdapter(
this,
android.R.layout.simple_list_item_1,
cursor,
from,
to
);
setListAdapter(mySimpleCursorAdapter);
getListView().setOnItemClickListener(myOnItemClickListener);
}
OnItemClickListener myOnItemClickListener
= new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Cursor cursor = mySimpleCursorAdapter.getCursor();
cursor.moveToPosition(position);
int int_ID = cursor.getInt(cursor.getColumnIndex(android.provider.MediaStore.Audio.Albums._ID));
getThumbnail(int_ID);
}};
private Bitmap getThumbnail(int id){
String[] thumbColumns = {thumb_DATA, thumb_IMAGE_ID};
CursorLoader thumbCursorLoader = new CursorLoader(
this,
sourceUri,
null,
null,
null,
null);
Cursor thumbCursor = thumbCursorLoader.loadInBackground();
Bitmap thumbBitmap = null;
if(thumbCursor.moveToFirst()){
//int thCulumnIndex = thumbCursor.getColumnIndex(thumb_DATA);
//String thumbPath = thumbCursor.getString(thCulumnIndex);
String thumbPath = android.provider.MediaStore.Audio.Albums.ALBUM_ART;
Toast.makeText(getApplicationContext(),
thumbPath,
Toast.LENGTH_LONG).show();
thumbBitmap = BitmapFactory.decodeFile(thumbPath);
//Create a Dialog to display the thumbnail
AlertDialog.Builder thumbDialog = new AlertDialog.Builder(othercustom.this);
ImageView thumbView = new ImageView(othercustom.this);
thumbView.setImageBitmap(thumbBitmap);
LinearLayout layout = new LinearLayout(othercustom.this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(thumbView);
thumbDialog.setView(layout);
thumbDialog.show();
}else{
Toast.makeText(getApplicationContext(),
"NO Thumbnail!",
Toast.LENGTH_LONG).show();
}
return thumbBitmap;
}
public class MyAdapter extends SimpleCursorAdapter{
Cursor myCursor;
Context myContext;
public MyAdapter(Context context, int layout, Cursor c, String[] from,
int[] to) {
super(context, layout, c, from, to);
myCursor = c;
myContext = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if(row==null){
LayoutInflater inflater=getLayoutInflater();
row=inflater.inflate(R.layout.itemtexview, parent, false);
}
ImageView thumbV = (ImageView)row.findViewById(R.id.thumb);
TextView textV = (TextView)row.findViewById(R.id.text);
myCursor.moveToPosition(position);
int myID = myCursor.getInt(myCursor.getColumnIndex(android.provider.MediaStore.Audio.Albums._ID));
String myData = myCursor.getString(myCursor.getColumnIndex(android.provider.MediaStore.Audio.Albums.ALBUM));
textV.setText(myData);
String[] thumbColumns = {thumb_DATA, thumb_IMAGE_ID};
CursorLoader thumbCursorLoader = new CursorLoader(
myContext,
sourceUri,
null,
null,
null,
null);
Cursor thumbCursor = thumbCursorLoader.loadInBackground();
Bitmap myBitmap = null;
if(thumbCursor.moveToFirst()){
int thCulumnIndex = thumbCursor.getColumnIndex(thumb_DATA);
//String thumbPath = thumbCursor.getString(thCulumnIndex);
String thumbPath = android.provider.MediaStore.Audio.Albums.ALBUM_ART;
myBitmap = BitmapFactory.decodeFile(thumbPath);
thumbV.setImageBitmap(myBitmap);
}
Animation animation = null;
animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.wave);
animation.setDuration(200);
row.startAnimation(animation);
animation = null;
return row;
}
}
}
Use this,
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if(row==null){
LayoutInflater inflater=getLayoutInflater();
row=inflater.inflate(R.layout.itemtexview, parent, false);
}
ImageView thumbV = (ImageView)row.findViewById(R.id.thumb);
TextView textV = (TextView)row.findViewById(R.id.text);
myCursor.moveToPosition(position);
String myID = myCursor.getString(myCursor.getColumnIndex(android.provider.MediaStore.Audio.Albums._ID));
String myData = myCursor.getString(myCursor.getColumnIndex(android.provider.MediaStore.Audio.Albums.ALBUM));
textV.setText(myData);
// gets the artWorkUri.
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
// gets the albumArtUri from artWorkUri and albumId.
Uri albumArtUri = Uri.withAppendedPath(sArtworkUri, myID);
thumbV.setImageURI(albumArtUri);
Animation animation = null;
animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.wave);
animation.setDuration(200);
row.startAnimation(animation);
animation = null;
return row;
}
Related
I am creting music player app and I got music files from device but it is not in shorted order.
It is displaying like this (Not in sorted order)
Code which displays songs in ListView :
public class songlist extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private ListView lv_songlist;
public Cursor cursor;
private MediaCursorAdapter mediaAdapter = null;
private String currentFile;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.songlist);
lv_songlist = (ListView) findViewById(R.id.songlist);
cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
if (null != cursor) {
cursor.moveToFirst();
mediaAdapter = new MediaCursorAdapter(this, R.layout.listitem, cursor);
}
}
private class MediaCursorAdapter extends SimpleCursorAdapter {
public MediaCursorAdapter(Context context, int layout, Cursor c) {
super(context, layout, c,
new String[]{MediaStore.MediaColumns.DISPLAY_NAME, MediaStore.MediaColumns.TITLE, MediaStore.Audio.AudioColumns.DURATION},
new int[]{R.id.displayname, R.id.title, R.id.duration});
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView title = (TextView) view.findViewById(R.id.title);
TextView name = (TextView) view.findViewById(R.id.displayname);
TextView duration = (TextView) view.findViewById(R.id.duration);
name.setText(cursor.getString(
cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)));
title.setText(cursor.getString(
cursor.getColumnIndex(MediaStore.MediaColumns.TITLE)));
long durationInMs = Long.parseLong(cursor.getString(
cursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION)));
Duration d = new Duration();
String durationInMin = d.convertDuration(durationInMs);
duration.setText("" + durationInMin);
view.setTag(cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA)));
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.listitem, parent, false);
bindView(v, context, cursor);
return v;
}
}
}
Create string like this :
private String sortOrder = MediaStore.MediaColumns.DISPLAY_NAME+"";
And pass this string to last argument in contentResolver.query method's last parameter :
cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, sortOrder);
I want to display a list of songs on clicking the album in a grid.
Here's my code for DisplayAlbum extends AppCompatActivity implements LoaderManager.LoaderCallbacks
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.album_detail);
Intent i = getIntent();
b = i.getExtras();
String str;
// if((str =(String) b.getCharSequence("album_name"))!= null)
// Toast.makeText(getApplicationContext(), str, Toast.LENGTH_SHORT).show();
val = new String[]{(String) b.getCharSequence("album_name")};
ListView lv = (ListView) findViewById(R.id.al_songs);
mAdapter = new DisplaySongsAdapter(this, null);
lv.setAdapter(mAdapter);
}
static final String[] ALBUM_DETAIL_PROJECTION = { MediaStore.Audio.Media._ID,MediaStore.Audio.Media.ALBUM_ID,MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.DURATION};
String where = MediaStore.Audio.Media.ALBUM + "=?";
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String orderby = MediaStore.Audio.Media.TITLE;
return new CursorLoader(this, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,ALBUM_DETAIL_PROJECTION, where, val, orderby);
}
And this is the code for DisplaySongsAdapter extends CursorAdapter
public DisplaySongsAdapter(Context context, Cursor c) {
super(context, c);
mcontext=context;
nInflater = LayoutInflater.from(context);
}
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView songTitle = (TextView) view.findViewById(R.id.songTitle);
songTitle.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)));
TextView alname = (TextView) view.findViewById(R.id.al_name);
alname.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)));
TextView artist = (TextView) view.findViewById(R.id.songArtist);
artist.setText(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)));
ImageView albumArt = (ImageView) view.findViewById(R.id.list_image);
albumArt.setScaleType(ImageView.ScaleType.FIT_XY);
ImageView albumimg = (ImageView) view.findViewById(R.id.al_art);
albumimg.setScaleType(ImageView.ScaleType.FIT_XY);
ImageButton listmenu = (ImageButton) view.findViewById(R.id.expanded_menu);
listmenu.setOnClickListener(overflowClickListener);
Long albumId = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
Bitmap img = getAlbumart(context,albumId);
if(img != null) {
albumArt.setImageBitmap(img);
albumimg.setImageBitmap(img);
}
else{
Bitmap def = getDefaultAlbumArt(context);
albumArt.setImageBitmap(def);
albumimg.setImageBitmap(img);
}
}
#Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
final View view = nInflater.inflate(R.layout.song_item, parent, false);
return view;
}
But the activity doesnt populate the listview. It simply shows a blank inflated layout. Why is that the case? Where did I go wrong?
It might be related to the fact that you don't actually feed any items in the adapter :-)
mAdapter = new DisplaySongsAdapter(this, null);
I solved it using a SimpleCursorAdapter. However I would still like to know a way around with LoaderManager
I've been trying to create multiple image chooser, everything works fine, but the grid scroll is very lazy and slow, I've been tried to use different libraries for image loading in getView() (Picasso,aquery..)
but its no difference ,even with asynctask, that's my code:
Main:
public class Media extends ActionBarActivity {
private AQuery aq;
GridView myGridView;
MyAdapter mySimpleCursorAdapter;
boolean isOddClicked = true;
static String img=null;
final Uri srcUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
final String srcData = MediaStore.Images.Media.DATA;
final String srcImgId = MediaStore.Images.Media._ID;
final Uri thumbUri = MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI;
final String thumb_DATA = MediaStore.Images.Thumbnails.DATA;
final String thumb_IMAGE_ID = MediaStore.Images.Thumbnails.IMAGE_ID;
ArrayList<String> al = new ArrayList<String>(5);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.media);
//Always show the menu
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception ex) {}
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
myGridView = (GridView)findViewById(R.id.gridview);
//
CursorLoader cursorLoader = new CursorLoader(
this,srcUri,null,null,null,MediaStore.Images.Media.DATE_TAKEN+ " DESC");
//
Cursor cursor = cursorLoader.loadInBackground();
//
int[] to = {android.R.id.text1};
String[] from = {MediaStore.MediaColumns.TITLE};
mySimpleCursorAdapter = new MyAdapter(
this,android.R.layout.simple_list_item_1,
cursor,from,to,CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
//
myGridView.setAdapter(mySimpleCursorAdapter);
myGridView.setOnItemClickListener(myOnItemClickListener);
}
OnItemClickListener myOnItemClickListener = new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
Cursor cursor = mySimpleCursorAdapter.getCursor();
cursor.moveToPosition(position);
int int_ID = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.Media._ID));
}
};
private Bitmap getThumbnail(int id){
String[] thumbColumns = {srcData, srcImgId};
CursorLoader thumbCursorLoader = new CursorLoader(
this,srcUri,thumbColumns,srcImgId + "=" + id,null,null);
Cursor thumbCursor = thumbCursorLoader.loadInBackground();
Bitmap thumbBitmap = null;
if(thumbCursor.moveToFirst()){
int thCulumnIndex = thumbCursor.getColumnIndex(srcData);
String thumbPath = thumbCursor.getString(thCulumnIndex);
Toast.makeText(getApplicationContext(),thumbPath,Toast.LENGTH_LONG).show();
thumbBitmap = BitmapFactory.decodeFile(thumbPath);
//Create a Dialog to display the thumbnail
AlertDialog.Builder thumbDialog = new AlertDialog.Builder(Media.this);
ImageView thumbView = new ImageView(Media.this);
thumbView.setImageBitmap(thumbBitmap);
LinearLayout layout = new LinearLayout(Media.this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(thumbView);
thumbDialog.setView(layout);
thumbDialog.show();
}else{
Toast.makeText(getApplicationContext(),"NO Thumbnail!",Toast.LENGTH_LONG).show();
}
return thumbBitmap;
}
public class MyAdapter extends SimpleCursorAdapter{
Cursor myCursor;
Context myContext;
public MyAdapter(Context context, int layout, Cursor c, String[] from,int[] to, int flags) {
super(context, layout, c, from, to, flags);
myCursor = c;
myContext = context;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater=getLayoutInflater();
convertView =inflater.inflate(R.layout.media_row, parent, false);
holder = new ViewHolder();
holder.thumbnail = (ImageView)convertView.findViewById(R.id.thumb);
holder.title = (CheckBox) convertView.findViewById(R.id.itemCheckBox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
myCursor.moveToPosition(position);
final int myID = myCursor.getInt(myCursor.getColumnIndex(MediaStore.Images.Media._ID));
final String[] thumbColumns = {srcData ,srcImgId};
CursorLoader thumbCursorLoader = new CursorLoader(
myContext,srcUri,thumbColumns,srcImgId + "=" + myID,null,null);
Cursor thumbCursor = thumbCursorLoader.loadInBackground();
if(thumbCursor.moveToFirst()){
final int thCulumnIndex = thumbCursor.getColumnIndex(thumb_DATA);
final String thumbPath = thumbCursor.getString(thCulumnIndex);
Bitmap o = MediaStore.Images.Thumbnails.getThumbnail(getApplicationContext().getContentResolver(), myID, Images.Thumbnails.MICRO_KIND, null);
//holder.thumbnail.setImageBitmap(o);
//Phase II:
String uri = null;
Cursor cursor = MediaStore.Images.Thumbnails.queryMiniThumbnail(
getContentResolver(), myID,
MediaStore.Images.Thumbnails.MINI_KIND,null );
if( cursor != null && cursor.getCount() > 0 ) {
cursor.moveToFirst();//**EDIT**
uri = cursor.getString( cursor.getColumnIndex( MediaStore.Images.Thumbnails.DATA ) );
}
MediaAsync loadAsync = new MediaAsync(getApplicationContext(), holder.thumbnail);
loadAsync.onPostExecute(thumbPath);
/*
holder.thumbnail.setOnClickListener(new OnClickListener(){
public void onClick(View v){
img = thumbPath;
al.add(thumbPath);
if (holder.title.isChecked()) {
holder.title.setChecked(false);
holder.thumbnail.setBackgroundResource(0);
}else{
holder.title.setChecked(true);
holder.thumbnail.setBackgroundResource(R.drawable.media_row_border);
}
}
});
*/
}
return convertView ;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.media, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
finish();
}
else if (id == R.id.done) {
Intent main = new Intent(this, Main.class);
main.putStringArrayListExtra("media_lst", al);
startActivity(main);
}
return super.onOptionsItemSelected(item);
}
private static class ViewHolder {
ImageView thumbnail;
CheckBox title;
}
}
mediaAsynce :
public class MediaAsync extends AsyncTask<String,String, String>{
private ImageView mImageView;
private Context mContext;
public MediaAsync(Context context,ImageView imageView) {
mImageView = imageView;
mContext = context;
}
#Override
protected String doInBackground(String... params) {
String url = params[0].toString();
return url;
}
#Override
protected void onPostExecute(String result) {
Uri uri = Uri.fromFile(new File(result));
Picasso.with(mContext)
.load(uri)
//.resize(100, mWidth)
.into(mImageView);
// AQuery aq = new AQuery(mContext);
// aq.id(mImageView).image(result, true, true, 0, R.drawable.ic_launcher);
}
}
As you said you have tried picasso library it should work
Just try once again using picassso like this Async task and all is not required
Just create a simple adapter and load images.
public class GridViewDynamicAdapter extends BaseDynamicGridAdapter {
ImageDetails details;
public static List<ImageDetails> list;
public GridViewDynamicAdapter(Context context, List<ImageDetails> items,
int columnCount) {
super(context, items, columnCount);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
CheeseViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(
R.layout.item_grid, null);
holder = new CheeseViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (CheeseViewHolder) convertView.getTag();
}
if (position == getCount() - 1) {
ImageApplication.fromRecorderPaths = (List<ImageDetails>) getItems();
}
details = (ImageDetails) getItem(position);
// Log.d("Setting=", details.path);
holder.build(getContext(), details.path);
return convertView;
}
private class CheeseViewHolder {
private ImageView image;
private CheeseViewHolder(View view) {
image = (ImageView) view.findViewById(R.id.item_img);
}
void build(final Context context, String title) {
Picasso.with(context).load(new File(title)).centerCrop()
.resize(150, 150).error(R.drawable.ic_launcher).into(image);
}
}
}
OR
Try this example
I am getting bitmaps from MediaStore like this
public class VideoStoredInSDCard extends Activity
{
private Cursor videoCursor;
private int videoColumnIndex;
ListView videolist;
int count;
String thumbPath;
String[] thumbColumns = { MediaStore.Video.Thumbnails.DATA,MediaStore.Video.Thumbnails.VIDEO_ID };
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initialization();
}
private void initialization()
{
System.gc();
String[] videoProjection = { MediaStore.Video.Media._ID,MediaStore.Video.Media.DATA,
MediaStore.Video.Media.DISPLAY_NAME,MediaStore.Video.Media.SIZE };
videoCursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,videoProjection, null, null, null);
count = videoCursor.getCount();
videolist = (ListView) findViewById(R.id.PhoneVideoList);
videolist.setAdapter(new VideoListAdapter(this.getApplicationContext()));
videolist.setOnItemClickListener(videogridlistener);
}
private OnItemClickListener videogridlistener = new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position,long id)
{
System.gc();
videoColumnIndex = videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
videoCursor.moveToPosition(position);
String filename = videoCursor.getString(videoColumnIndex);
Log.i("FileName: ", filename);
//Intent intent = new Intent(VideoActivity.this, ViewVideo.class);
//intent.putExtra("videofilename", filename);
//startActivity(intent);
}};
public class VideoListAdapter extends BaseAdapter
{
private Context vContext;
int layoutResourceId;
public VideoListAdapter(Context c)
{
vContext = c;
}
public int getCount()
{
return videoCursor.getCount();
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View listItemRow = null;
listItemRow = LayoutInflater.from(vContext).inflate(R.layout.listitem, parent, false);
TextView txtTitle = (TextView)listItemRow.findViewById(R.id.txtTitle);
TextView txtSize = (TextView)listItemRow.findViewById(R.id.txtSize);
ImageView thumbImage = (ImageView)listItemRow.findViewById(R.id.imgIcon);
videoColumnIndex = videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME);
videoCursor.moveToPosition(position);
txtTitle.setText(videoCursor.getString(videoColumnIndex));
videoColumnIndex = videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE);
videoCursor.moveToPosition(position);
txtSize.setText(" Size(KB):" + videoCursor.getString(videoColumnIndex));
int videoId = videoCursor.getInt(videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID));
Cursor videoThumbnailCursor = managedQuery(MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
thumbColumns, MediaStore.Video.Thumbnails.VIDEO_ID+ "=" + videoId, null, null);
if (videoThumbnailCursor.moveToFirst())
{
thumbPath = videoThumbnailCursor.getString(videoThumbnailCursor.getColumnIndex(MediaStore.Video.Thumbna ils.DATA));
Log.i("ThumbPath: ",thumbPath);
}
thumbImage.setImageURI(Uri.parse(thumbPath));
return listItemRow;
}
}
}
How to get thumbnails from a specific folder in sd card. I am using this tutorial http://gypsynight.wordpress.com/2012/02/17/how-to-show-all-video-file-stored-in-your-sd-card-in-a-listview/
It can be done easily this way:
int videoId = videoCursor.getInt(videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID));
ContentResolver cr = getContentResolver();
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 1;
Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(cr, videoId, MediaStore.Video.Thumbnails.MICRO_KIND, options);
thumbImage.setImageBitmap(curThumb);
How do I get a string from my cursor into my putExtra call in an onItemClick for a ListView? I need to grab the string from my DB column named 'gotoURL' into the:
i.putExtra("Url", ???).
...of my onItemClick in the Activity. Sample code would be helpful for the learning. Thnx!!
My Activity has:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.list_view2);
final ListView lv = getListView();
activityTitle = (TextView) findViewById(R.id.titleBarTitle);
activityTitle.setText("ADVISORY CIRCULATORS");
displayResultList();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
// #Override
public void onItemClick(AdapterView<?> a, View v, int position,long id)
{
Toast.makeText(List_AC.this, "Clicked!", Toast.LENGTH_LONG).show();
Object o = lv.getItemAtPosition(position);
Adapter_AC fullObject = (Adapter_AC)o;
Intent i = new Intent(List_AC.this, DocView.class);
//i.putExtra("url", Adapter_AC.gotoURL);
startActivity(i);
}
});
}
private void displayResultList() {
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
extStorageDirectory = Environment.getExternalStorageDirectory()
.toString();
File dbfile = new File(extStorageDirectory
+ "/XXX/XXX/dB/XXX.db");
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile,
null);
Cursor databaseCursor = db.rawQuery(
"SELECT * FROM AC_list ORDER BY `label` ASC", null);
Adapter_AC databaseListAdapter = new Adapter_AC(this,
R.layout.list_item, databaseCursor, new String[] { "label",
"title", "description" }, new int[] { R.id.label,
R.id.listTitle, R.id.caption });
databaseListAdapter.notifyDataSetChanged();
this.setListAdapter(databaseListAdapter);
} else if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_UNMOUNTED)) {
Log.i("tag", "SDCard is NOT writable/mounted");
Alerts.sdCardMissing(this);
}
}
}
And my Adapter:
public class Adapter_AC extends SimpleCursorAdapter {
private Cursor dataCursor;
private LayoutInflater mInflater;
public Adapter_AC(Context context, int layout, Cursor dataCursor,
String[] from, int[] to) {
super(context, layout, dataCursor, from, to);
this.dataCursor = dataCursor;
mInflater = LayoutInflater.from(context);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView.findViewById(R.id.label);
holder.text2 = (TextView) convertView.findViewById(R.id.listTitle);
holder.text3 = (TextView) convertView.findViewById(R.id.caption);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
dataCursor.moveToPosition(position);
int label_index = dataCursor.getColumnIndex("label");
String label = dataCursor.getString(label_index);
int title_index = dataCursor.getColumnIndex("title");
String title = dataCursor.getString(title_index);
int description_index = dataCursor.getColumnIndex("description");
String description = dataCursor.getString(description_index);
int goto_index = dataCursor.getColumnIndex("gotoURL");
String gotoURL = dataCursor.getString(goto_index);
holder.text1.setText(label);
holder.text2.setText(title);
holder.text3.setText(description);
return convertView;
}
static class ViewHolder {
TextView text1;
TextView text2;
TextView text3;
}
}
You can use the CursorAdapter's getItem() method to get a Cursor pointing to the current id. Like so:
...
Cursor cursor = adapter.getItem(id);
i.putExtra("url", cursor.getString(cursor.getColumnIndex("gotoURL")));
...
Note that I suspect you'll need to include that column in your projection, like so:
Adapter_AC databaseListAdapter = new Adapter_AC(this,
R.layout.list_item, databaseCursor, new String[] { "label",
"title", "description", "gotoURL" }, new int[] { R.id.label,
R.id.listTitle, R.id.caption, R.id.dummy });
And create a dummy (View.GONE) invisible field for it so that the data is pulled in the cursor.
You might try using the 'Tag' field in the ListView: set it to the correct value when creating and reading it to get the right value when clicked...