Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从原始文件夹复制的文件可以';我不能开门_Android_Excel - Fatal编程技术网

Android 从原始文件夹复制的文件可以';我不能开门

Android 从原始文件夹复制的文件可以';我不能开门,android,excel,Android,Excel,在上述代码的帮助下,我在SD卡上复制了一些.xls文件,但当我尝试使用以下代码打开文件时,会出现错误,如“无法打开此文档”或“不支持文件格式”,但我转到“文件资源管理器”并尝试打开文件,但不会出错 InputStream in = getResources().openRawResource(rawid); FileOutputStream out = null; try { out = new FileOutputStream(FILE_DIRECTORY_PA

在上述代码的帮助下,我在SD卡上复制了一些.xls文件,但当我尝试使用以下代码打开文件时,会出现错误,如“无法打开此文档”或“不支持文件格式”,但我转到“文件资源管理器”并尝试打开文件,但不会出错

InputStream in = getResources().openRawResource(rawid);
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(FILE_DIRECTORY_PATH + "/" + fileName );
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    byte[] buff = new byte[1024];
    int read = 0;

    try {
        while ((read = in.read(buff)) > 0) {
            out.write(buff, 0, read);
        }

        out.flush();
        out.close();
        in.close();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
         try {
            in.close();
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
打开文件

InputStream in = getResources().getAssets().open(fileName);
    OutputStream out = null;
    try {
        out = new FileOutputStream(FILE_DIRECTORY_PATH + "/" + fileName );
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    byte[] buff = new byte[1024];
    int read = 0;

    try {
        while ((read = in.read(buff)) > 0) {
            out.write(buff, 0, read);
        }

        out.flush();
        out.close();
        in.close();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
         try {
            in.close();
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
因此,变化如下

  • 我把文件放在资产文件夹中
  • 我们需要使用OutputStream而不是FileOutputStream
  • 获取文件路径时发生更改

  • 您是否安装了管理xls文件的软件?代码看起来不错。你有什么例外吗?写入后,您可以检查ti文件的大小吗?你关了两次门。删除flush()调用后的一个。是的,我有两个应用程序,如果我通过文件资源管理器,这两个应用程序会打开复制的文件。错误有点像不受支持的文档格式…然后你知道为什么它不工作。但是当我通过文件资源管理器时,会打开同一个文件??为什么?对不起,我误解了你写的东西。如果引发ActivityNotFoundException,则“application/vnd.ms excel”可能是错误的,或者该文件不存在。尝试从文件\u目录\u路径+“/abc.xls”创建一个文件,并检查.exits()返回的文件
    InputStream in = getResources().getAssets().open(fileName);
        OutputStream out = null;
        try {
            out = new FileOutputStream(FILE_DIRECTORY_PATH + "/" + fileName );
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        byte[] buff = new byte[1024];
        int read = 0;
    
        try {
            while ((read = in.read(buff)) > 0) {
                out.write(buff, 0, read);
            }
    
            out.flush();
            out.close();
            in.close();
    
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
             try {
                in.close();
                out.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
        File temp = new File(GlobalValues.CSV_FILE_DIRECTORY_PATH
                + fileName);
    
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.parse("file://"+temp.getAbsolutePath() ), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        try {
            startActivity(intent);
        } 
        catch (ActivityNotFoundException e) {
            Toast.makeText(this, "No Application Available to View Excel", Toast.LENGTH_SHORT).show();
        }