How to remove A file from removable/secondary SD card - java

I want to delete a file from the removable sd card, I tried many ways but nothing did the job.
Tried:
file.delete();
and
File file = new File(selectedFilePath);
boolean deleted = file.delete();
and
DocumentFile documentFile = DocumentFile.fromFile(file);
documentFile.delete();
and
DocumentsContract.deleteDocument(context.getContentResolver(),
Uri.fromFile(file );
none of which deletes the file

I want to delete a file from the removable sd card
In general, you can't.
If you put the file in one of the Context locations (getExternalFilesDirs(), etc.), then you should be able to delete it, using delete() on a File object.
If this is some other file, you do not have permission to do anything with it, including delete it.
And on Android Q, you will not have much access to external or removable storage at all.
You are welcome to use the Storage Access Framework (e.g., ACTION_OPEN_DOCUMENT, ACTION_OPEN_DOCUMENT_TREE) and work with content that way.
i am trying to work on file explore
Android Q is severely restricting that entire app category. I recommend that you build something else.

Related

using and/or extracting expandable files (obb) android

I was able to upload an expandable file and download it
on my app from google play. Following the o[fficial tutorial][1]
It saved the obb file to /Android/Obb/main.2.myappname.obb
I assumed this obb file would be extracted on assets or raw folder or
something similiar. The obb file was renamed after I zipped about 20 .mp3
files. They are named like so: 1.mp3, 2.mp3 etc.
Is it possible to extract the obb file if not how will I be able to
access the invididual mp3 files, the way I am able to access them from
assets or raw folder. Let's say I want to access 1.mp3 which is present
on the .zip archive that was renamed to main.2.myappname.obb
Thank you.
I'm surprised that no one has chimed in here. You can use the StorageManager to mountObb(), unmountObb(), getMountedObbPath(), and isObbMounted(). After mounting an Obb and calling getMOuntedObbPath, you can use the path returned to do normal reads from it. It acts as a virtual drive. You just cannot write back to it.
Here's the link to the documentation: https://developer.android.com/reference/android/os/storage/StorageManager.html
You can read from the OBB directly via an InputStream using the APK Expansion Zip Library. Documentation:
https://developer.android.com/google/play/expansion-files?hl=ko#ZipLib
Sample code:
try {
String obbFilePath = mContext.getObbDir() + "/" + "main.{package_name}.{version_code}.obb";
ZipResourceFile expansionFile = new ZipResourceFile(obbFilePath);
InputStream inputStream = mExpansionFile.getInputStream("1.mp3");
} catch (Exception e) {
Log.d(TAG, "Exception loading obb: " + e);
}
In place of the last line, since you are streaming a MP3, using MediaPlayer is a better idea.

Use box java sdk to download an item without knowing if it's a file or a folder

With the ID of an item stored on box, you can download it if it's a file, or download its contents if it's a folder. Either way it seems you need to know what sort of thing you're downloading in order to access it, doing either BoxFile file = new BoxFile(api, id); or BoxFolder folder = new BoxFolder(api, id); before handling the actual download.
I was hoping to be able to do something like
BoxItem boxItem = new BoxItem(api, id);
if (boxItem instanceof BoxFile) {
// download file
} else if (boxItem instanceof BoxFolder) {
// download all files in folder
}
sort of like the example in the docs of downloading a folder's contents. However, in that case the ID is that of a specific folder, whereas my ID is for either a folder or a file in the root folder, and I don't want to loop through all of the root folder's contents. And, anyway, BoxItem cannot be instantiated.
How can I tell ahead of time whether I'm downloading a file or a folder, with just the item's ID? If not, is there a way to download the item anyway?
I don't believe the API supports downloading an entire folder (and therefore the SDK doesn't either). The closest you can get is downloading all of the folder's files. That's why BoxFile has a download() method, but BoxFolder and BoxItem don't.
As for checking whether or not an ID corresponds to a file or folder - there isn't a way to tell without trying to make an API request. For example, you could try doing new BoxFile(api, id).getInfo() and seeing if it returns a 404.

Extracting files from res folder in Executable JAR (txt files to be specific)

I would like to ask if its possible to put text files into my jar, I use them to make my map in my game, but users can get Highscores. now I want to save the Highscores with the map, so I have to save the map on the user their PC. Is there any way how I could do this? I've searched the internet for some ideas but I could not find anything that even came close to what I've wanted. I only had 3/4th of a year java so I don't know much about these things, everything that happens outside the debug of eclipse are problems for me(files are mainly one of those things, null exceptions, etc).
The main question now.
Is it possible to do? If yes, do you have any terms I could search on, or some sites/guides/tutorials? If no, is there any other way how I could save the highscores?
EDIT:
to make clear
Can I get the text file (the text inside the file) to be extracted to a different file in like the home directory of my game (where I save the settings and stuff) the basic maps are inside the jar file, so I want them to be extracted on the first start-up of the program
Greetings Carolien
"extracted to a different file in like the home directory of my game (where i save the settings and stuff) the basic maps are inside the jar file, so i want them to be extracted on the first startup of the program"
You can get the URL by using getClass().getResource()
URL url = getClass().getResource("/res/myfile.txt");
Then create a File object from the URI of the URL
File file = new File(url.toURI());
Then just perform your normal file operations.
if (file.renameTo(new File(System.getProperty("user.home") + "\\" + file.getName()))) {
System.out.println("File is moved successful!");
} else {
System.out.println("File is failed to move!");
}
Assuming your file structure is like below, it should work fine
ProjectRoot
src
res
myfile.txt
Note: the above is moving the entire file. If you want to extract just the data inside the file, then you can simple use
InputStream is = getClass().getResourceAsStream("/res/myfile.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
The just do normal IO operation with the reader. See here for help with writing the file.

Where does Gdx search when i use Gdx.files.local(path)?

I'm using libgdx for a game. I want to read/write text files to update highscores, very simple.
I have three files already created placed in "$ANDROID-PROJECT/assets/data" , but Gdx seems to not find them when i use Gdx.files.local("data/file.txt"), but it does when i use Gdx.files.internal("data/file.txt"). Because i need to also write the file, i'm forced to use Gdx.files.local.
Can anyone tell me Where does Gdx search when i use Gdx.files.local(path) ? what path should i specify ?
code:
//for read
FileHandle file = Gdx.files.local("data/scoresCla.txt");
clasico = Float.parseFloat(file.readString());
//for write
FileHandle file = Gdx.files.local("data/scoresCla.txt");
file.writeString(String.valueOf(res), false);
if anybody have this problem, i solved by changing Gdx.files.local for Gdx.files.external. This way the files are stored in the SD Card, it has the same functionality of R/W files as local but in the SD Card.

Delete file in internal memory from Android device?

I am trying to delete a file stored in internal memory. The file does gets deleted by using
activity.deleteFile(filename);
but only in emulator. On the actual device the method always returns false. When I try to access the file from adb shell there is permission denied being displayed. So, I guess there is permission related issue with deleting the files in internal memory.
Can someone let me know how to actually delete the file from internal memory in Android?
If you're talking about just any file in the file system... Does this not work?
if (new File("fileUrl").delete()) {
// Deleted
} else {
// Not deleted
}
Due to security constraints you can only delete files that were created by your app. You also can not delete files that are part of your app package (apk), i.e. files in /res, /assets, etc..
Here FileName is the name of File which you want to delete and without path separator.that mean FileName should not contain path separator like "/".
And File should be created by your application.
My Problem was solved by that code..
if(getApplicationContext().deleteFile(FileName))
{
Toast.makeText(getApplicationContext(),"File Deleted",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(),"Not Deleted",Toast.LENGTH_SHORT).show();
}
You should use:
_context.openFileOutput(fileName, Context.MODE_WORLD_READABLE);
when writing the file

Categories

Resources