0

我使用教程将 zip 下载到应用程序内部存储的子目录中。我把 zip 写到/data/data/my.package.name/files/mySubDirectory/the.zip.

但是,当我检查 zip 是否存在时,它不存在:

    String fileDirectory = this.getFilesDir().getAbsolutePath() + "/mySubDirectory/the.zip";
    File file = new File(fileDirectory);
    if(file.exists()) {
        Log.e(this.class.getName(), "file exists");
    } else {
        Log.e(this.class.getName(), "file doesn't exist");
    }

我证实这fileDirectoryFile outFile.FileOutputStream

可能是什么问题呢?

4

3 回答 3

0

尝试在没有 getabsolutepath() 的情况下使用 getFilesDir() + "/" subdirectory + "/" "the.zip"。那就是我使用的可能是问题所在。

好的,也许您的问题是权限问题您是否在 DDMS 中的 data/data/package/files 下看到该文件?检查文件的权限

这是我的代码

         String path = getFilesDir() + "/"
            + subDirName + "/";
            File file = new File(path);
            file.mkdirs();
            setReadable(file);

我使用以下内容使文件可读

            @TargetApi(9)
            private void setReadable(File file) {
                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                            try {
                                    Runtime.getRuntime().exec(
                                                    "chmod 777 " + file.getCanonicalPath());
                            } catch (IOException e1) {
                                    e1.printStackTrace();
                            }
                    } else {
                            file.setReadable(true, false);
                    }
            }
            }   
于 2013-01-20T06:27:07.213 回答
0

使用这个 SO question,我使用这个例子创建了一个子目录:

File mydir = context.getDir("mydir", Context.MODE_PRIVATE); //Creating an internal dir;
File fileWithinMyDir = new File(mydir, "myfile"); //Getting a file within the dir.
FileOutputStream out = new FileOutputStream(fileWithinMyDir); //Use the stream as usual to write into the file

问题是我没想到子目录会以“app_”开头,所以我在错误的地方寻找zip。

于 2013-01-20T06:53:37.437 回答
0

尝试获取您的文件路径如下:

 String fileDirectory=Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "data" + File.separator + "data" + File.separator+ getActivity().getPackageName()+ File.separator +"mySubDirectory"+File.separator+"the.zip";
于 2013-01-20T06:44:06.487 回答