2

I have a pdf file stored in my cache. I want to read this pdf through third party application(Adobe reader). But I am not allowed to do this as intents cannot access application cache directory unless the pdf is made public by storing in sdcard.

Is there any workaround that can allow the third party app to read pdf from cache?

Thanks

Gaurav
  • 1,700
  • 4
  • 22
  • 38

2 Answers2

0

It is not possible for 3rd party apps to access your app's cache directory.

This thread contains several links on libraries which allow you to display the PDF contents within your app: Render a PDF file using Java on Android

Community
  • 1
  • 1
Ilya Shinkarenko
  • 2,134
  • 18
  • 35
-1

yes, It is very much possible, using FileProvider, you can keep your files secure in your cache, and give access to your files temporarily to viewers. Here is a link that will help you implement.

The Android support library v4 contains a class called FileProvider, it’s a special subclass of ContentProvider. This class help in sharing of files associated with an app by creating a content:// uri instead of a file:/// uri .

A content URI allows you to grant read and write access using temporary access permissions. So you can provide temporary access to files in our cache directory without needing our app to have WRITE_EXTERNAL_STORAGE permission.

The first thing you should do is add a new content provider to your AndroidManifest file:

<manifest>
    ...
    <application>
        ...
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">

            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/fileprovider" />
        </provider>
        ...
    </application>
</manifest>

A FileProvider can only generate a content URI for files in directories that you specify beforehand. To specify a directory, specify its storage area and path in XML (save the file as res/xml/fileprovider.xml).

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <cache-path name="image" path="image/"/>
</paths>

Now you can share your apps cache files to other apps. To share file you need to add some more line to you intent.

File cacheFile = new File(context.getCacheDir(), "image/yoo.jpg");

Uri uri = FileProvider.getUriForFile(context, "my.package.provider", 
cacheFile);

Intent intent = ShareCompat.IntentBuilder.from(context)
    .setType("image/jpg")
    .setSubject(context.getString(R.string.share_subject))
    .setStream(uri)
    .setChooserTitle(R.string.share_title)
    .createChooserIntent()
    .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

context.startActivity(intent);

First of all you have to get reference to your cache file you want to share, in our case its cacheFile located inside cache directory + image folder.

Next you have to get this cache file’s uri which can be obtained using FileProvider.getUriForFile. Here you have to replace my.package with your app package name. Do not remove that .provider from the end.

In the end when you are creating intent then just add flags Intent.FLAG_GRANT_READ_URI_PERMISSION to your intent so that other app can read this uri.

Reference: https://medium.com/androidsrc/share-cache-files-with-other-android-apps-using-fileprovider-897fe5705e45

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/20705839) – Eugene Primako Aug 28 '18 at 14:23
  • @EugenePrimako Please review again, I have made the changes. – Harsh Saini Aug 28 '18 at 15:35