Accessing Files in Content Providers

Content Providers represent files as fully qualified URIs rather than as raw file blobs. To insert a file into a Content Provider, or access a saved file, use the Content Resolvers openOutputStream or openInputStream methods respectively. The process for storing a file is shown in Listing 7-17.

LISTING 7-17: Adding files to Content Providers Available for download on // Insert a new row into your provider, returning its unique URI. Uri uri = getContentResolver().insert(MyProvider.CONTENT_URI, newValues);

// Open an output stream using the new row's URI.

OutputStream outStream = getContentResolver().openOutputStream(uri); // Compress your bitmap and save it into your provider. sourceBitmap.compress(Bitmap.CompressFormat.JPEG, 50, outStream);

catch (FileNotFoundException e) { }


Having created an application that features a list of earthquakes, you have an excellent opportunity to share this information with other applications.

By exposing this data through a Content Provider you make it possible for yourself, and others, to create new applications based on earthquake data without having to duplicate network traffic and the associated XML parsing.

Was this article helpful?

0 0
Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide

Post a comment