我是Android开发的新手。我需要创建视频文件浏览器,视频文件必须显示为缩略图,支持上下滑动。我应该从什么开始?谢谢,等待您的评论..
这是您问题的完整解决方案。
public class EntertainmentActivity extends Activity {
private static final String MEDIA_PATH = new String( "/mnt/sdcard/VideoFolder");
static String[] fileList = null;
GridView gridView;
static String FILE_PATH = "/mnt/sdcard/VideoFolder/";
String MiME_TYPE = "video/mp4";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
updateSongList();
gridView = (GridView) findViewById(R.id.gridView1);
File dir = new File(MEDIA_PATH);
if (dir.exists()) {
} else {
dir.mkdirs();
}
if (fileList != null) {
gridView.setAdapter(new ImageAdapter(this, fileList));
}
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
String videoFilePath = FILE_PATH + fileList[position];
System.out.println("******************************videoFilePath****************" + videoFilePath);
System.out.println("******************************MiME_TYPE****************"+ MiME_TYPE);
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
File newFile = new File(videoFilePath);
intent.setDataAndType(Uri.fromFile(newFile), MiME_TYPE);
startActivity(intent);
}
});
}
public void updateSongList() {
File videoFiles = new File(MEDIA_PATH);
Log.d("*********Value of videoFiles******", videoFiles.toString());
if (videoFiles.isDirectory()) {
fileList = videoFiles.list();
}
if (fileList == null) {
System.out.println("File doesnot exit");
Toast.makeText(this, "There is no file please add files to "+MEDIA_PATH, Toast.LENGTH_SHORT).show();
} else {
System.out.println("fileList****************" + fileList);
for (int i = 0; i < fileList.length; i++) {
Log.e("Video:" + i + " File name", fileList[i]);
}
}
}
网格视图适配器
public class ImageAdapter extends BaseAdapter {
private Context context;
private final String[] VideoValues;
public ImageAdapter(Context context, String[] VideoValues) {
this.context = context;
this.VideoValues = VideoValues;
}
public View getView(int position, View convertView, ViewGroup parent) {
System.out.println("***********IngetView************");
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from gridlayout.xml
gridView = inflater.inflate(R.layout.grid_row, null);
// set value into textview
TextView textView = (TextView) gridView
.findViewById(R.id.grid_item_label);
textView.setText(EntertainmentActivity.fileList[position]);
System.out.println("value of fileList[position]" + EntertainmentActivity.fileList[0]);
// set image
ImageView imageThumbnail = (ImageView) gridView
.findViewById(R.id.grid_item_image);
Bitmap bmThumbnail;
System.out
.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>> file path>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
+ EntertainmentActivity.fileList[position]);
bmThumbnail = ThumbnailUtils
.createVideoThumbnail(EntertainmentActivity.FILE_PATH + EntertainmentActivity.fileList[position],
MediaStore.Video.Thumbnails.MINI_KIND);
if (bmThumbnail != null) {
System.out
.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>> THUMB NAIL>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
imageThumbnail.setImageBitmap(bmThumbnail);
} else {
System.out
.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>NO THUMB NAIL>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
}
} else {
gridView = (View) convertView;
}
return gridView;
}
public int getCount() {
// return 0;
return VideoValues.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
}
主.xml布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff99cc00" >
<GridView
android:id="@+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="200dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="4"
android:padding="10dp"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" >
</GridView>
</RelativeLayout>
grid_row.xml行布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="320dp"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="vertical" >
<ImageView
android:id="@+id/grid_item_image"
android:layout_width="320dp"
android:layout_height="300dp" />
<TextView
android:id="@+id/grid_item_label"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#92d050"
android:ellipsize="marquee"
android:gravity="center"
android:padding="3dp"
android:singleLine="true"
android:text="test string"
android:textColor="@android:color/white" />
</LinearLayout>
安卓清单.xml
不要忘记将用户权限放在 Android Manifest 中
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />