SurfaceView播放视频:
public class SurfaceActivity extends Activity implements SurfaceHolder.Callback {
/** Called when the activity is first created. */
MediaPlayer player;
SurfaceView surface;
SurfaceHolder surfaceHolder;
Button play,pause,stop;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
play=(Button)findViewById(R.id.button1);
pause=(Button)findViewById(R.id.button2);
stop=(Button)findViewById(R.id.button3);
surface=(SurfaceView)findViewById(R.id.surface);
surfaceHolder=surface.getHolder();//SurfaceHolder是SurfaceView的控制接口
surfaceHolder.addCallback(this); //因为这个类实现了SurfaceHolder.Callback接口,所以回调参数直接this
surfaceHolder.setFixedSize(320, 220);//显示的分辨率,不设置为视频默认
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//Surface类型
play.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
player.start();
}});
pause.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
player.pause();
}});
stop.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
player.stop();
}});
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
//必须在surface创建后才能初始化MediaPlayer,否则不会显示图像
player=new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDisplay(surfaceHolder);
//设置显示视频显示在SurfaceView上
try {
player.setDataSource("/sdcard/3.mp4");
player.prepare();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if(player.isPlaying()){
player.stop();
}
player.release();
//Activity销毁时停止播放,释放资源。不做这个操作,即使退出还是能听到视频播放的声音
}
}
VideoView播放视频:
videoView = (VideoView) this.findViewById(R.id.videoView);
MediaController mc = new MediaController(this);
videoView.setMediaController(mc);
//videoView.setVideoURI(Uri.parse(""));
videoView.setVideoPath("/sdcard/test.mp4");
videoView.requestFocus();
videoView.start();
读取assets:
(a)Context.getAssets().open(“test.txt”);
(b)AssetManager assetManager = getAssets();
InputStream is = assetManager.open("jin.txt");
读取drawable下的图片:
//1,已将图片保存到drawable目录下 //通过图片id获得Drawable Resource res=gerResource(); Drawable drawable=res.getDrawable(id);//id为R.drawable.图片名称 //通过图片id获得Bitmap Resource res=gerResource(); Bitmap bitmap=BitmapFactory.decodeResource(res, id); //若只知道图片的名称,可以通过图片的名称获得图片的id //name:图片的名称 defType:图片的类型(png,jpeg),defPackage:工程的包名 int id =int id =res.getIdentifier(name, defType, defPackage); //获得id之后可以根据你的需要来获得Bitmap或Drawable //2,已将图片保存到assest目录下 //知道图片的名称,通过inputstream打开图片 AssetManager asm=getAssetMg(); InputStream is=asm.open(name);//name:图片的名称 //获得Drawable Drawable da = Drawable.createFromStream(is, null); //获得Bitmap Bitmap bitmap=BitmapFactory.decodeStream(is); //3,图片保存在sdcard,已知图片的路径 //图片路径 String path = Environment.getExternalStorageDirectory().toString()+ "/DCIM/device.png"; RandomAccessFile mMiniThumbFile; File imgfile = new File(path); try { mMiniThumbFile = new RandomAccessFile(imgfile, "rw"); } catch (IOException ex) { // Open as read-only so we can at least read the existing // thumbnails. try { mMiniThumbFile = new RandomAccessFile(imgfile, "r"); } catch (IOException ex2) { // ignore exception System.out.println(ex2.toString()); } } data= new byte[10553]; try { mMiniThumbFile.seek(0); int got = mMiniThumbFile.read(data, 0, 10552); System.out.println("got="+got); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); System.out.println(e.toString()); } if (data != null) { //通过data获得bitmap Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,data.length);