【android】 Unable to open content: file:///sdcard/hello.3gp 3gp视频不能播放

【报错】
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)
………….很多
【android】 Unable to open content: file:///sdcard/hello.3gp 3gp视频不能播放_第1张图片

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

访问sd卡中数据需要权限。
【更改后结果】可以正常播放
【android】 Unable to open content: file:///sdcard/hello.3gp 3gp视频不能播放_第2张图片

【下面分享代码】
【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>

以上代码由[email protected]提供。

如有补充欢迎评论。

你可能感兴趣的:(android,视频)