Play video file from internal storage android using VideoView

40,676

Solution 1

Copy it to External Storage (Temporarily)

I ran into the same issue as you and resorted to just simply copying the file temporarily to external storage before playing it and then deleting the temporary file afterwards.

Here is some sample code that I used to do this:

try {
  // Copy file to temporary file in order to view it.
  temporaryFile = generateTemporaryFile(file.getName());
  FileUtils.copyFile(file, temporaryFile);
  previewVideo(temporaryFile, videoView);

} catch (IOException e) {
  e.printStackTrace();
}


# Helpers

protected File generateTemporaryFile(String filename) throws IOException {
  String tempFileName = "20130318_010530_";

  File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

  File tempFile = File.createTempFile(
      tempFileName,       /* prefix     "20130318_010530" */
      filename,           /* filename   "video.3gp" */
      storageDir          /* directory  "/data/sdcard/..." */
  );

  return tempFile;
}

protected void previewVideo(File file, VideoView videoView) {
  videoView.setVideoPath(file.getAbsolutePath());

  MediaController mediaController = new MediaController(this);

  videoView.setMediaController(mediaController);

  mediaController.setMediaPlayer(videoView);

  videoView.setVisibility(View.VISIBLE);

  videoView.start();
}

Solution 2

please try this. I have explained the procedure to play video from raw folder on this link: Video player not workig! . On that, if you modify

Uri uri = Uri.parse("android.resource://" + getPackageName() + "/"+R.raw.VideoName);

with

Uri uri = Uri.parse(Environment.getExternalStorageDirectory()+"<path to your video>");

For example:

Uri uri = Uri.parse(Environment.getExternalStorageDirectory()+"/dcim/camera/2012-05-15_17-50-39_319.3gp");

I think will solve your issue. Remember to give the necessary permissions on the manifest.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Solution 3

A little late to the game here, but if you add a FileProvider to your app, you can then create URIs that VideoView can utilize.

It's actually pretty trivial to do and once you are done, you can access your files like:

Uri video = FileProvider.getUriForFile(context, context.getPackageName(), new File(myAbsoluteVideoPath));

myVideoView.setVideoURI(video);

This can be used to play videos that you generated in your app and saved on the device.

Share:
40,676
baradas
Author by

baradas

Updated on March 10, 2021

Comments

  • baradas
    baradas about 3 years

    Am trying to play a video stored in the internal storage in Android. However, no matter what I do it keeps coming back with either a -1 error or a -38 error. Both seem to be rather generic errors hence are not much intelligible.

    I am wondering if it is possible to use a VideoView and not a MediaPlayer instance to play a video file from the local storage.

    The steps involved in my app include,

    1. downloading a file from a remote url
    2. Storing the file in internal storage (note i use the convention for ensuring that it has global read permissions. i.e

      openFileOutput(file_name, Context.MODE_WORLD_READABLE);
      
    3. Reading the media file back at a later point from this location, and playing it in a videoView.

      String filePath = "file://" + getFilesDir()+File.separator+file_name;
      Uri videoUri = Uri.parse(filePath);
      Log.d("Video Player", filePath);
      videoPlayer.setVideoURI(videoUri);
      

    I also went through other links in StackOverflow which point out that I need to implement a CustomContentProvider to be able to read these files. Is there a direct way of accessing the file uri and setting it to the videoView without having to resorting to creating a custom content provider and using a mediaPlayer instead of a videoView.

    Other StackOverflow references used

    1. Android - Load video from private folder of app
    2. Can a videoview play a video stored on internal storage?
  • baradas
    baradas about 12 years
    Hi, this is fine, when I store my videos on an external storage how do I access videos stored in a path within the device internal storage. Note I am not storing the video in the raw folder of the app.
  • baradas
    baradas about 12 years
    Update : Tried the above, but no luck with this. The video still refuses to play when I try to access it. I have checked the video permissions in the dab shell and they seem to be ok.
  • Basil
    Basil about 12 years
    @baradas where are you storing the video's actually? Is it on the SD Card?
  • Abdul Rahman K
    Abdul Rahman K over 8 years
    still this is the only solution possible
  • Joshua Pinter
    Joshua Pinter over 5 years
    @NoumanCh Added some rough code snippets to help you along the path. Let me know if that helps.
  • Nouman Ch
    Nouman Ch over 5 years
    buddy I'm using internal storage because my file is supposed to be secure like youtube downloaded videos. I think this way will ruin up my security. what you say?
  • Joshua Pinter
    Joshua Pinter over 5 years
    @NoumanCh Yeah buddy. If you find a more secure solution, make sure to let the rest of us know.
  • Nouman Ch
    Nouman Ch over 5 years
    sure bro. I'll.
  • Lutaaya Huzaifah Idris
    Lutaaya Huzaifah Idris almost 4 years
    What if am using this path : context?.getExternalFilesDir(Environment.DIRECTORY_MOVIES) Can I still pass it as myAbsoluteVideoPath, because it has the package name already ?
  • Chirag Kalra
    Chirag Kalra over 3 years
    I don't wanna use unnecessary permissions.
  • Thien Ngan
    Thien Ngan about 2 years
    This is what I'm looking for, I did miss out the permission on the manifest.