[英]File or directory is not created in Android 10 and above device
我正在开发一个多文件转换器应用程序,用户可以在其中将 select 一个 excel 文件转换为 pdf,下面的方法用于创建 pdf 文件,该文件在我的手机中运行良好,即 android 派(android 9),但是当我在 android 中运行应用程序 10 真实设备也在模拟器中没有创建目录。 请帮助我处理代码并使用 dexter 库请求运行时许可
我在清单中添加了这一行:-
android:requestLegacyExternalStorage="true"
权限:--
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
我的 CreatePDF 方法:-
void Convert_to_PDF(Uri path) throws IOException, DocumentException, URISyntaxException {
File f = new File(Environment.getExternalStorageDirectory() + "/MultiFileCon");
if (!f.exists()) if ( !f.mkdir()) return;
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH.mm.ss");
Date date = new Date();
// System.out.println(formatter.format(date));
Log.d("DATE", formatter.format(date) + ".pdf");
File pdffilex = new File(f.getAbsolutePath(), formatter.format(date) + ".pdf");
Log.d("ABC", "Convert_to_PDF: "+uriforExcel.getPath());
if (android.os.Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
Toast.makeText(this, "android version is "+android.os.Build.VERSION.SDK_INT, Toast.LENGTH_LONG).show();
Log.d("VERSION NAME", "Convert_to_PDF: "+android.os.Build.VERSION.SDK_INT);
File file = new File(path.getPath());//create path from uri
final String[] split = file.getPath().split(":");//split the path.
filePath = split[1];
input_document = new FileInputStream(filePath);
}
else {
input_document = new FileInputStream(new File(PathUtil.getPath(this,path)));
}
// Read workbook into HSSFWorkbook
HSSFWorkbook my_xls_workbook = new HSSFWorkbook(input_document);
// Read worksheet into HSSFSheet
HSSFSheet my_worksheet = my_xls_workbook.getSheetAt(0);
// To iterate over the rows
Iterator<Row> rowIterator = my_worksheet.iterator();
//We will create output PDF document objects at this point
Document iText_xls_2_pdf = new Document();
PdfWriter.getInstance(iText_xls_2_pdf, new FileOutputStream(pdffilex));
iText_xls_2_pdf.open();
//we have two columns in the Excel sheet, so we create a PDF table with two columns
//Note: There are ways to make this dynamic in nature, if you want to.
PdfPTable my_table = new PdfPTable(7);
//We will use the object below to dynamically add new data to the table
PdfPCell table_cell;
//Loop through rows.
while(rowIterator.hasNext()) {
Row row = rowIterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext()) {
Cell cell = cellIterator.next(); //Fetch CELL
switch(cell.getCellType()) { //Identify CELL type
//you need to add more code here based on
//your requirement / transformations
case Cell.CELL_TYPE_STRING:
//Push the data from Excel to PDF Cell
table_cell=new PdfPCell(new Phrase(cell.getStringCellValue()));
Log.d("DOL", cell.getStringCellValue());
//feel free to move the code below to suit to your needs
my_table.addCell(table_cell);
break;
}
//next line
}
// Log.d("DOL", "Convert_to_PDF: "+datatobe);
}
//Finally add the table to PDF document
iText_xls_2_pdf.add(my_table);
iText_xls_2_pdf.close();
//we created our pdf file..
input_document.close(); //close xls
}
SimpleDateFormat 格式化程序 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
':'
是文件系统路径中的禁止字符。
尝试:。
"dd-MM-yyyy HH.mm.ss"
出于安全原因, Environment.getExternalStorageDirectory()
方法不适用于 Android 11 或更高版本的设备。
您可以轻松地将文件存储在公共目录中。 以下是您如何访问 Android Studio 中的公共目录的示例。
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
所以你可以在你的项目中尝试这段代码。
File baseFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "MultiFileCon");
if (!baseFile.exists()) {
baseFile.mkdirs();
}
File pdfFile = new File(baseFile, "PDF_" + Calendar.getInstance().getTimeInMillis() + ".pdf");
Log.e(TAG, "onCreate: pdfFile path is: " + pdfFile.getAbsolutePath());
/storage/emulated/0/Download/MultiFileCon/PDF_1665148985587.pdf
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.