I'm trying to code a system for running user studies. I have a webpage and a webservice on top of a user DB.
My Android app is a client to run the studies on. I'm use the Account Manager to store user credentials authenticated via the webservice. The point is to then implement a sync adapter that will send a text file with study data to my server, via the webservice (using the credentials).
My problem is that I'm having difficulty with the Content Provider, I've searched a lot throught the web but I can't seem to find an example of how to code a Content Provider tied to a file on external storage. All the examples I find use Content Providers tied to database tables. Can someone please point me in the right direction or shed some coding light? (BTW, I have read the Android Developer texts on Content Providers, also, I'm using Android 2.2).
Any help is appreciated.
Cheers,
André Coelho
A content provider can be used to read file data by overriding the openFile method. You could have a top level provider that provides an listing of all the files (via query) then each file could have its own unique URI that could then have openFile called on it to retrieve the file data.
Related
Well, as far as I kwnow a content provider is a database, and is used to pass data through apps.
But this data can be accessed only in the phone that it was saved? Or if I saved some data using the app with phone1, can I retrieve it using the app with phone 2?
If it can't do the second option, what could I use to do it?
Thanks.
Content provider is not a database. You can think this as a layer between your app and underlying data. The data can be in sqlite database, file or something else. Content provider is very useful to access and store your data to database or file. You need this when you use syncadapter or widgets in your app. Now to answer your question, content provider of your app can be accessed by a different app in your same phone provided you use correct permission. As I mentioned it's not a database, so answer of second question is not. I would recommend you to read about content provider on android developer site to get the basic concepts.
A content provider component supplies data from one application to others on request. Such requests are handled by the methods of the ContentResolver class. A content provider can use different ways to store its data and the data can be stored in a database, in files, or even over a network.
I have been reading content providers for a while and i have seen that of two types, one is in built for eg Contacts(that i understood) and other is making our own content provider by content:// thing. Well most of the examples i have gone through are doing it in one app. I mean what is the point of using content providers then if i store data in one app and retrieving it in the same as the definition says it shares data between apps?
I am looking it as i made two projects and i used content provider in one and store some data in database. Then i make other project and get that stored data through the content :// uri . Is that what is main function of content provider? Is this thing possible? If so how?
I have been asking it clear my basics.
To understand content providers you need to understand the Android Architecture first. All android apps runs in its own VM (Virtual Machine), it means when you run app 'A' and store some files or create database in it, now when you run app 'B' and create database in it.
Those two apps 'A' and 'B' do not know each other or in easy words they do not share data between them. To make data accessible one app has to share its data so other can access it. Thats where ContentProvider comes in.
Through content providers any app can expose its data to other apps which are interested in taking it.
One example is your Contact list. You can access Contact List through content provider although it is not created by you and own by Android, but it intents to share data with you, and you can access it through content providers.
I believe you are looking for information on the Content Resolver. As others pointed out, the Provider is just for sharing your app's data. When you want to access it, even in another app, you use the Content Resolver to send commands to the other App's Content Provider; insert(), update(), delete(), and query().
What you spoke of about content://URI, that's the identifier for the name of the data you are looking for in a Content Provider. The link explains Providers, Resolvers, Contracts, URIs, and how to create them.
http://www.grokkingandroid.com/android-tutorial-writing-your-own-content-provider/
Content providers let you centralize content in one place and have many different applications access it as needed. A content provider behaves very much like a database where you can query it, edit its content, as well as add or delete content usingg insert(), update(), delete(), and query() methods. In most cases this data is stored in an SQlite database.
I am looking it as i made two projects and i used content provider in
one and store some data in database. Then i make other project and get
that stored data through the content :// uri . Is that what is main
function of content provider?
I think you are looking for this:
http://www.vogella.com/tutorials/AndroidSQLite/article.html
Android Content Provider system allows other apps to access the data of an app.
Is there any equivalent, or similar approach in WinRT?
I read App contract and extensions and Share contract in MSDN, but it has to execute provider app (an app holds data source) firstly, and then select target app afterward.
In Android Content Provider however, it can query data provider's repository while using target app (data receiver) with no selection of choosing steps. small but big difference in usage.
I'm not sure what kind of data you want your app to share, but it seems to me that what you are looking for might be the file picker contracts. They, I quote:
Make files accessible from a file picker if your app has a unique and/or valuable view of those files or if users cannot easily access the files another way.
I am thinking regarding the future options of my app and I am thinking of the idea of backing-up the data from the application's Database and also sharing that data with another phone, say via e-mail, messaging, Bluetooth, you name it, but basically saving it as a file and opening it from the other phone and having the same values on both phones.
What would be the best approach for such an Android application?
Would Content Providers accomplish exactly this or are they concerned with sharing data only between different Apps? Thanks!
I believe it is possible ,
If you read the documentation about the internal storage here, It mentions
You can save files directly on the device's internal storage. By default, files saved to the internal storage are private to your application and other applications cannot access them
So i believe you can copy the whole sqlite DB file to some temp location then share that file via BT or email or any other sharing option .
But DO NOTE, that the same application package can only access the file if you want perhaps another application to use the db then u need to set the SharedUserId , as mentioned here
Content Providers are generally only for sharing your app's data to other apps.
Content providers are the standard interface that connects data in one process with code running in another process.
So far, all content provider tutorials I found, teach how to implement a content provider locally, on the Android device. The URI for such content providers always starts with content:// as in:
content://com.google.provider.NotePad/notes/23
My question: is it possible to implement a content provider that is not on the Android device?
That is, a content provider that follows all the conventions and convenience of Android content providers but is rather located on a remote website, using MySQL for example, instead of SQLite3?
If so, how do I go about implementing such content provider? Where do I find information that teaches how to do that? Does such content provider's URI start with 'content://'?
I don't think it is possible to create a content provider that is not on the Android device. A Content Provider is part of the Android framework and its resolution (i.e. what does the name maps to) is part of the framework as well. Realize that a URI that Android uses for the name of a service is not the same as a URL over the public internet.
If you want to connect to some server side data over the public internet, it is best to wrap it in some web service that will return JSON or XML to avoid database connection issues.
This is indeed possible and I don't think you'll find a better example than RESTProvider .
This exposes a contentprovider interface to REST API services and can be used with any remote service that returns XML or JSON.