gpt4 book ai didi

java - 文件选取器无法筛选和选取 intent.setType ("application/excel")

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

我正在开发一个 android 应用程序,用于使用 apache poi 读取/写入 excel 文件。为了从文件系统中选取一个文件,我使用了以下代码

private void showFileChooser() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("application/excel");
intent.addCategory(Intent.CATEGORY_OPENABLE);

try {
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"),
FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(this, "Please install a File Manager.",
Toast.LENGTH_SHORT).show();
}
}

intent.setType("application/excel"); 行用于过滤 excel 文件。但 android 文件管理器不选择任何 excel 文件。它仍处于禁用阶段。我通过将 intent.setType("application/excel"); 替换为 intent.setType("file/.mp3"); 检查了这段代码,它工作正常并且可以选择mp3 文件。我也试过 intent.setType("file/.xls"); 不走运。

最佳答案

在此链接中,您可以引用 Microsoft Office MIME 列表

https://stackoverflow.com/a/4212908/4300670

Extension MIME Type
.doc application/msword
.dot application/msword

.docx application/vnd.openxmlformats-officedocument.wordprocessingml.document
.dotx application/vnd.openxmlformats-officedocument.wordprocessingml.template
.docm application/vnd.ms-word.document.macroEnabled.12
.dotm application/vnd.ms-word.template.macroEnabled.12

.xls application/vnd.ms-excel
.xlt application/vnd.ms-excel
.xla application/vnd.ms-excel

.xlsx application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
.xltx application/vnd.openxmlformats-officedocument.spreadsheetml.template
.xlsm application/vnd.ms-excel.sheet.macroEnabled.12
.xltm application/vnd.ms-excel.template.macroEnabled.12
.xlam application/vnd.ms-excel.addin.macroEnabled.12
.xlsb application/vnd.ms-excel.sheet.binary.macroEnabled.12

.ppt application/vnd.ms-powerpoint
.pot application/vnd.ms-powerpoint
.pps application/vnd.ms-powerpoint
.ppa application/vnd.ms-powerpoint

.pptx application/vnd.openxmlformats-officedocument.presentationml.presentation
.potx application/vnd.openxmlformats-officedocument.presentationml.template
.ppsx application/vnd.openxmlformats-officedocument.presentationml.slideshow
.ppam application/vnd.ms-powerpoint.addin.macroEnabled.12
.pptm application/vnd.ms-powerpoint.presentation.macroEnabled.12
.potm application/vnd.ms-powerpoint.template.macroEnabled.12
.ppsm application/vnd.ms-powerpoint.slideshow.macroEnabled.12

.mdb application/vnd.ms-access

您可以像下面这样搜索 Excel 文件

private void searchExcelFile() {
String[] mimeTypes = {"application/vnd.ms-excel" , "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"};
Intent searchExcel = new Intent();
searchExcel.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true);
searchExcel.setAction(Intent.ACTION_GET_CONTENT);
//searchExcel.addCategory(Intent.CATEGORY_OPENABLE);


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
searchExcel.setType(mimeTypes.length == 1 ? mimeTypes[0] : "*/*");
if (mimeTypes.length > 0) {
searchExcel.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
}
} else {
String mimeTypesStr = "";
for (String mimeType : mimeTypes) {
mimeTypesStr += mimeType + "|";
}
searchExcel.setType(mimeTypesStr.substring(0,mimeTypesStr.length() - 1));
}

startActivityForResult(Intent.createChooser(searchExcel,"Selecione Arquivo Excel"), EXCEL_IMPORTED);
}

可以得到如下路径:

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);


if (resultCode == RESULT_OK) {

if (requestCode == EXCEL_IMPORTED) {


if (data.getClipData() == null) {

readExcelData(data.getData().getPath());
} else {
for (int i = 0; i < data.getClipData().getItemCount(); i++) {
Log.i("debinf cliinfo", "data.getClipData().getItemAt(i).getUri().toString()" + data.getClipData().getItemAt(i).getUri().toString());

}
}


}
} else {
Log.i("debinf cliinfo", "resultCol NOT OK");
}

}

有两种读取 Excel 文件的方法(可能更多),因为它的扩展名(.xls 和 .xlsx)请参见下面的链接:

https://stackoverflow.com/a/33047554/4300670

您可以在读取可能性之间切换并写入 SQLiteDatabe,如下所示:

private void readExcelData(String ExcelFilePath) {

// HSSFWorkbook is for .xls
// XSSFWorkbook is for .xlsx
// Check the extension of the Excel file

String[] path = ExcelFilePath.split(":");

Workbook workbook = null;

if (ExcelFilePath.endsWith(".xls")) {

try {
InputStream inputStream = new FileInputStream(new File(path[1]));
workbook = new HSSFWorkbook(inputStream);
} catch (FileNotFoundException e) {
Log.i("debinf cliinf", "readExcelData: FileNotFoundException " + e.getMessage());
} catch (IOException e) {
Log.i("debinf cliinf", "readExcelData: Error reading InputStream " + e.getMessage());
}

} else if (ExcelFilePath.endsWith(".xlsx")) {

try {
InputStream inputStream = new FileInputStream(new File(path[1]));
workbook = new XSSFWorkbook(inputStream);
} catch (FileNotFoundException e) {
Log.i("debinf cliinf", "readExcelData: FileNotFoundException " + e.getMessage());
} catch (IOException e) {
Log.i("debinf cliinf", "readExcelData: Error reading InputStream " + e.getMessage());
}
}

Log.i("debinf cliinf", "uri is " + ExcelFilePath.endsWith(".xls"));


Sheet sheet = workbook.getSheetAt(0);
Log.i("debinf cliinfo", "sheet is " + sheet);
if (sheet == null) {
return;
}

File dbfile = new File(Environment.getExternalStorageDirectory()+"/"+groupKeyIntent+"/"+"client.db");

if (!dbfile.isFile()) {

String Folder = Environment.getExternalStorageDirectory()+"/"+groupKeyIntent+"/";
new File(Folder).mkdirs();
Log.i("debinf cliinfo", "rootFolder created: " + Folder);
ClientDatabaseHelper createDatabaseHelper = new ClientDatabaseHelper(this,"client.db", Folder);
clientListTable = createDatabaseHelper.getWritableDatabase();

clientRepository = new ClientRepository(clientListTable);
//clientList = clientRepository.SearchAllClients();
//mClientListAdapter.notifyDataSetChanged();

}

clientRepository.insertExcelToSqlite(sheet, groupKeyIntent);
clientList = clientRepository.SearchAllClients();
mClientListAdapter = new ClientListAdapter(clientList, clientRepository, groupNameIntent, groupKeyIntent, groupCreatorIntent);
mClientList.setAdapter(mClientListAdapter);
//mClientListAdapter.notifyDataSetChanged();

}

希望对你有帮助!

关于java - 文件选取器无法筛选和选取 intent.setType ("application/excel"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47858688/

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