Note that there are some explanatory texts on larger screens.

plurals
  1. POVIEW intent from a Content Provider URI?
    primarykey
    data
    text
    <p>I have some requirements to protect some sensitive data. The data is downloaded as a PDF from a URL and saved as an Application Private file using the following code:</p> <pre><code>public File downloadPDF(final Context fileContext, Uri reportUri, final String fileName) { try { HttpGet get = new HttpGet(reportUri.toString()); File file = httpClient.execute(get, new ResponseHandler&lt;File&gt;() { @Override public File handleResponse(HttpResponse response) throws ClientProtocolException, IOException { if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { response.getEntity().writeTo(fileContext.openFileOutput(fileName, Context.MODE_WORLD_READABLE)); return fileContext.getFileStreamPath(fileName); } return null; } }); return file; } catch (IOException e) { Log.e(TAG, "Unable to download report.", e); } return null; } </code></pre> <p>Now, what I'd like to do is change this to using Context.MODE_PRIVATE and create a ContentProvider so that my application has complete control over the sharing of this file to a PDF reader such as Adobe Reader. Is this possible? I currently use code like the following to pass the report URI to the currently configured PDF reader. </p> <pre><code> // Fire up a PDF viewer intent for the URL. Intent intent = new Intent(Intent.ACTION_VIEW); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(uri, "application/pdf"); startActivity(intent); </code></pre> <p>Would the same work through a ContentProvider type URI? A content://package/fileid type URI? I'll be trying a little spike tomorrow to see if I can, but if anyone knows that only file:// URIs are allowed, it would be really helpful.</p> <hr> <p><strong>UPDATE</strong></p> <p>I was able to solve my problem satisfactorily by implementing a ContentProvider subclass with the following method overridden:</p> <pre><code>@Override public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException { // The filename is the path in the URI without the initial slash. String fileName = uri.getPath().substring(1); File file = getContext().getFileStreamPath(fileName); return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY); } </code></pre> <p>Then, when I fire off the viewing intent, it is rewritten something like the following:</p> <pre><code>Intent intent = new Intent(Intent.ACTION_VIEW); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Uri uri = Uri.withAppendedPath(Uri.parse("content://providername/"),filePath); intent.setData(uri); startActivity(intent); </code></pre> <p>And in my case, I use Adobe Reader, which properly implements the loading from <code>content://</code> URIs.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload