Android program crash and .android_secure - android

I was working on an mp3 player android project for varsity that so far just consists of a single activity that recursively adds all mp3 files on the phones sd card.
My app just kept crashing and after a lot of debugging I noticed the reason is that it finds a hidden directory /sdcard/.android_secure and when it attempts to call listFiles() on this directory the app crashes. I got around around the problem by excluding hidden files from being processed.
My question is what exactly is going on here? I assume my app doesn't have permission to access the contents of .android_secure. Is there a more robust way of scanning through an sdcard?
I've add the related code below. Any help is appreciated.
File storage = Environment.getExternalStorageDirectory();
addMp3Files(storage);
}
private void addMp3Files(File rootDir) {
for (File file : rootDir.listFiles()) {
if (file.getName().toLowerCase().endsWith(".mp3")) {
songs.add(file.getAbsolutePath());
} else if (file.isDirectory() && !file.isHidden()) {
addMp3Files(file);
}
}
}

The .android_secure folder is used for the App2SD feature. Files of apps that have been moved to SD card are stored here. If you check the permission bit you can see that it's ------; i.e., your application has no read permission of this folder. That's why it crashed.
To exclude files and folders that your application cannot read, such as .android_secure, you can check them with the file.canRead() method.

Related

How to remove LOST.DIR folder from USB Mass Storage in Android programmatically

I copy files from Android internal storage to USB drive, folder LOST.DIR always is created by system.
How can I remove this folder programmatically
I use library libaums to handle communicate with USB
I tried to delete this folder after copy but USB get error
private fun exportOnlyFilesToUsb(fileSystem: FileSystem, file: File) {
val root = fileSystem.rootDirectory
if (!file.isDirectory) {
root.search(file.name)?.delete()
val targetFile = root.createFile(file.name)
copyFile(file, targetFile)
return
}
file.listFiles()?.forEach {
exportOnlyFilesToUsb(fileSystem, it)
}
}
private fun copyFile(file: File, usbFile: UsbFile) {
if (file.isDirectory || usbFile.isDirectory) return
FileInputStream(file).use { input ->
UsbFileOutputStream(usbFile).use { output ->
input.copyTo(output)
}
}
}
// Then I delete
root.search(LOST_DIR)?.delete()
First check if you have write access with File.canWrite().
I think you have no write access to removable media since Android KitKat.
You would need SAF and let the user select the directory with ACTION_OPEN_DOCUMENT_TREE.
Yes it is a nuisance as the medium will be used in other devices which sometimes dont like that directory.
The same counts for an Android directory that is often created.
Yes remove all. Nobody asked for it.
LOST.DIR is a folder created by an Android phone on the SD card to get back accidentally deleted files like photos, videos, etc. If you are a smartphone user, you would have already seen LOST.DIR folders are located on your external SD card.
Lost.DIR folder basically helps you to recover files lost due to various reasons like application crash, improper ejection of the USB drive or SD card/ ejection of SD card during the reading/ writing process, Android abruptly frozen or shut down unexpectedly, interruptions while downloading files on SD card, etc.
Some users might delete Lost.DIR folder thinking that it is a virus folder, which is completely false. You might have observed that once you delete Lost.DIR folder, again it will be automatically recreated after you boot your phone, this is because LOST.DIR is the Android system folder.
So don't delete this. Actually, you can't do that.

Android: Application to manage files on SDcard

I have a problem with understanding how to manage files at SDcard in the tablet.
I have my own application in Kotlin (in manifest.xml I have a permission READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE) and I need to have a folder "SongBook" in the root of SDCard. In this folder will be stored audio files (mp3) and HTML files. I read files without problems, but I need to create and delete these files. Now I use a File.
var file = File(Path_to_file)
try { file.delete() }
catch (e: Exception) { println("exception: "+ e.toString()) }
To create a file and write data to it, I use FileWriter. As I wrote, my app has permissions, but the file isn't deleted (or isn't created a new file).
I know about Android 4.4 KitKat's idea that applications not have access to SDcard to write. Apps may use a 'providers folder' with all permissions. But this folder is deleted after uninstalling application - no way for me. Data stored in "SongBook" is my own (like photos or videos) and must stay stored on SDcard after application uninstall.
Please, I need help, is any correct way to do this (without rooting android device) and using a folder in the root of SDcard to read/write/delete/create files?
Thx for responses (and sorry for my English).

Protect my Android app's files from "cleaners" and "optimizer" apps?

A rare but reoccurring problem reported with one of my apps is that "all my files were deleted" (files referring to project files generated from said app, save files essentially).
The culprit ends up being those "optimizer" apps that clean "unwanted" files from your Android device. Because my app generates custom-file-extension files, maybe they're not recognized and deleted?
Regardless, I know of the .nomedia file, a file used to tell media players to ignore a directory when scanning for media. Is there a similar file to "protect" directories from automated modification/deletion? Maybe even something placed in the Android Manifest?
Save the files in the internal storage that your app is assigned to by the system. No other apps can access it as it is private to your app. If you save files in a public directory, any app can access that.
File dir = context.getFilesDir()
To get your app's storage 'partition'

android obb file location in internal memory for 4.0 - 4.4

Help please solve my problem with obb files, I add downloader_library to my project, when application start he download for me obb file, this worked good.
If file stored in external sd card no problem,app work, but when I try on telephone without sd card my app is crash. I look to method which look if obb file exists and I see he always look on sd card, How I can find my obb file in internal memory ?
This method looks if obb file exists:
static public boolean doesFileExist(Context c, String fileName, long fileSize,
boolean deleteFileOnMismatch) {
// the file may have been delivered by Market --- let's make sure
// it's the size we expect
File fileForNewFile = new File(Helpers.generateSaveFileName(c, fileName));
if (fileForNewFile.exists()) {
if (fileForNewFile.length() == fileSize) {
return true;
}
if (deleteFileOnMismatch) {
// delete the file --- we won't be able to resume
// because we cannot confirm the integrity of the file
fileForNewFile.delete();
}
}
return false;
}
This method - Helpers.generateSaveFileName use Environment.getExternalStorageDirectory();
fileForNewFile for 4.0 -
/mnt/sdcard/Android/obb/com.example.app/main.1.com.example.app.obb
fileForNewFile for 4.4 -
/storage/sdcard/Android/obb/com.example.app/main.1.com.example.app.obb
How I understand and read in internal memory obb must be somewhere in /data/data/com.example.app but how receive this directory ? And what real path:
/data/data/com.example.app/obb/main.1.com.example.app.obb ?
/data/data/com.example.app/Android/obb/main.1.com.example.app.obb ?
On phone (4.4.2) without sd card file manager show me path after download - /Android/obb/com.example.app/main.1.com.example.app.obb but phone without root and i don't see upper directory where is this Android folder.
I completely confused please help.
The directory you are trying to read (the one with data/data) is the getFilesDir() result. Can you try here: /mnt/obb/packagename/ with the regular read storage permission? The obb folder should already exist (at least for emulators).. Other paths should already be constructed if you tried to access folders using the tutorial given on the Android developers website.
Also you don't need root to access the files through ADB, and you can push/pull them through ADB as well with basic commands

android shared user id and reading/writing a file

I have been sufferring in one problem for several days.
Currently I'm running in the source code of "Settings" on Android2.2.
In AdroidMenifest.xml, we can see:
android:sharedUserId="android.uid.system"
With this, many permissions can be accessed for the activities in Settings.
But with this statement, the sd card can't be accessed for Read/Write, I have tried to read files in directory
File f = new File("/mnt/sdcard/"+filename);
or
File f = new File("/sdcard/"+filename);
But all of them don't work, I got an exception telling me that the file didn't exist (I have already put the file there).
If I delete android:sharedUserId="android.uid.system", then I can access the file successfully . However, I need the android:sharedUserId="android.uid.system"
to make the other activities run well.
Does anybody happened to meet the same problem, and have you solved it? Thanks!
The system user can not access the SD card, because if the SD card gets unmounted it may need to kill any processes that have files open on it and we don't want system processes being killed like that. If you want to access the SD card, you need to not use the system shared user ID.

Categories

Resources