10

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?
Community
  • 1
  • 1
baradas
  • 454
  • 1
  • 5
  • 14

4 Answers4

9

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();
}
Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245
6

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" />
Community
  • 1
  • 1
Basil
  • 2,163
  • 2
  • 16
  • 25
  • 2
    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 May 15 '12 at 13:56
  • 1
    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. – baradas May 16 '12 at 11:10
  • @baradas where are you storing the video's actually? Is it on the SD Card? – Basil May 17 '12 at 12:53
  • This is what I'm looking for, I did miss out the permission on the manifest. – Thien Ngan Apr 08 '22 at 12:15
2

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.

ekawas
  • 6,594
  • 3
  • 27
  • 39
  • 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 ? – Lutaaya Huzaifah Idris Jul 17 '20 at 21:21
0

Change targetSdkVersion to 27.

android ,

compileSdkVersion 30 
buildToolsVersion "30.0.3"

defaultConfig 
{
    applicationId "com.naved.playonmp3"
    minSdkVersion 18
    targetSdkVersion 27
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}