Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't open pdf file for intent android

I thoroughly checked that pdf file is in /storage/emulated/0/Download/Abcd.pdf" but can't open it with intent.

I opened it in various viewers and some oh them result in a error: "can't open file". Microsoft word says: check file in the device, but Abcd.pdf file is opened well when I opened it in file directory in file system of android.

Did I set the route wrong?

enter image description here

AndroidManifest.xml

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

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

MainActivity.Java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent=new Intent();

    File mypath=new File( Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download/","Abcd.pdf");

    Log.e("download",mypath.getAbsolutePath());
      //this log says : /storage/emulated/0/Download/Abcd.pdf
    Uri pdfUri = FileProvider.getUriForFile(getApplicationContext(), getApplicationContext().getPackageName() + ".provider", mypath);



    Log.e("download",mypath.exists()+"");
    if (mypath.exists()) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Log.e("download","result : "+pdfUri.getPath());
            intent = new Intent(Intent.ACTION_VIEW);
            intent.setType("application/pdf");
            intent.putExtra(MediaStore.EXTRA_OUTPUT, pdfUri);
        }else{

        }
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);


        try {
            startActivity(intent);
        }
        catch (ActivityNotFoundException e) {
            Log.e("error","error"+e);
        }
    }
}

res/xml/provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
 <paths xmlns:android="http://schemas.android.com/apk/res/android">
 <external-path
name="storage/emulated/0"
path="."/>
</paths>
like image 943
Pedro J Avatar asked Mar 05 '23 10:03

Pedro J


2 Answers

I am using this function :

public void openPdf(LocalFile magazine) {

        if (BuildConfig.DEBUG)
            Log.d(TAG, "openPdf() called with: magazine = [" + magazine + "]");

        Intent intent = new Intent(Intent.ACTION_VIEW);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

            File file = new File(Uri.parse(magazine.getPath()).getPath());
            Uri uri = FileProvider.getUriForFile(getContext(),
                getContext().getApplicationContext().getPackageName() + ".provider", file);
            intent.setDataAndType(uri, "application/pdf");
            intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

        } else {
            intent.setDataAndType(Uri.parse(magazine.getPath()), "application/pdf");
        }

        try {
            startActivity(intent);
        } catch (Throwable t) {
            t.printStackTrace();
            //attemptInstallViewer();
        }

    }

This is works fine for me.

like image 71
Ehsan Avatar answered Mar 10 '23 11:03

Ehsan


I've one question by which this problem might be resolved, Have you wrote Storage Permission in Manifest?

Also is your App has Permission to Read External Storage? Check this from Mobile Settings and it would start working automatically.

Here is how you can initialize Permissions Statement in Manifest File.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

If it's given in Manifest and also your Mobile has guaranteed the Permissions and App is not working then share the full code of Activity, I might help you in that.

like image 27
Numan Gillani Avatar answered Mar 10 '23 11:03

Numan Gillani