Java.io.File - Cannot rename and move file on Android -
i can't rename/move temporary file , open it
here code used create 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, (try to) save on 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 file"); // display on console } else { log.i("file rename", "filed renamed successfully"); } } }
the way create parent folder :
public static java.io.file getfolderstoragedir(string foldername) { // directory 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 message on console : "can not rename file". file.renameto(mynewfile)
not work..
the path
seems :
d/file path﹕ /storage/emulated/0/documents/12047/4691_test.pdf
here androidmanifest.xml
<uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="android.permission.read_external_storage" />
result : parent folder created not file ...
any idea problem ?
try instead of using raw file constructor, use method getfilestreampath provided context. say, do:
file oldfile = ctx.getfilestreampath("shoppinglists.tmp"); file newfile = ctx.getfilestreampath("shoppinglists.csv"); oldfile.renameto(newfile);
the problem presumably new file() refers name relative program's current directory, not, , not guaranteed be, directory in internal files stored.
Comments
Post a Comment