Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing file in Android Database (Custom Content Provider) using _data field - v1.6

I have been battling with an issue in my Android app. I have been reading Android docs, my book on Android (Andriod Pro 2) and have looked at almost each and every example I could get my hands on but could not figure out how to implement storing of files (images, binary data) against records in Sqlite table exposed by the content provider.

Here is what I have done (almost similar to Notepad sample app):

  • Implemented ContentProvider in my provider. It all works for CRUD operations. Internally it uses an implementation SQLiteOpenHelper (pretty standard stuff)
  • I have added a _data column of type text
  • I insert a record and get the Uri back
  • Call openOutputStream on the content provider and start writing the data

This is exactly according to the Android Pro 2 book.Here is the snippet of that part of code:

Uri uri = getContentResolver().insert(MyAuthority.CONTENT_URI, contentValues);
OutputStream s = getContentResolver().openOutputStream(uri);

But I get the error below:

11-29 01:07:30.717: WARN/System.err(1490): java.io.FileNotFoundException: No files supported by provider at content://myproviderauthority/podcasts/1

What do I need to do? I read Android documentation but they are very vague:

the field that exposes the data to clients should actually contain a content: URI string.

1) So does the field type need to be TEXT or is there a special content: URI string data type?

2) At the time of inserting I do not have the URI. Do I need to get the URI after insert and then update that field in the record?

The record should also have another field, named "_data" that lists the exact file path on the device for that file. This field is not intended to be read by the client, but by the ContentResolver.

3) So what should be the type of this field be? TEXT - I assume?

The client will call ContentResolver.openInputStream() on the user-facing field holding the URI for the item.

4) This is utter rubbish! How can I call ContentResolver.openInputStream() on the field?? You can only call this on the record and passing the Uri.

Thank you for your time and I hope you can help me. I have also reviewed these SO questions (this and this) and they did not help.

By the way I am building against Level 4 of the API (version 1.6).

like image 308
Aliostad Avatar asked Nov 29 '10 19:11

Aliostad


1 Answers

To see where "No files supported by provider at..." comes from, have a look here. The situation there seems very similar with the one you've described.

In short, your content provider (or some of its superclasses) needs to override and implement openFile method. That error is raised by the default "not implemented--back off!" code in ContentProvider.java.

I find it helpful to peek at underlying source code of the framework when I get stuck, since documentation is sometimes a bit lacking.

like image 164
Pēteris Caune Avatar answered Oct 30 '22 12:10

Pēteris Caune