本地Android项目文件获取文件路径 [英] Getting file path for local Android project files

查看:901
本文介绍了本地Android项目文件获取文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以编程方式访问,这将被包含在我的项目文件夹中的特定文件。有没有办法做到这一点?如果是这样,凡在我的项目文件夹我把文件,以及什么是一些简单的code,以获得它的文件路径?

 私人无效saveFileToDrive(){
        线程t =新主题(新的Runnable(){
            @覆盖
            公共无效的run(){
                尝试 {
                    java.io.File的小号preadsheet =新的java.io.File(无题US preadsheet.xlsx);
                    字符串文件路径= S preadsheet.getAbsolutePath();
                    的System.out.println(文件路径为+文件路径);

                    URL fileURL =的getClass()getClassLoader()的getResource(无题US preadsheet.xlsx)。
                    串FILEPATH2 = fileURL.getPath();
                    的System.out.println(文件路径2是+ FILEPATH2);

                    java.io.File的fileContent =新的java.io.File(文件路径);
                    FileContent mediaContent =新FileContent(应用程序/ vnd.ms-EXCEL,fileContent);

                    文件主体=新的文件();
                    body.setTitle(fileContent.getName());
                    body.setMimeType(应用程序/ vnd.ms-EXCEL);


                    文件fil​​e = service.files()插入(身体,mediaContent).setConvert(真).execute()。

                    如果(文件!= NULL){
                        showToast(文件上传+ file.getTitle());
                    }
                    其他
                             ;
                }赶上(UserRecoverableAuthIOException E){

                    startActivityForResult(e.getIntent(),REQUEST_AUTHORIZATION);
                }赶上(IOException异常E){

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

解决方案

把文件放在项目的根文件夹。然后得到的文件URL,路径和其他信息为:

 文件文件=新的文件(的test.txt);
   串文件路径= file.getAbsolutePath();
 

编辑:另一种方式(如果该文件是在类路径,例如把文件中的SRC文件夹中,并确保其移动的bin或类编译后的文件夹):

  URL fileURL =的getClass()getClassLoader()的getResource(文件名)。;
  字符串文件名= fileURL.getFile();
  串文件路径= fileURL.getPath();
 

I want to programmatically access a specific file which will be included in my project folder. Is there a way to do this? If so, where in my project folder do I put the file, and what is some simple code to get its file path?

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();
    }

解决方案

Put the file in root folder of your project. Then get the File URL, Path and other details as:

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

EDIT: Alternate way (if the file is in your classpath e.g. put the file in "src" folder, and make sure its moved in "bin" or "classes" folder after compilation):

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

这篇关于本地Android项目文件获取文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆