android 开发 文件打开为自己开发app操作步骤代码

android开发中 自己播放器  MP4文件打开方式选择自己开发app实现步骤。红色重要具体实现方法

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="activiity.date.gfonda.com.fonda">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-feature
        android:name="android.hardware.touchscreen"
        android:required="false" />
    <uses-feature
        android:name="android.software.leanback"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.Leanback">
        <activity
            android:name=".MainActivity"
            android:banner="@mipmap/ic_launcher"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:logo="@mipmap/ic_launcher"
            android:screenOrientation="landscape">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.VIEW"></action>
                <category android:name="android.intent.category.DEFAULT"></category>
                <data android:mimeType="video/mp4"></data>
            </intent-filter>

        </activity>

    </application>

</manifest>

  

/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package activiity.date.gfonda.com.fonda;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.MediaController;
import android.widget.VideoView;

/*
 * MainActivity class that loads MainFragment
 */
public class MainActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    private String videopath;
    private VideoView videoview;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainactivity);

        videoview=findViewById(R.id.videoView2);


        Intent intent = getIntent();
        String action = intent.getAction();//action
        String type = intent.getType();//类型

        if (Intent.ACTION_VIEW.equals(action) && type != null && "video/mp4".equals(type)) {

           // Uri uri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
            //如果是媒体类型需要从数据库获取路径
            //String filePath=getRealPathFromURI(uri);
            Uri uri = intent.getData();
            String str = getRealPathFromURI(uri);
//            AlertDialog.Builder builder  = new AlertDialog.Builder(MainActivity.this);
//            builder.setTitle("确认" ) ;
//            builder.setMessage(str) ;
//            builder.setPositiveButton("是" ,  null );
//            builder.show();
            videoview.setVideoPath(str);
        }else {

        videopath="file:///android_asset/sp.mp4";

        //videoview.setVideoPath(videopath);

        String uris = "android.resource://" + getPackageName() + "/" + R.raw.sp;
     //  String uri="";
        videoview.setVideoPath(uris);
        }
        videoview.requestFocus();
        //创建MediaController对象
        MediaController mediaController = new MediaController(this);
        videoview.setMediaController(mediaController);
        videoview.start();
        //监听视频播放完的代码
        videoview.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mPlayer) {
                // TODO Auto-generated method stub
                mPlayer.start();
                mPlayer.setLooping(true);
            }
        });
    }
    /**
     * 通过Uri获取文件在本地存储的真实路径
     */
    private String getRealPathFromURI(Uri contentUri) {
        String[] proj = {MediaStore.MediaColumns.DATA};
        Cursor cursor=getContentResolver().query(contentUri, proj, null, null, null);
        if(cursor.moveToNext()){
            return cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA));
        }
        cursor.close();
        return null;
    }
}

  

猜你喜欢

转载自www.cnblogs.com/woaijingjing/p/11127590.html
今日推荐