Download Video to Internal Storage

All your problems with Audio, Video and Images.

Download Video to Internal Storage

Postby sntflw » Wed Dec 19, 2012 4:42 pm

Hey guys,

I've been searching almost the whole internet for a solution to my problem. I'm downloading a video from my webserver (provided by PHP) and want to display it. Unfortunately the device I'm developing the application for does not have any external storage, thus I'm using the internal one.

My code looks like this:

Download & Store to temporary file:
Code: Select all
         Log.i(TAG, "Opening remote connection to " + mFile.getHost()
               + mFile.getPath());
         URLConnection c = mFile.openConnection();
         c.connect();
         final long lastModified = c.getLastModified();
         final String mExtension;
         if (c.getHeaderField("Content-Disposition") != null) {
            final String mFilename = c
                  .getHeaderField("Content-Disposition").split("=")[1];
            Log.i("Downloader", "Filename is " + mFilename
                  + ", split length is " + mFilename.split("\\.").length);
            mExtension = mFilename.split("\\.")[mFilename
                  .split("\\.").length - 1];
         } else {
            mExtension = "mp4";
         }
         if (lastModified > 0 && mLastUpdate > 0) {
            if (lastModified < mLastUpdate) {
               Log.i(TAG, "File was not modified. Not updating.");
               return;
            } else
               Log.i(TAG, "File is modified, updating..");
         } else
            Log.i(TAG,
                  "No last modified or last update set. Updating anyway.");

         InputStream is = c.getInputStream();

         Log.i(TAG, "Creating temporary local file");
         // create local temporary file
         FileOutputStream fos = mContext.openFileOutput(TMP_FILENAME,
               Context.MODE_WORLD_READABLE);

         // start reading
         byte[] buf = new byte[BUFFER_SIZE];
         int bytesRead = 0;
         int curRead = 0;
         Log.i(TAG, "Starting download.. to " + TMP_FILENAME);
         if (mDownloadChangeListener != null)
            mDownloadChangeListener.onDownloadStart();

         while ((curRead = is.read(buf)) > -1) {
            fos.write(buf);
            bytesRead += curRead;
         }
         Log.i(TAG, "Read " + bytesRead + " bytes in total.");
         Log.i(TAG, "Download finished!");
         // end of stream, tell app to rename file.
         if (mDownloadChangeListener != null)
            mDownloadChangeListener.onDownloadFinished(TMP_FILENAME,
                  mExtension);

         is.close();
         fos.close();


After successfully downloading the file, my listener does the following:

Code: Select all
   FileInputStream fis = openFileInput(Downloader.TMP_FILENAME);
               FileOutputStream fos = openFileOutput(Downloader.FILENAME
                     + ext, Context.MODE_WORLD_WRITEABLE);
               byte[] buf = new byte[2048];
               while (fis.read(buf) > 0) {
                  fos.write(buf);
               }
               fis.close();
               fos.close();
               deleteFile(Downloader.TMP_FILENAME);


(Before I check whether the previous video is still playing, thus this is executed before re-starting the video (is supposed to play in an infinite loop)

To play the video I use a slightly modified version of a VideoView, allowing me to set a FileDescriptor as Video source. I set the FileDescriptor as follows:

Code: Select all
            FileInputStream fid = new FileInputStream((getFilesDir() + "/"
                  + Downloader.FILENAME + mPrefs.getString(
                  "fileExtension", ".mp4")));

//            mVideoView.setVideoURI(Uri.parse("android.resource://" + getPackageName() + "/"
//                  + R.raw.test));
            mVideoView.setVideoFD(fid.getFD());


This is what I get:

12-19 14:33:48.074: W/MediaPlayer(5560): info/warning (3, 0)
12-19 14:33:48.074: I/MediaPlayer(5560): Info (3,0)
12-19 14:33:48.394: E/MediaPlayer(5560): error (1, -1007)
12-19 14:33:48.394: E/MediaPlayer(5560): Error (1,-1007)

I've already tried to simply give the absolute path to the videoview (after checking for its existence of course), thus setting it WORLD_READABLE, but it did not work neither. I've then read that the FD somehow passes the permissions which android seems to be pretty harsh about when it comes to internal storage, thus I'm using it.
The first lets say 50ms of the video play, then it stops with the error messages posted above.

If I on the other hand play the video from RAW resources, it works flawless...

Any ideas how to solve my problem?

I'd really appreciate any help!

Thank you.
sntflw
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Dec 19, 2012 4:35 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Yahoo [Bot] and 18 guests