如何解决使用 URI 从 Android 中的外部存储打开文件
我的代码进入内部存储并选择一个 excel 文件并将 excel 数据上传到应用程序的 sqlite 数据库但我在打开文件时出错,我使用 FileInputStream() 从路径字符串打开文件命名 FilePath 但我收到错误消息,我的文件路径中没有这样的文件或目录,任何人都可以帮助我
@Override
public void onClick(View v) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(add_number.this);
alertDialog.setTitle("Confirm Import...");
alertDialog.setMessage("Are you sure you want to Import Data?");
alertDialog.setPositiveButton("Yes",new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which) {
Intent fileIntent = new Intent(Intent.ACTION_GET_CONTENT);
fileIntent.setType("*/*");
try {
startActivityForResult(fileIntent,requestcode);
}
catch (ActivityNotFoundException e){
lbl.setText("No activity can handle picking a file. Showing alternatives.");
}
}
});
alertDialog.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
Toast.makeText(getApplicationContext(),"Import Canceled",Toast.LENGTH_SHORT ).show();
}
});
alertDialog.show();
}
});
}
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
super.onActivityResult(requestCode,resultCode,data);
if (data == null)
return;
switch (requestCode) {
case requestcode:
String FilePath = data.getData().getPath();
Log.e("File path",FilePath);
if (FilePath.contains("/root_path"))
FilePath = FilePath.replace("/root_path","");
Log.e("New File path",FilePath);
try {
if (resultCode == RESULT_OK) {
AssetManager as = getAssets();
InputStream inStream =new FileInputStream(new File(FilePath));
Toast.makeText(this,"hellow",Toast.LENGTH_SHORT).show();
Workbook wb = null;
try {
Log.e("Extension",FilePath.substring(FilePath.lastIndexOf(".")));
if (FilePath.substring(FilePath.lastIndexOf(".")).equals(".xls")) {
Log.e("File Type","Selected file is XLS");
wb = new hssfWorkbook(inStream);
}
else if (FilePath.substring(FilePath.lastIndexOf(".")).equals(".xlsx")) {
Log.e("File Type","Selected file is XLSX");
wb = new XSSFWorkbook(inStream);
}
else {
wb = null;
lbl.setText("Please select a valid Excel file");
return;
}
inStream.close();
} catch (IOException e) {
lbl.setText("First " + e.getMessage().toString());
e.printstacktrace();
}
DatabasesqliteHelper dbAdapter = new DatabasesqliteHelper(this);
Sheet sheet1 = wb.getSheetAt(0);
insertExcelTosqlite(dbAdapter,sheet1);
dbAdapter.close();
}
} catch (Exception ex) {
lbl.setText(ex.getMessage().toString() + "Second");
Log.e("POI Errorr",ex.getMessage().toString());
}
}```
##my result output and error##
here my path /document/primary:Download/flagCache/com.andro
id.packageinstaller.png is provided but file input stream find no file exist I try to check many solutions and different article no any of those materials helped me
```2021-02-15 02:59:22.487 21798-21798/com.example.paymentmessage E/File path: /document/primary:Download/flagCache/com.android.packageinstaller.png
2021-02-15 02:59:22.487 21798-21798/com.example.paymentmessage E/New File path: /document/primary:Download/flagCache/com.android.packageinstaller.png
2021-02-15 02:59:22.488 21798-21798/com.example.paymentmessage E/POI Errorr: /document/primary:Download/flagCache/com.android.packageinstaller.png (No such file or directory)```
## i had include permission in manifests file##
```<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.paymentmessage">
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".broadcaster"></activity>
<activity```
hope I will find a solution
### thank you in advance###
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。