gpt4 book ai didi

android - 写入存储打开时出错失败 : ENOENT (No such file or directory) in android

转载 作者:行者123 更新时间:2023-11-29 02:18:34 25 4
gpt4 key购买 nike

我正在尝试在 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

我的 list

<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 版本上它运行良好?

最佳答案

你可以这样试试

File downloadFolder = context.getExternalFilesDir("Foldername");
File[] files = downloadFolder.listFiles();

在此处使用文件夹名称获取存储

关于android - 写入存储打开时出错失败 : ENOENT (No such file or directory) in android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58056439/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com