微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java.io.FileNotFoundException: /sdcard/...: open failed: EISDIR (Is a directory)

如何解决java.io.FileNotFoundException: /sdcard/...: open failed: EISDIR (Is a directory)

你好,我正在尝试将文件上传到我的应用程序和一些设备上,以便从 sdcard 上传它给我一个错误:java.io.FileNotFoundException: (sdcard 中的路径): open Failed: EISDIR (Is a directory) .有人知道为什么吗?我的代码

浏览文件和打开文件管理器:

 private void dobrowseFile()  {
    Intent chooseFileIntent = new Intent(Intent.ACTION_GET_CONTENT);
    chooseFileIntent.setType("application/pdf");
    // Only return URIs that can be opened with ContentResolver
    chooseFileIntent.addCategory(Intent.CATEGORY_OPENABLE);

    chooseFileIntent = Intent.createChooser(chooseFileIntent,"Choose a file");
    startActivityForResult(chooseFileIntent,UNIQUE_REQUEST_CODE);
} 

@Override
public void onActivityResult(int requestCode,int resultCode,Intent data) {
    if (requestCode == UNIQUE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            if (data != null) {
                Uri fileUri = data.getData();
                Log.i(LOG_TAG,"Uri: " + fileUri);

                String filePath = null;
                try {
                    filePath = FileUtils.getPath(this,fileUri);
                } catch (Exception e) {
                    Log.e(LOG_TAG,"Error: " + e);
                    Toast.makeText(this,"Error: " + e,Toast.LENGTH_SHORT).show();
                }
                getBase64FromPath(filePath);
            }
        }
    }
    super.onActivityResult(requestCode,resultCode,data);
}

文件路径编码文件

@RequiresApi(api = Build.VERSION_CODES.O)
public void getBase64FromPath(String path) {
    String base64 = "";
    try {
        File file = new File(path);
        byte[] buffer = new byte[(int) file.length() + 100];
        file.getParentFile().mkdirs();
        FileInputStream fileInputStream = new FileInputStream(file); //THIS LINE GIVES ME ERROR
        @SuppressWarnings("resource")
        int length = fileInputStream.read(buffer);
        base64 = Base64.encodetoString(buffer,length,Base64.DEFAULT);
        uploadFile(base64);
    } catch (IOException e) {
        Toast.makeText(this,"error:" + e.getMessage(),Toast.LENGTH_SHORT).show();
    }

}

如果有人知道为什么请告诉。它仅在某些设备上出现此错误。在其他方面它工作得很好。谢谢。

解决方法

摆脱 const App = await ethers.getContractFactory("app"); let app = await App.attach(addressOfContract); const logs = await app.filters.trustAdded(owner.address); { address: 'addressOfContract',topics: [ '0x9229966fc31285cf68748b91cfbc30dc196a49c2eaaf884d89125eb243aa211c','0x000000000000000000000000996bf770d6027b7c1315637cda4dda684780bbde' ] } ,因为它永远不会可靠地工作。

您可以通过调用 FileUtils.getPath() 获得有关您的内容的 InputStream。然后,您可以使用 getContentResolver().openInputStream(fileUri) 而不是 InputStream 来阅读您的内容。

请注意,对于大型 PDF,您的应用会因 FileInputStream 崩溃。我强烈建议您找到某种方法来上传您的内容,而无需一次性将其全部读入内存。例如,您可能会考虑不将其转换为 base-64。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。