main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="onClick" android:text="播放影片" /> </LinearLayout>PlayVideoActivity
public class PlayVideoActivity extends Activity { private static final String TAG = "PlayVideoActivity"; private static final int REQUEST_CODE = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onClick(View v) { // 選取要播放的影片 Intent it = new Intent(Intent.ACTION_GET_CONTENT); it.setType("video/*"); // 以要取得結果的方式呼叫其他 activity this.startActivityForResult(it, REQUEST_CODE); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode != RESULT_OK) { Log.d(TAG, "選檔失敗"); return; } if (requestCode != REQUEST_CODE) { Log.d(TAG, "不是選影片"); return; } // 取得影片資料 Uri uri = data.getData(); Log.d(TAG, "播放影片:" + uri.getPath()); // 以預設的播放程式播放 Intent it = new Intent(Intent.ACTION_VIEW); it.setData(uri); // 不管結果的方式呼叫其他 activity this.startActivity(it); } }相關文章
沒有留言:
張貼留言