0

I used a tutorial to download a zip into a subdirectory of my application's internal storage. I wrote the zip to /data/data/my.package.name/files/mySubDirectory/the.zip.

But, when I check to see whether the zip exists, it doesn't:

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

I verified that fileDirectory is the same path as the File outFile for the FileOutputStream.

What could be the problem?

boo-urns
  • 10,136
  • 26
  • 71
  • 107

3 Answers3

0

Try using getFilesDir() + "/" subdirectory + "/" "the.zip" Without the getabsolutepath(). That is what I used could be the issue.

OK maybe you problem is with permissions do you see the file in the DDMS under data/data/package/files ? Check the permissions for the files

Here is my code

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

I use the following to make the file readable

            @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);
                    }
            }
            }   
Ryan Heitner
  • 13,119
  • 6
  • 77
  • 119
0

Try getting your file path as below :

 String fileDirectory=Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "data" + File.separator + "data" + File.separator+ getActivity().getPackageName()+ File.separator +"mySubDirectory"+File.separator+"the.zip";
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
0

Using this SO question, I created a subdirectory using this example:

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

The problem is that I didn't expect the subdirectory to be prepended with "app_", so I was looking for the zip in the wrong place.

Community
  • 1
  • 1
boo-urns
  • 10,136
  • 26
  • 71
  • 107