1

I am trying to implement a way to open a PDF Reader for some files located in /Android/data/package.name/files/. I have failed at using a proper FileProvider, but I found that just added content:// did the job. But for some reason, it can't open some of them, and I can't understand why.

File file=new File("Direct path to file as string");

if(file.exists()){  ///Yes, File Exists
            try {

                Intent intent = new Intent(Intent.ACTION_VIEW);
                Log.v("Authority", context.getApplicationContext().getPackageName());
                Log.v("URI", Util.UriForFile(context, file).toString());
                intent.setDataAndType(Util.UriForFile(context, file), "application/pdf");
                intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
                context.startActivity(intent);


            } catch (ActivityNotFoundException e) {
                parent.alert(context.getString(R.string.error_nopdfreader));
            }
}

and the actual method Util.UriForFile:

public static Uri UriForFile(Context context, File file){
    return Uri.parse("content://"+file.getAbsolutePath());
//        if(context==null)
//            Log.v("Util", "Context is null");
//
//        if(file==null)
//            Log.v("Util", "File is null");
//
//        if(context.getApplicationInfo()==null)
//            Log.v("Util", "getApplicationInfo is null");
//
 //        return FileProvider.getUriForFile(context,  context.getApplicationInfo().packageName+".provider", file);
}
Myoch
  • 793
  • 1
  • 6
  • 24
  • "I found that just added content:// did the job" -- that will work with approximately zero apps. If it works with Adobe Reader, that is a bug in Adobe Reader. That is an invalid `Uri` (e.g., it has no authority string). "I have failed at using a proper FileProvider" -- consider asking a separate Stack Overflow question, where you provide a [mcve] and explain, in detail, what "failed" means. – CommonsWare Nov 28 '16 at 22:58
  • Well, half of my problem with FileProvider is described here: http://stackoverflow.com/questions/40318116/fileprovider-and-secondary-external-storage. Other than that, I also want to let the user specify another directory outside of the Android/data folder, and even on the secondary storage, but using the content:/// was the only thing that worked – Myoch Nov 28 '16 at 23:18
  • And even with the FileProvider on internal storage, Adobe won't open some specific files. It seems like the unopenable files are those with "%20" in the actual file name (there are no space, but real "%20" characters that get encoded to "%2520" by the FileProvider). – Myoch Nov 28 '16 at 23:24

1 Answers1

0

Ok, I "magically" solved the problem by going back to the use of FileProvider (uncommenting the lines), and doing 2 modifications:

  1. adding FLAG_GRANT_READ_URI_PERMISSION in the intent's flags
  2. adding the "non-documented" file path (for accessing files outside of Android/data):

      <root-path path="/storage/" name="root_test" />
    
Myoch
  • 793
  • 1
  • 6
  • 24
  • 1
    I had a similar issue. All the PDF apps were able to open the file through the Intent except Adobe Reader. My issue was the name of the file though. It didn't have .pdf at the end. Not sure why this wasn't an issue for other apps. – AliAvci Oct 08 '19 at 20:32