我正在尝试在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 Fine的Android版本上发生这种情况?
答案 0 :(得分:0)
您可以这样尝试
File downloadFolder = context.getExternalFilesDir("Foldername");
File[] files = downloadFolder.listFiles();
在此处使用文件夹名称获取存储空间
答案 1 :(得分:0)
尝试在文件路径中使用 File.separator
String FILE = Environment.getExternalStorageDirectory().toString() + File.separator + "PDF " + File.separator +"Name.pdf";
如果路径不存在,请尝试创建一个新目录,然后再将其传递给reportPdf.createPDF
方法
File newFile = new File(Environment.getExternalStorageDirectory().toString() + File.separator + "PDF ");
newFile.mkdir();
此外,在要求运行时权限的权限列表中,包括 Manifest.permission.READ_EXTERNAL_STORAGE 。
编辑2