給Android VideoView一個文件目錄,就能夠直接播放智能設備中的視頻文件,如今以播放事先用手機拍好並重命名的視頻文件test.mp4爲例。java
(1) 須要在佈局文件中寫一個ViedoView:android
<RelativeLayout 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" tools:context="com.example.videoview.MainActivity" > <VideoView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/videoView" /> </RelativeLayout>
(2)不要忘記在AndroidManifest.xml文件中添加讀寫外部存儲的權限:ide
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
(3)在java代碼中給VideoView設置文件目錄,而後start播放:佈局
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); VideoView videoView = (VideoView) findViewById(R.id.videoView); // 得到的path等於:/storage/emulated/0/DCIM File path = Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); // 拼接完整路徑 File f = new File(path, "/Camera/test.mp4"); // 此時的f.getAbsolutePath()=/storage/emulated/0/DCIM//\Camera/test.mp4 videoView.setVideoPath(f.getAbsolutePath()); // 開始播放視頻 videoView.start(); // VideiView獲焦點 // videoView.requestFocus(); } }
以上!另外對APP進行全方位的檢測,我都會用這個:www.ineice.com。spa