Sunday, 11 March 2012

Display video thumbnail on ListFragment with customized SimpleCursorAdapter

Last exercise show how to "Get Thumbnail of video". In this exercise, we are going to embed thumbnail in ListFragment using customized SimpleCursorAdapter, MyCursorAdapter.

Display video thumbnail on ListFragment with customized SimpleCursorAdapter

Please note that I just show the simple implementation without any optimization. It may be work not too efficiency.

Create /res/layout/row.xml, with a ImageView and a TextView. It's the layout of individual row.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<ImageView
android:id="@+id/image1"
android:layout_width="96dp"
android:layout_height="96dp"
android:src="@drawable/ic_launcher"/>
<TextView
android:id="@+id/text1"
android:textSize="26dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

</LinearLayout>


Modify VideoListFragment.java, to implement our customized SimpleCursorAdapter, MyCursorAdapter. The main job is in getView().
package com.exercise.AndroidListMedia;
import android.app.ListFragment;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class VideoListFragment extends ListFragment {

SimpleCursorAdapter adapter;
final Uri mediaSrc = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
Cursor cursor = adapter.getCursor();
cursor.moveToPosition(position);

String _id = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media._ID));
int intID = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID));

Uri playableUri
= Uri.withAppendedPath(mediaSrc, _id);

AndroidListMediaActivity mainActivity = (AndroidListMediaActivity)getActivity();
mainActivity.myPlayerFragment.setTargetUri(playableUri, intID);
}

@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
String[] from = {
MediaStore.MediaColumns.TITLE};
int[] to = {
android.R.id.text1};

Cursor cursor = getActivity().managedQuery(
mediaSrc,
null,
null,
null,
MediaStore.Audio.Media.TITLE);

adapter = new MyCursorAdapter(getActivity(),
R.layout.row, cursor, from, to);
setListAdapter(adapter);

}

public class MyCursorAdapter extends SimpleCursorAdapter{
public MyCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to) {
super(context, layout, c, from, to);
// TODO Auto-generated constructor stub
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater=getActivity().getLayoutInflater();
View row=inflater.inflate(R.layout.row, parent, false);
TextView text1 = (TextView)row.findViewById(R.id.text1);
ImageView image1 = (ImageView)row.findViewById(R.id.image1);

Cursor cursor = adapter.getCursor();
cursor.moveToPosition(position);

//Title
String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));
text1.setText(title);

//Thumbnail
BitmapFactory.Options options = new BitmapFactory.Options();
options.inDither = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmapThumb = MediaStore.Video.Thumbnails.getThumbnail(
getActivity().getContentResolver(),
cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID)),
Images.Thumbnails.MICRO_KIND,
options);

image1.setImageBitmap(bitmapThumb);

return row;
}
}
}


Download the files.

Next:
- Implement ActionBar for our Media Player App

No comments:

Post a Comment