Can a videoview play a video stored on internal storage?
MediaPlayer requires that the file being played has world-readable permissions. You can view the permissions of the file with the following command in adb shell:
ls -al /data/data/com.mypackage/myfile
You will probably see "-rw------", which means that only the owner (your app, not MediaPlayer) has read/write permissions.
Note: Your phone must be rooted in order to use the ls command without specifying the file (in the internal memory).
If your phone is rooted, you can add world-read permissions in adb shell with the following command:
chmod o+r /data/data/com.mypackage/myfile
If you need to modify these permissions programmatically (requires rooted phone!), you can use the following command in your app code:
Runtime.getRuntime().exec("chmod o+r /data/data/com.mypackage/myfile");
Which is basically a linux command. See https://help.ubuntu.com/community/FilePermissions for more on chmod.
EDIT: Found another simple approach here (useful for those without rooted phones). Since the application owns the file, it can create a file descriptor and pass that to mediaPlayer.setDataSource():
FileInputStream fileInputStream = new FileInputStream("/data/data/com.mypackage/myfile");mediaPlayer.setDataSource(fileInputStream.getFD());
This approach avoids the permission issue completely.
You can use:
videoView.setVideoURI(Uri.parse(file.getAbsolutePath()));
if the file is world readable
Or you can use a content provider
For detail check this tutorial
public class AndroidVideoViewExample extends Activity { private VideoView myVideoView; private int position = 0; private ProgressDialog progressDialog; private MediaController mediaControls; @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); // set the main layout of the activity setContentView(R.layout.activity_main); //set the media controller buttons if (mediaControls == null) { mediaControls = new MediaController(AndroidVideoViewExample.this); } //initialize the VideoView myVideoView = (VideoView) findViewById(R.id.video_view); // create a progress bar while the video file is loading progressDialog = new ProgressDialog(AndroidVideoViewExample.this); // set a title for the progress bar progressDialog.setTitle("JavaCodeGeeks Android Video View Example"); // set a message for the progress bar progressDialog.setMessage("Loading..."); //set the progress bar not cancelable on users' touch progressDialog.setCancelable(false); // show the progress bar progressDialog.show(); try { //set the media controller in the VideoView myVideoView.setMediaController(mediaControls); //set the uri of the video to be played myVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.kitkat)); } catch (Exception e) { Log.e("Error", e.getMessage()); e.printStackTrace(); } myVideoView.requestFocus(); //we also set an setOnPreparedListener in order to know when the video file is ready for playback myVideoView.setOnPreparedListener(new OnPreparedListener() { public void onPrepared(MediaPlayer mediaPlayer) { // close the progress bar and play the video progressDialog.dismiss(); //if we have a position on savedInstanceState, the video playback should start from here myVideoView.seekTo(position); if (position == 0) { myVideoView.start(); } else { //if we come from a resumed activity, video playback will be paused myVideoView.pause(); } } }); } @Override public void onSaveInstanceState(Bundle savedInstanceState) { super.onSaveInstanceState(savedInstanceState); //we use onSaveInstanceState in order to store the video playback position for orientation change savedInstanceState.putInt("Position", myVideoView.getCurrentPosition()); myVideoView.pause(); } @Override public void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); //we use onRestoreInstanceState in order to play the video playback from the stored position position = savedInstanceState.getInt("Position"); myVideoView.seekTo(position); }}