(Collect) How to fix broken insertion of recorded video into media content provider

Hi,

When you record a video from a VideoWidget in ODK Collect, the video is
copied into the instance folder AND inserted into the media content
provider. However, the record inserted into the media content provider
is broken, i.e. it somehow doesn't point to the correct file location.

When you go to Android's Gallery, you will find the copy of the video
(plus the original one) with its thumbnail, but when you try to play it
you get an error. If you look at the video details, it says "0.0 bytes"
and "unknown location".

I've already reported the bug
(http://code.google.com/p/opendatakit/issues/detail?id=523&thanks=523&ts=1329572386)
but I urgently need to fix it myself.

It seems like something goes wrong in this part of the VideoWidget code:

     File source = new File(binaryPath);
     File newVideo = new File(destVideoPath);
     FileUtils.copyFile(source, newVideo);
     if (newVideo.exists()) {
         // Add the copy to the content provier
         ContentValues values = new ContentValues(6);
         values.put(Video.Media.TITLE, newVideo.getName());
         values.put(Video.Media.DISPLAY_NAME, newVideo.getName());
         values.put(Video.Media.DATE_ADDED, System.currentTimeMillis());
         values.put(Video.Media.DATA, newVideo.getAbsolutePath());

         Uri VideoURI =

getContext().getContentResolver().insert(Video.Media.EXTERNAL_CONTENT_URI,
values);
Log.i(t, "Inserting VIDEO returned uri = " +
VideoURI.toString());
}

However, I can't understant where the problem is.
It's as if the DATA field was not inserted or its value was wrong, but
the code looks OK.......

In the log file, I can actually see the "Inserting VIDEO returned uri
..." debug text.

Any idea what the problem could be?? Does anybody know I can fix this?
Thanks in advance
m.

This is driving me crazy.

I've added some debug code to retrieve the DATA field of the newly
inserted video just after inserting it into the media database, and it
actually is identical to the new filename (as expected). However,
immediately after, I query the data base looking for a record with that
value of the DATA field and it doesn't find it:

This is my modified setBinaryData() in VideoWidget:

     File source = new File(binaryPath);
     File newVideo = new File(destVideoPath);
     FileUtils.copyFile(source, newVideo);
     	
     if (newVideo.exists()) {
         // Add the copy to the content provier
         ContentValues values = new ContentValues(6);
         values.put(Video.Media.TITLE, newVideo.getName());
         values.put(Video.Media.DISPLAY_NAME, newVideo.getName());
         values.put(Video.Media.DATE_ADDED, System.currentTimeMillis());
         values.put(Video.Media.DATA, newVideo.getAbsolutePath());
         Log.d(t,"Inserting VIDEO: data: 

"+values.getAsString(Video.Media.DATA));
Uri VideoURI =

getContext().getContentResolver().insert(Video.Media.EXTERNAL_CONTENT_URI,
values);
Log.i(t, "Inserting VIDEO returned uri = " +
VideoURI.toString());

         // check: (for debug only)
         Cursor c=getContext().getContentResolver().query(VideoURI, 

null, null, null, null);
if (c.moveToFirst()) {
Log.i(t,"The data path of the inserted video is
"+c.getString(c.getColumnIndex(Video.Media.DATA)));
}
// check: (for debug only)

c=getContext().getContentResolver().query(Video.Media.EXTERNAL_CONTENT_URI,
null,Video.Media.DATA +" = '"+newVideo.getAbsolutePath()+"'", null, null);
if (c.moveToFirst()) {
Log.d(t,"Double checked: video exists in media content
provider");
}
else {
Log.d(t,"Can't find the newly inserted video by file path");
}

     }

The output in the log file:

02-18 18:14:54.888 D/MediaWidget(12752): Inserting VIDEO: data:
/mnt/sdcard/odk/instances/Matteo2 New
York_2012-02-18_18-14-37/1329585294726.3gp
02-18 18:14:54.908 I/MediaWidget(12752): Inserting VIDEO returned uri =
content://media/external/video/media/5
02-18 18:14:54.918 I/MediaWidget(12752): The data path of the inserted
video is /mnt/sdcard/odk/instances/Matteo2 New
York_2012-02-18_18-14-37/1329585294726.3gp
02-18 18:14:54.928 D/MediaWidget(12752): Can't find the newly inserted
video by file path

This doesn't make any sense to me..........

··· On 02/18/2012 02:57 PM, Matteo Sisti Sette wrote: > Hi, > > When you record a video from a VideoWidget in ODK Collect, the video is > copied into the instance folder AND inserted into the media content > provider. However, the record inserted into the media content provider > is broken, i.e. it somehow doesn't point to the correct file location. > > When you go to Android's Gallery, you will find the copy of the video > (plus the original one) with its thumbnail, but when you try to play it > you get an error. If you look at the video details, it says "0.0 bytes" > and "unknown location". > > I've already reported the bug > (http://code.google.com/p/opendatakit/issues/detail?id=523&thanks=523&ts=1329572386) > but I urgently need to fix it myself. > > It seems like something goes wrong in this part of the VideoWidget code: > > File source = new File(binaryPath); > File newVideo = new File(destVideoPath); > FileUtils.copyFile(source, newVideo); > if (newVideo.exists()) { > // Add the copy to the content provier > ContentValues values = new ContentValues(6); > values.put(Video.Media.TITLE, newVideo.getName()); > values.put(Video.Media.DISPLAY_NAME, newVideo.getName()); > values.put(Video.Media.DATE_ADDED, System.currentTimeMillis()); > values.put(Video.Media.DATA, newVideo.getAbsolutePath()); > > Uri VideoURI = > > getContext().getContentResolver().insert(Video.Media.EXTERNAL_CONTENT_URI, > values); > Log.i(t, "Inserting VIDEO returned uri = " + VideoURI.toString()); > } > > However, I can't understant where the problem is. > It's as if the DATA field was not inserted or its value was wrong, but > the code looks OK....... > > In the log file, I can actually see the "Inserting VIDEO returned uri > ..." debug text. > > > Any idea what the problem could be?? Does anybody know I can fix this? > Thanks in advance > m.

Nobody has any idea why videos and audios are not properly inserted
into the media database?

··· 2012/2/18 Matteo Sisti Sette : > This is driving me crazy. > > I've added some debug code to retrieve the DATA field of the newly inserted > video just after inserting it into the media database, and it actually is > identical to the new filename (as expected). However, immediately after, I > query the data base looking for a record with that value of the DATA field > and it doesn't find it: > > This is my modified setBinaryData() in VideoWidget: > > > File source = new File(binaryPath); > File newVideo = new File(destVideoPath); > FileUtils.copyFile(source, newVideo); > > if (newVideo.exists()) { > // Add the copy to the content provier > ContentValues values = new ContentValues(6); > values.put(Video.Media.TITLE, newVideo.getName()); > values.put(Video.Media.DISPLAY_NAME, newVideo.getName()); > values.put(Video.Media.DATE_ADDED, System.currentTimeMillis()); > values.put(Video.Media.DATA, newVideo.getAbsolutePath()); > Log.d(t,"Inserting VIDEO: data: > "+values.getAsString(Video.Media.DATA)); > > Uri VideoURI = > > getContext().getContentResolver().insert(Video.Media.EXTERNAL_CONTENT_URI, > values); > Log.i(t, "Inserting VIDEO returned uri = " + > VideoURI.toString()); > > // check: (for debug only) > Cursor c=getContext().getContentResolver().query(VideoURI, null, > null, null, null); > if (c.moveToFirst()) { > Log.i(t,"The data path of the inserted video is > "+c.getString(c.getColumnIndex(Video.Media.DATA))); > } > // check: (for debug only) > > c=getContext().getContentResolver().query(Video.Media.EXTERNAL_CONTENT_URI, > null,Video.Media.DATA +" = '"+newVideo.getAbsolutePath()+"'", null, null); > if (c.moveToFirst()) { > Log.d(t,"Double checked: video exists in media > content provider"); > } > else { > Log.d(t,"Can't find the newly inserted video by file > path"); > } > > > } > > The output in the log file: > > 02-18 18:14:54.888 D/MediaWidget(12752): Inserting VIDEO: data: > /mnt/sdcard/odk/instances/Matteo2 New > York_2012-02-18_18-14-37/1329585294726.3gp > 02-18 18:14:54.908 I/MediaWidget(12752): Inserting VIDEO returned uri = > content://media/external/video/media/5 > 02-18 18:14:54.918 I/MediaWidget(12752): The data path of the inserted video > is /mnt/sdcard/odk/instances/Matteo2 New > York_2012-02-18_18-14-37/1329585294726.3gp > 02-18 18:14:54.928 D/MediaWidget(12752): Can't find the newly inserted video > by file path > > > This doesn't make any sense to me.......... > > > > On 02/18/2012 02:57 PM, Matteo Sisti Sette wrote: >> >> Hi, >> >> When you record a video from a VideoWidget in ODK Collect, the video is >> copied into the instance folder AND inserted into the media content >> provider. However, the record inserted into the media content provider >> is broken, i.e. it somehow doesn't point to the correct file location. >> >> When you go to Android's Gallery, you will find the copy of the video >> (plus the original one) with its thumbnail, but when you try to play it >> you get an error. If you look at the video details, it says "0.0 bytes" >> and "unknown location". >> >> I've already reported the bug >> >> (http://code.google.com/p/opendatakit/issues/detail?id=523&thanks=523&ts=1329572386) >> but I urgently need to fix it myself. >> >> It seems like something goes wrong in this part of the VideoWidget code: >> >> File source = new File(binaryPath); >> File newVideo = new File(destVideoPath); >> FileUtils.copyFile(source, newVideo); >> if (newVideo.exists()) { >> // Add the copy to the content provier >> ContentValues values = new ContentValues(6); >> values.put(Video.Media.TITLE, newVideo.getName()); >> values.put(Video.Media.DISPLAY_NAME, newVideo.getName()); >> values.put(Video.Media.DATE_ADDED, System.currentTimeMillis()); >> values.put(Video.Media.DATA, newVideo.getAbsolutePath()); >> >> Uri VideoURI = >> >> getContext().getContentResolver().insert(Video.Media.EXTERNAL_CONTENT_URI, >> values); >> Log.i(t, "Inserting VIDEO returned uri = " + VideoURI.toString()); >> } >> >> However, I can't understant where the problem is. >> It's as if the DATA field was not inserted or its value was wrong, but >> the code looks OK....... >> >> In the log file, I can actually see the "Inserting VIDEO returned uri >> ..." debug text. >> >> >> Any idea what the problem could be?? Does anybody know I can fix this? >> Thanks in advance >> m. > >

--
Matteo Sisti Sette
matteosistisette@gmail.com
http://www.matteosistisette.com

I've got this thread tagged, but haven't had a chance to look into the
issue.

I suspect that a Cursor is not being closed, which is causing inconsistent
results.

Mitch

··· On Fri, Feb 24, 2012 at 4:47 PM, matteo sisti sette < matteosistisette@gmail.com> wrote:

Nobody has any idea why videos and audios are not properly inserted
into the media database?

2012/2/18 Matteo Sisti Sette matteosistisette@gmail.com:

This is driving me crazy.

I've added some debug code to retrieve the DATA field of the newly
inserted
video just after inserting it into the media database, and it actually is
identical to the new filename (as expected). However, immediately after,
I
query the data base looking for a record with that value of the DATA
field
and it doesn't find it:

This is my modified setBinaryData() in VideoWidget:

   File source = new File(binaryPath);
   File newVideo = new File(destVideoPath);
   FileUtils.copyFile(source, newVideo);

   if (newVideo.exists()) {
       // Add the copy to the content provier
       ContentValues values = new ContentValues(6);
       values.put(Video.Media.TITLE, newVideo.getName());
       values.put(Video.Media.DISPLAY_NAME, newVideo.getName());
       values.put(Video.Media.DATE_ADDED,

System.currentTimeMillis());

       values.put(Video.Media.DATA, newVideo.getAbsolutePath());
       Log.d(t,"Inserting VIDEO: data:

"+values.getAsString(Video.Media.DATA));

       Uri VideoURI =

getContext().getContentResolver().insert(Video.Media.EXTERNAL_CONTENT_URI,

values);
Log.i(t, "Inserting VIDEO returned uri = " +
VideoURI.toString());

       // check: (for debug only)
       Cursor c=getContext().getContentResolver().query(VideoURI,

null,

null, null, null);
if (c.moveToFirst()) {
Log.i(t,"The data path of the inserted video is
"+c.getString(c.getColumnIndex(Video.Media.DATA)));
}
// check: (for debug only)

c=getContext().getContentResolver().query(Video.Media.EXTERNAL_CONTENT_URI,

null,Video.Media.DATA +" = '"+newVideo.getAbsolutePath()+"'", null,
null);
if (c.moveToFirst()) {
Log.d(t,"Double checked: video exists in media
content provider");
}
else {
Log.d(t,"Can't find the newly inserted video by
file
path");
}

   }

The output in the log file:

02-18 18:14:54.888 D/MediaWidget(12752): Inserting VIDEO: data:
/mnt/sdcard/odk/instances/Matteo2 New
York_2012-02-18_18-14-37/1329585294726.3gp
02-18 18:14:54.908 I/MediaWidget(12752): Inserting VIDEO returned uri =
content://media/external/video/media/5
02-18 18:14:54.918 I/MediaWidget(12752): The data path of the inserted
video
is /mnt/sdcard/odk/instances/Matteo2 New
York_2012-02-18_18-14-37/1329585294726.3gp
02-18 18:14:54.928 D/MediaWidget(12752): Can't find the newly inserted
video
by file path

This doesn't make any sense to me..........

On 02/18/2012 02:57 PM, Matteo Sisti Sette wrote:

Hi,

When you record a video from a VideoWidget in ODK Collect, the video is
copied into the instance folder AND inserted into the media content
provider. However, the record inserted into the media content provider
is broken, i.e. it somehow doesn't point to the correct file location.

When you go to Android's Gallery, you will find the copy of the video
(plus the original one) with its thumbnail, but when you try to play it
you get an error. If you look at the video details, it says "0.0 bytes"
and "unknown location".

I've already reported the bug

(
http://code.google.com/p/opendatakit/issues/detail?id=523&thanks=523&ts=1329572386
)
but I urgently need to fix it myself.

It seems like something goes wrong in this part of the VideoWidget code:

File source = new File(binaryPath);
File newVideo = new File(destVideoPath);
FileUtils.copyFile(source, newVideo);
if (newVideo.exists()) {
// Add the copy to the content provier
ContentValues values = new ContentValues(6);
values.put(Video.Media.TITLE, newVideo.getName());
values.put(Video.Media.DISPLAY_NAME, newVideo.getName());
values.put(Video.Media.DATE_ADDED, System.currentTimeMillis());
values.put(Video.Media.DATA, newVideo.getAbsolutePath());

Uri VideoURI =

getContext().getContentResolver().insert(Video.Media.EXTERNAL_CONTENT_URI,

values);
Log.i(t, "Inserting VIDEO returned uri = " + VideoURI.toString());
}

However, I can't understant where the problem is.
It's as if the DATA field was not inserted or its value was wrong, but
the code looks OK.......

In the log file, I can actually see the "Inserting VIDEO returned uri
..." debug text.

Any idea what the problem could be?? Does anybody know I can fix this?
Thanks in advance
m.

--
Matteo Sisti Sette
matteosistisette@gmail.com
http://www.matteosistisette.com

--
Mitch Sundt
Software Engineer
University of Washington
mitchellsundt@gmail.com