Question: I have a weird error message and can’t find solution. Project: Android with Eclipse IDE and Android SDK, min. API level 10, source code is attached. Problem: I can play none of my downloaded and saved media files from the application’s own data folder (/data/data/com.mypackage/myfile.mp4) by the MediaPlayer class. I double checked the video format. It can be even played when I place it into the /mnt/sdcard/… folder. Android shows a message saying “Cannot Play Video: Sorry, this video cannot be played”. In the log file I can find an error: MediaPlayer error(1, -2147483648) and Prepare failed. status=0×1. What’s the issue?

Answer: The typical errors are as follows:

1. File path is in error. Incorrect directory or Url or Uri found.

2. Media file is in error, incompatible format.

3. Missing permissions.

In Android MediaPlayer mp = new MediaPlayer(); does not create your apps’s own object. It uses “another” application and requires world-readable permissions for the application’s local media files. “World-readable” means another application can see it. You can view the permissions of these files on the DDMS window in Eclipse IDE:

Set file permissions to Context.MODE_WORLD_READABLE or Context.MODE_WORLD_WRITEABLE

File permission in data/data/yourapp/files folder

When your app is in error, you will probably see that “-rw——” gray line, which means that only your application has read/write permissions on your file(s).

For applications targetting SDK versions greater than Android 2.3, these permission flags can be explicitly set if desired. See File -> setReadable(,), or FileOutputStream documentation for hints:

1
2
3
4
5
6
    // context = Context, FileName=mediafile, like myfile.mp4
    File file = context.getFileStreamPath(FileName);
    if (file.exists()) {
        // Set to Readable and MODE_WORLD_READABLE
        file.setReadable(true, false);
    }

Call openFileOutput() with the name of the file and the operating mode. This returns a FileOutputStream:

1
2
3
4
5
6
7
8
9
10
11
12
13
    // Sample: copy myfile.mp4 from 'assets' to internal /data/data/... folder
    FileOutputStream outputStream = context.openFileOutput("myfile.mp4", Context.MODE_WORLD_READABLE);
    InputStream inputStream = context.getAssets().open("myfile.mp4");
    // Copy data
    byte[] buffer = new byte[8192];
    int length;
    while ( (length=inputStream.read(buffer)) > 0) {
        outputStream.write(buffer, 0, length);
    }
    // Close the streams
    inputStream.close();
    outputStream.flush();
    outputStream.close();