Permission specified in manifest

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

In code

 file = new File("/storage/emulated/0/Download/ccAlTT_D__.pdf"); Uri uri = Uri.fromFile(file); Intent intentOpenFile = new Intent(Intent.ACTION_VIEW); intentOpenFile.setDataAndType(uri, "application/pdf"); intentOpenFile.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intentOpenFile); 

The file path is correct. This code works on versions below 6, and in the sixth moment it opens the pdf file and immediately minimizes it, while leaving it in the logs -

 12-23 14:06:53.248 18679-16520/? E/DisplayData: openFd: java.io.FileNotFoundException: Permission denied 12-23 14:06:53.248 18679-16520/? E/PdfLoader: Can't load file (doesn't open) Display Data [PDF : ccAlTT_D__.pdf] +UriOpenable 12-23 14:06:53.381 881-896/? E/KernelCpuSpeedReader: Failed to read cpu-freq: /sys/devices/system/cpu/cpu4/cpufreq/stats/time_in_state: open failed: ENOENT (No such file or directory) 

If you have any ideas about this, please speak up.

  • And if you add READ_EXTERNAL_STORAGE ? - YurySPb
  • @ YuriSPb not. does not work - Pavel K
  • Most likely it is on the way, it is not recommended to do in such a hardcore form. try to make Environment.getExternalStorageDirectory () + "/Download/ccAlTT_D__.pdf" - Android Android
  • В Android 6 permissions must be requested. In order not to request permissions, set Target SDK 22 , or lower - Vladyslav Matviienko
  • @AndroidAndroid, I wrote for example. With getExternalStorageDirectory() does not work, I checked it long ago - Pavel K

2 answers 2

The logs are written

java.io.FileNotFoundException: Permission denied

It means that either the fact that the program for reading pdf does not support android 6, or that you need to use another method for android 6.

    The fact is that in the sixth version of Android, when installing an application, the user does not consent to the use of certain access rights by the program (the developers considered that this often frightened the user from installing applications). Instead, you now need to manually access the user when starting the program (or at the beginning of a certain action). For example, like this: (or replace READ_EXTERNAL_STORAGE with WRITE_EXTERNAL_STORAGE for more versatility)

     // проверка есть ли права доступа к файловой системе if (getContext().checkSelfPermission( Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { // показать сообщение о необходимости получить права if (shouldShowRequestPermissionRationale( Manifest.permission.READ_EXTERNAL_STORAGE)) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setMessage(R.string.permission_explanation); // кнопка OK диалога с запросом доступа builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // запрос на доступ requestPermissions(new String[]{ Manifest.permission.READ_EXTERNAL_STORAGE}, 1); } } ); builder.create().show(); } else { // запрос на доступ requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1); } } else { // если у нас уже есть доступ, делаем то что нам нужно file = new File("/storage/emulated/0/Download/ccAlTT_D__.pdf"); Uri uri = Uri.fromFile(file); Intent intentOpenFile = new Intent(Intent.ACTION_VIEW); intentOpenFile.setDataAndType(uri, "application/pdf"); intentOpenFile.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intentOpenFile); }