user2420837
user2420837

Reputation: 315

Video thumbnails in List

I am having trouble displaying video thumbnails in a list. The file path next to the image works, but not the thumbnail. Here is my code:

MainActivity.java:

public class MainActivity extends ListActivity{

File path = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Videos/");

String[] videoFileList = path.list();

public class MyThumbnaildapter extends ArrayAdapter<String>{

    public MyThumbnaildapter(Context context, int textViewResourceId,
            String[] objects) {
        super(context, textViewResourceId, objects);
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        View row = convertView;
        if(row==null){
            LayoutInflater inflater=getLayoutInflater();
            row=inflater.inflate(R.layout.row, parent, false);
        }

        TextView textfilePath = (TextView)row.findViewById(R.id.FilePath);
        textfilePath.setText(videoFileList[position]);
        ImageView imageThumbnail = (ImageView)row.findViewById(R.id.Thumbnail);

        Bitmap bmThumbnail;
        bmThumbnail = ThumbnailUtils.createVideoThumbnail(videoFileList[position], Thumbnails.MINI_KIND);
        imageThumbnail.setImageBitmap(bmThumbnail);

        return row;
    }

}

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setListAdapter(new MyThumbnaildapter(MainActivity.this, R.layout.row, videoFileList));
}

}

My row.xml file:

<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/Thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon"/>
<TextView
android:id="@+id/FilePath"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>

The file path is displayed correctly, but not the image. I suspect the problem is somehow in my row.xml, but there are no errors. If I direct it to just 2 videos that I manually set the path to, using 2 unique strings, it works fine. Thanks for your help!

Upvotes: 1

Views: 701

Answers (2)

Shakeeb Ayaz
Shakeeb Ayaz

Reputation: 6096

Are you sure that you are getting file video path not the video name? I think you are getting video name because in videoFileList array you are storing the string that comes after "/Videos/" . That means you are not storing the path but the file that are in '"videos"' folder. To get the thumbnail you need the file path not the file name. To get the videos only from videos folder you can use coursor like this.

public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      final String[] proj = { MediaStore.Video.Media.DATA };
      String folder="bluetooth";
     Cursor cursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
            proj, MediaStore.Video.Media.DATA + " like ? ",
            new String[] { "%" + folder + "%" }, null);
     videopath=new ArrayList<String>();
     while (cursor.moveToNext()) {

            videopath.add(cursor.getString(0));
        }
      setListAdapter(new MyThumbnaildapter(AndroidThumbnailList.this, R.layout.row, videopath));
  }
}

and modefy your code this way

1.

  public MyThumbnaildapter(Context context, int textViewResourceId,
        String[] objects) {
    super(context, textViewResourceId, objects);

Replace with below

 public MyThumbnaildapter(Context context, int textViewResourceId,
            ArrayList<String> videopath) {
 super(context, textViewResourceId, videopath);

2.

  videoFileList[position]

replce with

  videopath.get(position)

Update: If the file extension is not of video type then above code will give error so update your onCreate

  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      videopath=new ArrayList<String>();
      File path = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/bluetooth/");
      File[] files = path.listFiles();
      for( File f : files ){


                  String absPath = f.getAbsolutePath();
                  videopath.add(absPath);


      }

      setListAdapter(new MyThumbnaildapter(AndroidThumbnailList.this, R.layout.row, videopath));
  }

}

The updated code will populate all the files those are in bluetooth folder, video and non-video both and will return thumbnail for the video.

Upvotes: 1

prijupaul
prijupaul

Reputation: 2086

Do you mind checking whether the thumbnail created is NULL or not. As per the documenation, it says

Create a video thumbnail for a video. May return null if the video is corrupt or the format is not supported.

Upvotes: 0

Related Questions