How to use deleteFile() with argument that contains path separator? - android

In the application there is a deleteFile(String path) method implemented which has to delete the file located at the given path.
If the path contains path separator (character /), the method throws an exception, but to delete a specific file a full path has to be used and it contains a separator sign. Due to conflicting conditions I haven't found a way to successfully delete the file.
Does anyone know how to bypass the problem?

Without knowing what the exception is it is hard to say. In general it is best to avoid hardcoding path separators and instead use the File.separator symbol.
How are you going about deleting the File? The File class has a delete method; is that what you are using?
I'm guessing the problem might actually be unrelated to the things I mentioned up top, and instead be due to permissions. Are you trying to access files or directories that your program has no right to access?

According to javadoc of Application.deleteFile path couldn't contain path separator which is : at Unix and ; at Windows, so at least according to java doc you can delete file with a full path.
Edit:
'\' is File.separtor not pathSeparator.

Related

Where to place .glb file and how get path to it in android?

I'm trying to set up ARCore scene with Sceneform. There are this piece of code:
(supportFragmentManager.findFragmentById(R.id.single_player_ar_fragment) as ArFragment)
.setOnTapPlaneGlbModel(link)
It loads 3d-model with .glb extension by link, which is String. I can't understand where to put .glb file and how get a path to it. I tried to put it in assets or res folders, but in 2 hours i couldn't find how to get String path to it.
I know that these folders have open() methods, which returns InputStream, but setOnTapPlaneGlbModel() needs String with a path as an argument.
I had assumed that the files are in the same location as the sample:
https://github.com/SceneView/sceneform-android/tree/master/samples/3d-model-viewer/src/main/assets/models
Based on comment thread:
setOnTapPlaneGlbModel(Uri.parse(link).toString()) seems to resolve to the correct asset location and fix the issue.
References:
https://github.com/SceneView/sceneform-android/blob/master/samples/3d-model-viewer/src/main/java/com/google/ar/sceneform/samples/sceneviewbackground/MainActivity.java

How to check if a file is database in android programmatically?

How can I check If a file that I have selected from the list of files is database type or not in android ? The others files may be a text file, apk file or can be of any format. I assume not depending upon the extension of the file like .txt, .apk, .pdf etc as extension of the file can be changed also.
Thank You..!!
If you are using a subclass of SQLiteOpenHelper, you can create a new object with the file in question, and then call getReadableDatabase ().
If tha file is a database, no exception will be thrown.
Otherwise, a SQLiteDatabaseCorruptException will be trhown.
So you can surround your code with a try-catch block, and if an exception is thrown, then that file is not a database; otherwise, it is.
check by extension of file. Normally database file has extension as .db , .sql etc

How to store resource file with same name but different extensions in android

I am working on one static custom Android Media Player Application where i have many media files stored in raw folder.
Where as some files have same name but different extensions, here android is giving me
res\raw\then.mp4:0: error: Resource entry then is already defined.
res\raw\then.mp3:0: Originally defined here..
Can anybody please suggest me something on this?
Also there are few media files with java keywords like if, else, return, switch, case,class, else, final, long, new, this,true... where android is giving me error of invalid symbol.
Please suggest me solution for that also.
Thanks in advance...
As #blackbelt said, it's not possible, I'll just add that you can instead put your files in the assets directory instead of res.
You will not be able to use them like R.id.file, but you will get more flexibility.
simply you can't. Raw is build at compile-time inside R.java, and the name key must follow the java convention for naming. Since
if, else, return, switch, case,class, else, final, long, new,
this,true
are reserved keywords you can not use them.
Edit: R.java would look lie:
public static final class raw {
public static final int if=0x70000;

Edit text file programmatically

How can we modify a text file from android, such as
append at the end of the file.
clear data at any intermediate position and also blank lines in it.
Is there any option to delete a particular line from the text file, I read so many tutorials regarding this all have mentioned to create a temporary file and asked to copy the contents from the original file except that particular line, and to replace the original file with this temporary file.
Is there any alternative to do this with in that original file itself without creating a temporary file?
Thanks in Advance.
You can't do the second option programmatically, that's virtually impossible. The first option however is possible. Just use an InputStream Object to load your file, then you can get the text from the InputStream as a String, I believe, and then you can append the data at the end. Now that I think of it, it might be possible, but with a heck of a lot of work, to edit the text at the middle... But that's up to you to figure out...
Pretty much everything that is possible with java's file api's can be done in android
You can use a FileOutputStream, passing true for append and then using the normal output stream operations.
For changing files in a random-access manner, try RandomAccessFile. This allows you to change the current byte you are pointing to. However, if you delete data in this manner it won't automatically cause the file to contract to fill the empty space; you would need to do this manually and then change the length of the file via setLength().

Can anyone explain the File() parameters used to download file in android?

In Reference to this android file download problem
Can anyone explain what does this line mean in the code
FileOutputStream f = new FileOutputStream(new File(root,"Video.mp4"));
And what does it mean by the parameter root within the File().
Do I need to specify the root path to save the file?
If it is the case then how do we specify the root path in android ?
Regards
And what does it mean by the parameter root within the File(). Do I need to specify the root path to save the file? if it is the case then how do we specify the root path in android?
The code snippet from the question you linked doesn't define the variable, but if the code is downloading a file to the device, I would assume that it's a path on the SD card. Environment.getExternalStorageDirectory() will give you the root path to the SD card. You'll also need to specify the WRITE_EXTERNAL_STORAGE permission in your manifest.
If you're working on the emulator, you can create a virtual SD card when you create the emulator image.
The java.io.File(File, String) or java.io.File(String, String) are standard java constructors for Java. The first argument is just the parent directory path, while the second is the actual file name. If the file is in the current working directory or you know the full path as one string you can avoid the 2 argument constructors.
Since you are trying to download a file you can just acquire the file through a normal URL.openStream() to get an InputStream to get the contents of your downloaded file. For writing the data out you will follow the example you linked to to write the contents.
I'm unsure what the root variable was pointed to in the example. I'm not able to help you beyond this though since I have only gone through the first Hello, Android example myself.

Categories

Resources