file.delete() error 30 - android

I try to delete a text file I created with:
FileOutputStream fOut = openFileOutput("game.txt", MODE_PRIVATE);
by using
File file = new File(filename);
if (!file.exists())
{
Log.d(TAG,"gonna delete game.txt");
file.delete();
}
but keep getting error message:
Unable to unlink '/game.txt': Read-only file system (errno=30)
Do you know what's wrong here?

now it works well. Filename did not contain full path to file (although it's in root directory) it has to have full path

File file = new File(filename);
if (file.exists()){
Log.d(TAG,"gonna delete game.txt");
file.delete(); }
Try this.

Related

How to create sub folders under cache of an application using getExternalCacheDir()?

In my application I want to save some documents in my application cache. After use I want to clear that files from cache. If I clear the cache all saved data's will be loss but I don't want that. I need to create a sub folder under my cache and I want to read data's from that cache folder also after certain uses I wanted to clear data's present in that particular folder.
How can I do that using getExternalCacheDir()?
I tried using following snippet,
File tempFile = new File(AppDelegate.sharedDelegate().getExternalCacheDir()+"\documentfolder\", name + ext);
OutputStream outputStream = new FileOutputStream(tempFile);
IOUtils.copy(inputStream, outputStream);
outputStream.close();
return tempFile;
But I am getting
System.err: java.io.FileNotFoundException: /storage/emulated/0/Android/data/com.package.debug/cache/documentcache/Screenshot_20171108-011217.png.png (No such file or directory)
W/System.err: at java.io.FileOutputStream.open(Native Method)
W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:169)
How can I solve it?
getCacheDir(). It returns the absolute path to the application specific cache directory
File myDir = new File(getCacheDir(), "yourfolder");
myDir.mkdir();
then create file using below code.
File f = new File(your directyorypath + "filename.txt");
if (f.exists())
{
f.delete();
}
f.createNewFile();
This should work:
File dir = new File(AppDelegate.sharedDelegate().getExternalCacheDir(), "documentfolder");
dir.mkdirs();
then use dir as parent folder for your file:
File tempFile = new File(dir, name + ext);
OutputStream outputStream = new FileOutputStream(tempFile);
IOUtils.copy(inputStream, outputStream);
outputStream.close();
return tempFile;

FileNotFoundException : Can't Open file, file not found in Android

I am going to create one .pdf file and I am getting file not found exception. I have written WRITE_EXTERNAL_STORAGE permission for that in AndroidManifest.xml file and also provided runtime permission for Marshmallow device.
Below is my code:
File SDCardRoot = Environment.getExternalStorageDirectory();
//create a new file, to save the downloaded file
File file = new File(SDCardRoot + "xyz/", pdfFilename);
if (!file.exists()) {
file.mkdir();
}
OutputStream fileOutput = new FileOutputStream(file);
docWriter = PdfWriter.getInstance(doc, fileOutput);
Can you please get me out?

Android Subfolder sdcard

I create a folder called "res" inside sdcard
File f = new File(Environment.getExternalStorageDirectory() + "/res/");
f.mkdirs();
The folder is successfully created.
But when I try to create a file within the folder does not work.
String string = "hello!";
File file = new File(Environment.getExternalStorageDirectory() + "/res/","test.txt");
FileOutputStream fos = new FileOutputStream(file);
fos.write(string.getBytes());
fos.close();
LOGCAT
java.io.FileNotFoundException: /mnt/sdcard/res/test.txt (Not a directory)
The call to new File() doesn't actually create the file. You need to check if the file exists by using file.exists(). If it doesn't, you must create the file by calling file.createNewFile(). See this answer for sample code: https://stackoverflow.com/a/7012122/379245

Android - downloading file, java.io.FileNotFoundException

I may be way off here, but I am trying to download a file and store it in the downloads folder on my phone. I am getting a "java.io.FileNotFoundException" error, because the file doesn't exist, because I'm trying to download it...what am I doing wrong?
String PATH = Environment.getExternalStorageDirectory() + "/download/";
File dir = new File(PATH);
dir.mkdirs();
File outputFile = new File(dir, "downloadFile");
FileOutputStream fos = new FileOutputStream(outputFile);
This fails, with the following:
java.io.FileNotFoundException: /mnt/sdcard/download/downloadFile (Permission denied)
I am using the WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE permissions....
Please try following updated code,
String PATH = Environment.getExternalStorageDirectory() + "/download";
File dir = new File(PATH);
dir.mkdirs();
File outputFile = new File(dir, "downloadFile");
if ( !outputFile.exists() )
{
outputFile.create();
}
FileOutputStream fos = new FileOutputStream(outputFile);
There is a "/" after the download. This way Android is thinking that you are creating Recursive Directory. While using mkdirs(), you can not create recursive directories.
You can also check my answer for same in Java ME here.
Add Permission to write external memory in your Manifest file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Here you want to make directoy?
it there is already folder of download than use this code directly.
// create a File object for the parent directory
File Directory = new File("/sdcard/download/");
Directory.mkdirs();
// create a File object for the output file
File outputFile = new File(Directory, downloadFile);
// now attach the OutputStream to the file object, instead of a String representation
FileOutputStream fos = new FileOutputStream(outputFile);
It might be wise to use Environment.getExternalStorageDirectory() for getting the "SD Card" directory as this might change

Android create folders in Internal memory issue

I have a little issue with creating folders for my application in Internal Memory. I'm using this piece of code :
public static void createFoldersInInternalStorage(Context context){
try {
File usersFolder = context.getDir("users", Context.MODE_PRIVATE);
File fileWithinMyDir = new File(usersFolder, "users.txt"); //Getting a file within the dir.
FileOutputStream out = new FileOutputStream(fileWithinMyDir); //Use the stream as usual to write into the file.
File dataFolder = context.getDir("data", Context.MODE_PRIVATE);
File fileWithinMyDir2 = new File(dataFolder, "data.txt"); //Getting a file within the dir.
FileOutputStream out2 = new FileOutputStream(fileWithinMyDir2); //Use the stream as usual to write into the file.
File publicFolder = context.getDir("public", Context.MODE_PRIVATE);
File fileWithinMyDir3 = new File(publicFolder, "public.txt"); //Getting a file within the dir.
FileOutputStream out3 = new FileOutputStream(fileWithinMyDir3); //Use the stream as usual to write into the file.
} catch(FileNotFoundException e){
e.printStackTrace();
}
}
So folders are created but in front of their name there is the beginning "app_" : app_users, app_data, app_public. Is there a way to create the folders with the name given by me? And another question : I want to first create folder Documents and than all other folders "Data, Public, Users" on it.... And the last question : How can I give the right folder path if I wanted to create a file in Documents/Users/myfile.txt in Internal Memory?
Thanks in advance!
You can use this :
File myDir = context.getFilesDir();
String filename = "documents/users/userId/imagename.png";
File file = new File(myDir, filename);
file.createNewFile();
file.mkdirs();
FileOutputStream fos = new FileOutputStream(file);
fos.write(mediaCardBuffer);
fos.flush();
fos.close();
Is there a way to create the folders with the name given by me?
Use getFilesDir() and Java file I/O instead of getDir().
How can I give the right folder path if I wanted to create a file in Documents/Users/myfile.txt in Internal Memory?
Use getFilesDir() and Java file I/O, such as the File constructor that takes a File and a String to assemble a path.

Categories

Resources