【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)

………….很多

【先说解决方法】

少了一个访问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>

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

如有补充欢迎评论。

时间: 2024-07-31 20:38:01

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

Android学习笔记——文件路径(/mnt/sdcard/...)、Uri(content://media/external/...)学习

一.URI 通用资源标志符(Universal Resource Identifier, 简称"URI"). Uri代表要操作的数据,Android上可用的每种资源 - 图像.视频片段等都可以用Uri来表示. URI一般由三部分组成: 访问资源的命名机制. 存放资源的主机名. 资源自身的名称,由路径表示. Android的Uri由以下三部分组成: "content://".数据的路径.标示ID(可选) 举些例子,如: 所有联系人的Uri: content://con

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ex.activity/com.ex.activity.LoginActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ex.activity/com.ex.activity.LoginActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class 异常解决方案: 是因为设置background的图片太大了,造成了内存溢出,在Activity设置onCreate

Unable to start activity ComponentInfo{com.example.administrator.myapplication/com.example.administrator.myapplication.MainActivity}: android.view.InflateException: Binary XML file line #0: Binary XM

本来就是把fragment写死在activity的xml模板里面,结果报了这个错误, Unable to start activity ComponentInfo{com.example.administrator.myapplication/com.example.administrator.myapplication.MainActivity}: android.view.InflateException: Binary XML file line #0: Binary XM,,下面找到了解

eclipse升级Android SDK Tool版本到25.2.5后运行项目报错Unable to build: the file dx.jar was not loaded from the SDK folder

概述 由于最近通过SDK-Manager更新了build-tools,当要用到dx.jar这个包时,自动调用最新版本Android SDK build-tools中dx.jar,但是运行android项目时Console却提示: 解决方案 解决办法就是:确保dx.jar这个文件在build-tools\lib和选择的Andriod SDK Tools版本一致就可以. 我用的 Android SDk build-tools 最新版本是 28.0.3,安装的Andriod SDK Tools版本是2

Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class android.support.design.widget.TabLayout,TableLayout引起页面崩溃

在使用TableLayout的时候,运行引用程序直接Crash. FATAL EXCEPTION: main Process: com.edaixi.activity, PID: 9703 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.edaixi.activity/com.edaixi.activity.PriceCenterActivity}: android.view.InflateExcept

Android mediaplayer Couldn&#39;t open file on client side, trying server side

D/MediaPlayer( 2886): Couldn't open file on client side, trying server sideD/ActivityManager( 2624): checkComponentPermission() adjusting {pid,uid} to {2132,1013}E/MediaPlayerService( 2132): Couldn't open fd for content://media/external/audio/media/2

错误解决:android.view.InflateException: Binary XML file line #11: Error inflating class com.tony.timepicker.TimePicker

今天在做项目开发时遇到这么一个错误,完整的错误提示信息如下: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tony.timepicker/com.tony.timepicker.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class com.tony.timepicke

Android之內置、外置SDCard

From:http://blog.csdn.net/u011290399/article/details/10363881 在项目中,发现通过Android提供的API获取外置SDCard的操作一直不能成功,一直没有太多的时间解决该问题 昨天週末,终于可以抽空研究了一下这个问题了 在网上搜索下,结果百度的搜索引擎怎麽搜索也未能给我满足需求的搜索结果 不过,搜索出的内置SDCard与外置SDCard互换操作给出了思路 先看看SDCard的路径:/storage/extSdCard./storage

Unable to load template file &#39;rj\ThinkPHP/Tpl/dispatch_jump.tpl&#39;----thinkphp3.2.3

Unable to load template file 'rj\ThinkPHP/Tpl/dispatch_jump.tpl'----thinkphp3.2.3 1.报错原因:将thinkphp默认模板引擎改为smarty模板引擎,导致调用success()和error()方法失败. 2.解决方案一: 找到ThinkPHP\Library\Think\Controller.class.PHP文件中的protected function error()和protected function su