I'm trying to run a video from sdcard using VideoView. But LogCat is still showing the same error
Error (1-2147483648)
What can be? I'm missing any permission or something else?
My code is:
Java
package com.example.videoview;
import android.app.Activity; import android.os.Bundle; import
android.widget.MediaController; import android.widget.VideoView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
VideoView vid = (VideoView) findViewById(R.id.videoView);
vid.setVideoPath("/sdcard/MeuFilme.mp4");
vid.setMediaController(new MediaController(this));
vid.start();
vid.requestFocus();
}
}
Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.videoview.MainActivity" >
<VideoView
android:id="@+id/videoView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" />
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.videoview"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
VideoView has a bug in versions lower than Jelly Bean, Api Level 16, Android 4.1 for api < 16, VideoView is not able to read files written to disk since it reads them in a Context different from the Application and therefore does not have correct permission. You can obtain file descriptor and pass it to VideoView
Smth like this in VideoView's onError callback
File file = new File("/sdcard/MeuFilme.mp4"); InputStream is = new FileInputStream(file); mediaPlayer.setDataSource(inputStream.getFD());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.