Я пытаюсь записать внешнее хранилище на версии Android M, это выдает ошибку, но как тот же код, который я пытаюсь запустить на версии Android P, это работает отлично ..
Эта ошибка, которую я получаю при запуске на M.
W/System.err: java.io.FileNotFoundException: /storage/emulated/0/PDF/Name.pdf: open failed: ENOENT (No such file or directory)
W/System.err: at libcore.io.IoBridge.open(IoBridge.java:452)
at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
at java.io.FileOutputStream.<init>(FileOutputStream.java:127)
at java.io.FileOutputStream.<init>(FileOutputStream.java:116)
at com.itextpdf.io.util.FileUtil.getBufferedOutputStream(FileUtil.java:152)
at com.itextpdf.kernel.pdf.PdfWriter.<init>(PdfWriter.java:143)
at com.example.admin6.finaldemo.Fragment.Report.PdfCreation.SalesBillWiseReport.createPDF(SalesBillWiseReport.java:57)
at com.example.admin6.finaldemo.Fragment.Report.SalesReport$1.onClick(SalesReport.java:88)
at android.view.View.performClick(View.java:5716)
at android.widget.TextView.performClick(TextView.java:10926)
at android.view.View$PerformClick.run(View.java:22596)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
at libcore.io.IoBridge.open(IoBridge.java:438)
... 17 more
Мой манифест
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
Мой build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.admin6.finaldemo"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'me.dm7.barcodescanner:zxing:1.9.13'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.itextpdf:itext7-core:7.1.5'
implementation 'com.itextpdf:html2pdf:2.1.4'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
и разрешение «Мой код для времени выполнения»
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
try {
boolean res=reportPdf.createPDF();
if (res){
Toast.makeText(getActivity(), "Created", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
e.printStackTrace();
}
} else
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 101);
else
try {
boolean res=reportPdf.createPDF();
if (res){
Toast.makeText(getActivity(), "Created", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
e.printStackTrace();
}
Мой метод createPdf
public boolean createPDF() throws IOException {
File newFile = new File(Environment.getExternalStorageDirectory().toString() + File.separator + "PDF ");
newFile.mkdir();
File file = new File(newFile,"Test.pdf");
StringBuilder sb=new StringBuilder();
sb.append("<table border=1 cellspacing=0 cellpadding=5 width=100%>");
sb.append("<tr>");
sb.append("<th>Month </th>");
sb.append("<th>Savings</th>");
sb.append(" </tr>");
sb.append("<tr>");
sb.append("<td align=center>January</td>");
sb.append("<td align=center>$100</td>");
sb.append("</tr>");
sb.append("<tr>");
sb.append("<td align=center>February</td>");
sb.append("<td align=center>$80</td>");
sb.append("</tr>");
sb.append("</table>");
sb.append("<br/>");
sb.append("<table border=1 cellspacing=0 cellpadding=5 width=100%>");
sb.append("<tr>");
sb.append("<th>Month </th>");
sb.append("<th>Savings</th>");
sb.append(" </tr>");
sb.append("<tr>");
sb.append("<td align=center>January</td>");
sb.append("<td align=center>$100</td>");
sb.append("</tr>");
sb.append("<tr>");
sb.append("<td align=center>February</td>");
sb.append("<td align=center>$80</td>");
sb.append("</tr>");
sb.append("</table>");
PdfWriter writer = new PdfWriter(file.toString(),new WriterProperties().setFullCompressionMode(true));
HtmlConverter.convertToPdf(sb.toString(),writer);
return true;
}
Каким было бы решение для этой проблемы и почему это происходит в Android версии P ниже и в версии Android работает нормально?