Skip to content Skip to sidebar Skip to footer

Can't Open External Storage File In Android N Using Fileprovider

I am having trouble opening file inside external storage with Android N. File path file:///storage/emulated/0/Download/SamplePDFFile_5mb.pdf Initial implementation Perfectly w

Solution 1:

publicvoidgetGROUPSTORAGEPremission(){
    inthasWriteContactsPermission= Splash_activity.this.checkSelfPermission(Manifest.permission_group.STORAGE);
    if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
        Splash_activity.this.requestPermissions(newString[]{  Manifest.permission_group.STORAGE},
                PERMISSION_GROUPSTORAGE);
    } else {
       Filefile=newFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "SamplePDFFile_5mb.pdf");
     //old way//Uri uri = Uri.fromFile(file);//new wayUriuri= FileProvider.getUriForFile(MainActivity.this, getPackageName() + ".provider", file);

       Intentintent=newIntent(Intent.ACTION_VIEW);
       intent.setDataAndType(uri, "application/pdf");
       intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
       startActivity(intent);
    }

}
@OverridepublicvoidonRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNullint[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == PERMISSION_GROUPSTORAGE) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // Permission GrantedFilefile=newFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "SamplePDFFile_5mb.pdf");
     //old way//Uri uri = Uri.fromFile(file);//new wayUriuri= FileProvider.getUriForFile(MainActivity.this, getPackageName() + ".provider", file);

       Intentintent=newIntent(Intent.ACTION_VIEW);
       intent.setDataAndType(uri, "application/pdf");
       intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
       startActivity(intent);
        } else {
            // Permission Denied//Toast.makeText(mContext, "PERMISSION_GROUPSTORAGE Denied", Toast.LENGTH_SHORT).show();
        }
    }
 }

After N need to check runtime permission .

Solution 2:

Maybe late, but I solved it by adding an temporary permission to the intent, by doing the following:

Uripath= FileProvider.getUriForFile(MainActivity.this, "com.example.root.somepackage.provider", pdfFile);
    IntentpdfIntent=newIntent(Intent.ACTION_VIEW);
    pdfIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // GRANT TEMPORARY READ PERMISSION
    pdfIntent.setDataAndType(path, "application/pdf");

    try{
        startActivity(pdfIntent);
    }

This was helpful: Granting Content Provider URI Permissions

Solution 3:

This simple solution helps me to solve this kind of problems. Hope it will help you and save your time.

publicvoidopen_file(String filename) {
    Filepath=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
    Filefile=newFile(path, filename);

    // Get URI and MIME type of fileUriuri= FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".fileprovider", file);
    Stringmime= context.getContentResolver().getType(uri);

    // Open file with user selected appIntentintent=newIntent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.setDataAndType(uri, mime);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    context.startActivity(intent);
}

Post a Comment for "Can't Open External Storage File In Android N Using Fileprovider"