0

I am trying to set an Image to an imageView from a Video using MediaMetadataRetriever.

Every time when I move the seekbar I am taking the frame and trying to display it using

mediaMetadataRetriever.getFrameAtTime((int)(33*10),MediaMetadataRetriever.OPTION_CLOSEST);

It is working fine for first time but when I try it again using the progress of seekbar it is not working. Below is my code.

  @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            seekBar1 = (SeekBar)findViewById(R.id.seekBar1);
            imageView1 = (ImageView)findViewById(R.id.imageView1);

            File sdcard = Environment.getExternalStorageDirectory();
            file = new File(sdcard,"VID_20130922_130050.mp4");
            retriever = new MediaMetadataRetriever();
            try {
                retriever.setDataSource(file.getAbsolutePath());
                Log.v("", "");
                Bitmap bitmap = retriever.getFrameAtTime((int)(33*10),MediaMetadataRetriever.OPTION_CLOSEST);
                imageView1.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 480, 320, false));
                bitmap.recycle();

            } catch (IllegalArgumentException ex) {
                ex.printStackTrace();
            } catch (RuntimeException ex) {
                ex.printStackTrace();
            }

seekBar1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {

                String[] params =null;;
                if(progress >0)
                    setImage(progress);

            }
        });
    }

    public void setImage(int progress){
            Bitmap bitmap = retriever.getFrameAtTime((int)(33*progress),MediaMetadataRetriever.OPTION_CLOSEST);
    imageView1.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 480, 320, false));

        }

EDIT

Adding screen shot to explain the question better. here I only need multiple images according to frame, videoView or mediaPlayer is used.

enter image description here

abhishek
  • 857
  • 3
  • 13
  • 34
  • possible duplicate of [Android: Is it possible to display video thumbnails?](http://stackoverflow.com/questions/1334694/android-is-it-possible-to-display-video-thumbnails) – Pramod Ravikant Sep 25 '13 at 07:11
  • @micro.pravi I need images of the frame not thumbnails. Above link is not helpful. Thanks – abhishek Sep 25 '13 at 07:20

2 Answers2

0

Try like this to show Thumbnail of video to ImageView.

   Bitmap thumb = ThumbnailUtils.createVideoThumbnail(STRING_PATH_OF_VIDEO,MediaStore.Images.Thumbnails.FULL_SCREEN_KIND);
    Matrix matrix = new Matrix();
    Bitmap bitmap = Bitmap.createBitmap(thumb, 0, 0,
            thumb.getWidth(), thumb.getHeight(), matrix, true);
    imageView1.setImageBitmap(bitmap);
Amit Gupta
  • 8,914
  • 1
  • 25
  • 33
0

It was not working because the resolution was very high and I was rendering it on UI thread only, using Asyn task solved my problem.

public class FetchImageInBackground extends AsyncTask<String, String, Bitmap> {

        @Override
        protected Bitmap doInBackground(String... params) {
            Bitmap bitmap = retriever.getFrameAtTime(iProgress,MediaMetadataRetriever.OPTION_CLOSEST);
            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap result) {
            setImage(result);
            super.onPostExecute(result);
        }

    }
abhishek
  • 857
  • 3
  • 13
  • 34