0

如何制作像图片库一样的视频库。我们在图片库中提供了很多可绘制的内容,这样我们可以为视频库提供很多 url。例如,这是图片库 Relavant Link http://www.edumobile.org/android/android-development/image-gallery-example-in-android/ 就像我们如何制作视频库的幻灯片共享一样。

4

1 回答 1

8

我的主要加莱班

  public class HorizontalScrollExampleActivity extends Activity implements
        OnClickListener {
    LinearLayout myGallery;
    Cursor cursor;
    private ArrayList<VideoViewInfo> _videoRows;
    private VideoView mVideoView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gallaryview);

        myGallery = (LinearLayout) findViewById(R.id.mygallery);
        mVideoView = (VideoView) findViewById(R.id.videoView1);
        String targetPath = Environment.getExternalStorageDirectory()
                .getAbsolutePath();

        Toast.makeText(getApplicationContext(), targetPath, Toast.LENGTH_LONG)
                .show();
        loaddata();
        /*
         * File targetDirector = new File(targetPath);
         * 
         * File[] files = targetDirector.listFiles(); for (File file : files) {
         * myGallery.addView(insertPhoto(file.getAbsolutePath())); }
         */
    }

    private void loaddata() {
        String[] thumbColumns = { MediaStore.Video.Thumbnails.DATA,
                MediaStore.Video.Thumbnails.VIDEO_ID };

        String[] mediaColumns = { MediaStore.Video.Media._ID,
                MediaStore.Video.Media.DATA, MediaStore.Video.Media.TITLE,
                MediaStore.Video.Media.MIME_TYPE };

        cursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                mediaColumns, null, null, null);

        ArrayList<VideoViewInfo> videoRows = new ArrayList<VideoViewInfo>();
        if (cursor.moveToFirst()) {
            do {

                VideoViewInfo newVVI = new VideoViewInfo();
                int id = cursor.getInt(cursor
                        .getColumnIndex(MediaStore.Video.Media._ID));
                Cursor thumbCursor = managedQuery(
                        MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
                        thumbColumns, MediaStore.Video.Thumbnails.VIDEO_ID
                                + "=" + id, null, null);
                if (thumbCursor.moveToFirst()) {
                    newVVI.thumbPath = thumbCursor.getString(thumbCursor
                            .getColumnIndex(MediaStore.Video.Thumbnails.DATA));
                    Log.v("", newVVI.thumbPath);
                }

                newVVI.filePath = cursor.getString(cursor
                        .getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
                newVVI.title = cursor.getString(cursor
                        .getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));
                Log.v("", newVVI.title);
                newVVI.mimeType = cursor
                        .getString(cursor
                                .getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE));
                Log.v("", newVVI.mimeType);
                videoRows.add(newVVI);
            } while (cursor.moveToNext());
        }
        _videoRows = videoRows;
        myGallery.addView(insertPhoto(videoRows));
    }

    View insertPhoto(ArrayList<VideoViewInfo> videoRows) {
        LinearLayout layout = new LinearLayout(getApplicationContext());
        layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 250));
        layout.setGravity(Gravity.CENTER);
        for (int i = 0; i < videoRows.size(); i++) {

            Bitmap bmThumbnail;

            bmThumbnail = ThumbnailUtils.createVideoThumbnail(
                    videoRows.get(i).filePath, Thumbnails.MICRO_KIND);

            ImageView imageView = new ImageView(getApplicationContext());
            imageView.setLayoutParams(new LayoutParams(220, 220));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            // imageView.setBackgroundResource(R.drawable.canada);
            imageView.setImageBitmap(bmThumbnail);
            imageView.setPadding(20, 40, 20, 40);
            imageView.setTag(videoRows.get(i).filePath);

            imageView.setOnClickListener(this);
            layout.addView(imageView);

        }

        return layout;
    }

    @Override
    public void onClick(View v) {
        if (mVideoView.isPlaying() == true) {
            mVideoView.stopPlayback();
        }
        Toast.makeText(getApplicationContext(), v.getTag().toString(),
                Toast.LENGTH_SHORT).show();

        String filepath = v.getTag().toString();
        play(filepath);

    }

    public void play(String index) {

        Uri videoUri = Uri.parse(index);
        MediaController mc = new MediaController(this);

        mc.setAnchorView(mVideoView);
        mVideoView.setMediaController(mc);
        mVideoView.setVideoURI(videoUri);
        mc.setMediaPlayer(mVideoView);
        mVideoView.requestFocus();
        mVideoView.start();
    }

    public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth,
            int reqHeight) {
        Bitmap bm = null;

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        bm = BitmapFactory.decodeFile(path, options);

        return bm;
    }

    public int calculateInSampleSize(

    BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            if (width > height) {
                inSampleSize = Math.round((float) height / (float) reqHeight);
            } else {
                inSampleSize = Math.round((float) width / (float) reqWidth);
            }
        }

        return inSampleSize;
    }

}

galary 类的 xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="none" >

    <LinearLayout
        android:id="@+id/mygallery"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" />
</HorizontalScrollView>

<VideoView
    android:id="@+id/videoView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

视频视图信息类

class VideoViewInfo {
    String filePath;
    String mimeType;
    String thumbPath;
    String title;
}

注意:代码需要根据逻辑进行一些小的修改。但它会做你正在寻找的......我会尽快更新它......直到那时欢呼......更新:代码更新工作正常。

于 2013-06-27T14:01:02.140 回答