这有可能在Android中像图库一样制作视频库

时间:2013-06-26 12:31:32

标签: java android

如何制作像图库一样的视频库。我们在图片库中提供了大量的可绘制内容,这样我们可以为视频库提供大量的网址。对于ex,这是图片库Relavant Link http://www.edumobile.org/android/android-development/image-gallery-example-in-android/ 像这样幻灯片分享视频库我们如何制作..

1 个答案:

答案 0 :(得分:7)

  

我的主要gallay课程

  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" />

  

类VideoViewInfo

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

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