0

I can't rename/move my temporary file and open it

Here is the code I used to create the temporary file

@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
   //[...]
   java.io.File tempFile = java.io.File.createTempFile("filetmp", "_handled", null);
   FileOutputStream fos = new FileOutputStream(tempFile);
   fos.write(responseBody); //responseBody (byte[]) not null
   fos.close();
   //[...]
}

Then, I (try to) save it on the disk

private void saveIntoDisk(java.io.File file) {
        if (PersitencyManager.isExternalStorageWritable()) {
            java.io.File dirEvent = this.getParentEvent().getDirectory();
            Log.d("ROOT PATH", "" + dirEvent.getAbsolutePath());
            java.io.File myNewFile = new java.io.File(dirEvent.toString() + "/"+identifiant+"_"+name);
            Log.d("FILE PATH", "" + myNewFile.getAbsolutePath());
            path = myNewFile.getAbsolutePath();
            if (!file.renameTo(myNewFile)) {
                Log.e("File Rename", "Can not rename this file"); // display on console
            } else {
                Log.i("File Rename", "Filed renamed successfully");
            }
        }
}

The way I create the parent folder :

public static java.io.File getFolderStorageDir(String folderName) {
        // Get the directory for the user's public pictures directory.
        java.io.File file = new java.io.File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOCUMENTS), folderName);
        if (!file.mkdirs()) {
            if (!file.isDirectory()) {
                Log.e("Directory_Creation", "Directory not created");
            }
        }
        return file;
    }

I get this message on my console : "Can not rename this file". file.renameTo(myNewFile) does not work..

The path seems to be good :

D/FILE PATH﹕ /storage/emulated/0/Documents/12047/4691_test.pdf

Here is my AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Result : The parent folder is created but not the file ...

Any idea about my problem ?

Denis Lukenich
  • 3,084
  • 1
  • 20
  • 38
Kevin Machado
  • 4,141
  • 3
  • 29
  • 54

2 Answers2

1

I found the problem..

I can't rename a file with a path that is located on other storage zone.

renameTo() : Both paths be on the same mount point. On Android, applications are most likely to hit this restriction when attempting to copy between internal storage and an SD card.

When I created the temporary file, I give null at the directory parameter and as Google said

directory : [...] null for the default location for temporary files, which is taken from the "java.io.tmpdir" system property. [...]

So my temporary directory was on internal storage than the SD Card Directory.

So, I modified the directory :

@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
   //[...]
   java.io.File dir = new java.io.File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DOCUMENTS), folderName);
java.io.File tempFile = java.io.File.createTempFile("filetmp", "_handled", dir.getAbsolutePath());
   FileOutputStream fos = new FileOutputStream(tempFile);
   fos.write(responseBody); //responseBody (byte[]) not null
   fos.close();
   //[...]
}
Kevin Machado
  • 4,141
  • 3
  • 29
  • 54
0

Try instead of using a raw File constructor, use the method getFileStreamPath provided by the Context. That is to say, do:

File oldfile = ctx.getFileStreamPath("shoppinglists.tmp");
File newfile = ctx.getFileStreamPath("shoppinglists.csv");
oldFile.renameTo(newFile);

The problem is presumably that new File() refers to a name relative to the program's current directory, which is probably not, and certainly not guaranteed to be, the directory in which internal files are stored.

Rename a file in the internal storage

Community
  • 1
  • 1
StephenG
  • 2,851
  • 1
  • 16
  • 36