获取本地Android项目文件的文件路径

7

我想以编程的方式访问一个特定的文件,该文件将包含在我的项目文件夹中。有没有办法做到这一点?如果可以,我应该把文件放在项目文件夹的哪个位置,并且如何编写简单的代码来获取它的文件路径?

private void saveFileToDrive() {
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    java.io.File spreadsheet = new java.io.File("Untitled spreadsheet.xlsx");
                    String filePath = spreadsheet.getAbsolutePath();
                    System.out.println("file path is"+filePath);

                    URL fileURL = getClass().getClassLoader().getResource("Untitled spreadsheet.xlsx");
                    String filePath2 = fileURL.getPath();
                    System.out.println("file path2 is"+filePath2);

                    java.io.File fileContent = new java.io.File(filePath);
                    FileContent mediaContent = new FileContent("application/vnd.ms-excel", fileContent);

                    File body = new File();
                    body.setTitle(fileContent.getName());
                    body.setMimeType("application/vnd.ms-excel");


                    File file = service.files().insert(body, mediaContent).setConvert(true).execute();

                    if (file != null) {
                        showToast("File uploaded: " + file.getTitle());
                    }
                    else
                             ;
                } catch (UserRecoverableAuthIOException e) {

                    startActivityForResult(e.getIntent(), REQUEST_AUTHORIZATION);
                } catch (IOException e) {

                    e.printStackTrace();
                }
            }
        });
        t.start();
    }

1
请注意,被接受的答案是错误的。 - Chris Stratton
3个回答

5

将文件放在项目的根目录中。然后获取文件的URL、路径和其他细节:

   File file = new File("test.txt");
   String filePath = file.getAbsolutePath();

编辑: 如果文件在类路径中(例如将文件放在“src”文件夹中,并确保在编译后将其移到“bin”或“classes”文件夹中),则可以使用另一种方法:

  URL fileURL = getClass().getClassLoader().getResource(fileName);
  String fileName = fileURL.getFile();
  String filePath = fileURL.getPath();

1
@JohnRoberts 你在使用某个IDE(例如eclipse)运行你的程序吗? - Yogendra Singh
1
@JohnRoberts 你需要访问文件路径还是文件本身? - Yogendra Singh
1
@JohnRoberts 已更新答案并提供了可用代码。现在有两个选项,都可以正常工作。请尝试其中任何一个。 - Yogendra Singh
这两种方法都会导致FileNotFoundException异常。对于第二种方法,我得到了以下错误信息:11-29 14:43:45.189: W/System.err(21133): java.io.FileNotFoundException: /file:/data/app/com.example.drivequickstart-2.apk!/Untitled spreadsheet.xlsx: open failed: ENOENT (No such file or directory)。对于第一种方法,我得到了以下错误信息:11-29 14:50:22.713: W/System.err(22064): java.io.FileNotFoundException: /Untitled spreadsheet.xlsx: open failed: ENOENT (No such file or directory)。此外,我注意到我的文件在第二种方法中没有被重新创建到bin文件夹中。 - John Roberts
1
这个答案不可行,因为它忽略了以这种方式打包的数据不是一个字面上的文件这一事实。 - Chris Stratton
显示剩余2条评论

0

1
这更适合作为评论和投票关闭:您只是在说“这取决于......”并链接到另一个答案。 - Sam
1
关键点在于,尽管这些机制使数据可用,但它不是java.io.File。 - Chris Stratton

0

所以你想访问应用程序内部的文件;而且你想直接这样做,也就是说,从Android上下文中(然后使用[android.|<package_name>.]R.<resource_type>.<resource_name>)。

你有两个选择: res/raw 文件夹或 assets/ 文件夹(在 res 父文件夹之外)。

要在两者之间进行选择,请参考 https://developer.android.com/guide/topics/resources/providing-resources.html 中的说明。

任意文件以其原始形式保存。要使用原始InputStream打开这些资源,请调用Resources.openRawResource()并提供资源ID,即R.raw.filename
但是,如果您需要访问原始文件名和文件层次结构,则可以考虑将某些资源保存在assets/目录中(而不是res/raw/)。assets/中的文件没有资源ID,因此只能使用AssetManager读取它们。
要直接访问res/raw/中的文件,即从Android上下文(然后使用[android.|<package_name>.]R.<resource_type>.<resource_name>),您可以像这样操作:
File file = new File("app/src/main/res/raw/country_data_from_world_bank.xml");
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接