我在 android 中实现了一个视频播放,我对 android 来说是全新的,这是我迄今为止收集的一些代码。
根据它应该播放视频的逻辑。不知道我在哪里做错了。
package com.themetanoia.readfilefromsdc;
import java.io.File;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;
import android.widget.AdapterView.OnItemClickListener;
public class ReadFileFromSDCActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Get the file path for external storage
String a = Environment.getExternalStorageDirectory().getAbsolutePath();
final String finalPath = a+"/Videos";
//Define new file function
File f=new File(finalPath);
if(f.isDirectory()){
//Define arrayadapter
setListAdapter(new ArrayAdapter<String>(this, R.layout.listview,files));
//Define listview
ListView listview = getListView();
listview.setTextFilterEnabled(true);
//Onclick list item event
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//get selected items
String selectedValue = (String) getListAdapter().getItem(position);
Uri uri = Uri.parse(finalPath);
MediaController mediaController = new MediaController(getBaseContext());
mediaController.setAnchorView(mVideoView);
Uri video = Uri.parse(finalPath+"/"+selectedValue);
mVideoView.setMediaController(mediaController);
mVideoView.setVideoURI(video);
mVideoView.start();
}
});
}
}
}