【报错】
sd卡对应路径中已放置相关视频,但运行还是会报下面的错误:
VideoView: Unable to open content: file:///sdcard/hello.3gp
java.io.FileNotFoundException: /sdcard/hello.3gp: open failed: EACCES (Permission denied)
at libcore.io.IoBridge.open(IoBridge.java:452)
at java.io.FileInputStream.(FileInputStream.java:76)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1095)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1046)
at android.media.MediaPlayer.setDataSource(MediaPlayer.java:992)
at android.widget.VideoView.openVideo(VideoView.java:346)
at android.widget.VideoView.setVideoURI(VideoView.java:256)
at android.widget.VideoView.setVideoURI(VideoView.java:239)
………….很多

【先说解决方法】
少了一个访问sd卡的权限 在AndroidManifest.xml文件中加入
允许应用程序读取扩展存储器
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

访问sd卡中数据需要权限。
【更改后结果】可以正常播放

【下面分享代码】
【java】

package irdc.ex07_13;import android.app.Activity;import android.graphics.PixelFormat;import android.media.MediaPlayer;import android.net.Uri;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.MediaController;import android.widget.TextView;import android.widget.Toast;import android.widget.VideoView;public class EX07_13 extends Activity {  private TextView mTextView01;  private VideoView mVideoView01;  private String strVideoPath = "";  private Button mButton01, mButton02;  private String TAG = "HIPPO_VIDEOVIEW";  /* 预设判别sd卡存在flag為false */  private boolean bIfSDExist = false;  /** Called when the activity is first created. */  @Override  public void onCreate(Bundle savedInstanceState)  {    super.onCreate(savedInstanceState);    /* 全屏幕 */    getWindow().setFormat(PixelFormat.TRANSLUCENT);    setContentView(R.layout.main);    /* 判断sd卡是否存在 */    if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))    {      bIfSDExist = true;    }    else    {      bIfSDExist = false;      mMakeTextToast              (                      getResources().getText(R.string.str_err_nosd).toString(),                      true              );    }    mTextView01 = (TextView)findViewById(R.id.myTextView1);    mVideoView01 = (VideoView)findViewById(R.id.myVideoView1);    /* 延伸学会 */    mVideoView01.setOnPreparedListener(new MediaPlayer.OnPreparedListener()    {      @Override      public void onPrepared(MediaPlayer mp)      {        // TODO Auto-generated method stub        mTextView01.setText(strVideoPath);      }    });    mVideoView01.setOnCompletionListener(new MediaPlayer.OnCompletionListener()    {      @Override      public void onCompletion(MediaPlayer arg0)      {        // TODO Auto-generated method stub        mMakeTextToast                (                        getResources().getText(R.string.str_complete).toString(),                        true                );      }    });    mButton01 = (Button)findViewById(R.id.myButton1);    mButton02 = (Button)findViewById(R.id.myButton2);    mButton01.setOnClickListener(new Button.OnClickListener()    {      @Override      public void onClick(View arg0)      {        // TODO Auto-generated method stub        if(bIfSDExist)        {          strVideoPath = "file:///sdcard/hello.3gp";          playVideo(strVideoPath);        }      }    });    mButton02.setOnClickListener(new Button.OnClickListener()    {      @Override      public void onClick(View arg0)      {        // TODO Auto-generated method stub        if(bIfSDExist)        {          /* 延伸学会 */          //resetVideo();          strVideoPath = "file:///sdcard/test.3gp";          playVideo(strVideoPath);        }      }    });  }  private void playVideo(String strPath)  {    if(strPath!="")    {      /* 呼叫VideoURI方法,指定解析路径 */      mVideoView01.setVideoURI(Uri.parse(strPath));      /* 设定控制Bar显示于此Context中 */      mVideoView01.setMediaController(new MediaController(EX07_13.this));      mVideoView01.requestFocus();      /* 呼叫VideoView.start()自动播放 */      mVideoView01.start();      if(mVideoView01.isPlaying())      {        /* 下程式不会执行,因start()后尚需要preparing() */        mTextView01.setText("Now Playing:"+strPath);        Log.i(TAG, strPath);      }    }  }  /* private void resetVideo() { if(mVideoView01!=null) { mVideoView01.seekTo(0); } } */  public void mMakeTextToast(String str, boolean isLong)  {    if(isLong==true)    {      Toast.makeText(EX07_13.this, str, Toast.LENGTH_LONG).show();    }    else    {      Toast.makeText(EX07_13.this, str, Toast.LENGTH_SHORT).show();    }  }}

【xml】
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/white"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/myTextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@drawable/blue"
android:text="@string/hello"
/>
<VideoView
android:id="@+id/myVideoView1"
android:layout_width="320px"
android:layout_height="240px"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/myButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_button1" />
<Button
android:id="@+id/myButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_button2" />
</LinearLayout>
</LinearLayout>

以上代码由george_hsieh@qq.com提供。

如有补充欢迎评论。

更多相关文章

  1. android sd卡读写 附源码
  2. NDK编译:fatal error: GLES2/gl2platform.h: No such file or dir
  3. Android(安卓)拍照 以及从本地选择图片 上传
  4. Error: Error parsing D:\android-sdk-windows\sdk\system-im
  5. android开机自动唤醒屏幕、打开锁屏页并启动app
  6. Android(安卓)Permission denied 错误 ( 附Android权限大全 )
  7. Android(安卓)apk打包过程
  8. 在Android应用中实现Google搜索的例子
  9. Android(安卓)重读官方文档 4 SharedPreferences

随机推荐

  1. Android Studio v0.1尝鲜
  2. android 如何依赖android:sharedUserId更
  3. Android入门篇
  4. java websocket client ssl(wss)
  5. Shake Android ui elements
  6. JS判断手机操作系统(ios或android)并跳转到
  7. Android ORM框架GreenDao用法
  8. Android(安卓)databinding 双向绑定(Demo)
  9. 让android应用程序获得system权限
  10. android 中 涉及到context的时候this和th