Android: Application to manage files on SDcard - android

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).

Related

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 Understand Storage Paths

I want to save file on phone. new File(directory, myCustomFolder).
My files are large and MP3
1 - Environment.getExternalStorageDirectory() is not always access to save file. On some phones that don't have sd card not available and return nothing. :(
2 - getFilesDir() after unistall my app my files remove. (Is not the right place to store files) :(
3 - getCacheDir() after unistall my app my files remove. (Is not the right place to store files) :(
4 - getDataDirectory() after unistall my app my files remove. (Is not the right place to store files) :(
Now, How do I distinguish the right place and secure and always available to save my files?
I need a SD card if there was not, I have my files stored on internal memory and the public(Public means no save to data app folder). ?!!
Apps like WhatsApp and Telegram how to make folder into two external memory and internal memory?
Please help me.Thanks
You can always create a folder for your app in the Downloads folder. It will always be there. You can access it via Environment.getExternalStoragePublicDirectory(Environment.DI‌​RECTORY_DOWNLOADS). And since you are saving files, it makes it even a more appropriate place to store it, don't you think so?
Environment.getExternalStorageDirectory() GOOD WORKING.
solution is: android API 23+ need check dangerous permission:
https://developer.android.com/training/permissions/requesting.html

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

How to delete file from SDcard when uninstalling the application

I have application that record a sound, I want to delete this sound file when uninstalling this application. how is that possible ?
You should store the files in the internal storage. Thus, when the application is removed, so are the files.
http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
There are 2 ways to saved the file so it can be deleted upon uninstall
on the SD-card:
in an app only file note that it is deleted automatically only on API 8 and above.
// find or create private file need to be in a Context object
File root_private_SD_Card_file = getExternalFilesDir(null);
File myOwnFile = getExternalFilesDir("MyOwnFile") //
String path = myOwnFile.getAbsolutePath()
this method also hides the image video and audio files from other applications
2.
by storing directly to the device's storage from within a context
openFileInput(fileName);
[android developer's link][1]
This method can be problematic because device storage can be limited or occupied.
If the minSdk of your application is 8 you can use
context.getExternalFilesDir(null)
as the doc stands:
Returns the absolute path to the directory on the external filesystem
(that is somewhere on Environment.getExternalStorageDirectory()) where
the application can place persistent files it owns. These files are
private to the applications, and not typically visible to the user as
media.
When you uninstall the application, the related directory would be deleted (tipically the root would be com.yourdomain.yourappname).
If you have a Samsung phone with Android ICS, that does not work (I think it is a bug)

Android program crash and .android_secure

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.

Categories

Resources