I want to save logs on Android using tinylog and I have added dependencies and configuration file. Sometimes the file is created with content and other times the file is empty. It seems like random behavior. Also the app has writing and reading permissions.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I have added this on graddle.
implementation 'org.tinylog:tinylog-api:2.1.0'
implementation 'org.tinylog:tinylog-impl:2.1.0'
I have put my tinylog.properties file inside src/main/resources
writer1 = logcat
writer1.level = debug
writer2 = rolling file
writer2.level = trace
writer2.format = {date} [{thread}] {level}:\n{message}
writer2.file = /storage/emulated/0/app/logs/log_{date:yyyyMMdd}.txt
writer2.charset = UTF-8
writer2.append = true
writer2.policies = daily: 03:00
writingthread = true
Related
val jsonString_ = File("C:/Users/admin/Document/sample.json").readText()
System.out.println(jsonString_)
//or this
val jsonString = applicationContext.assets.open("../res/sample.json")
.bufferedReader()
.use { it.readText() }
System.out.println(jsonString)
I tried both codes but I get "No such file or directory" or "java.io.FileNotFoundException" errors.I tried Intellij, it worked but it doesn't work on android
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:requestLegacyExternalStorage="true"/>
I added them to the manifest file,nothing changed.
Target SDK in my project is 31 and when I'm trying to delete a file from DCIM directory, I'm getting following error such as delete denied by permission:android.permission.ACCESS_MEDIA_PROVIDER.
Please consider file path like this: "/storage/emulated/0/DCIM/Screenshots/pic1.jpg"
I have tried so much to find the result every where including stack overflow, but didn't succeeded yet. Even I changed target SDK to lower 30 but not worked.
Following are the solutions that I had already worked on but nothing works:
1.flags in manifest file
android:requestLegacyExternalStorage="true"
android:preserveLegacyExternalStorage="true"
2.permissions in manifest file
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="40"
tools:replace="android:maxSdkVersion" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
Please find the attached code
val fileToDelete = File("/storage/emulated/0/DCIM/Screenshots/pic1.jpg")
fileToDelete.delete()
NOTE: filepath is hardcoded only to explain better
Please find the attached log
2022-03-21 11:22:35.331 8639-20226/com.filepickerdemo D/ContentResolver: delete denied by permission:android.permission.ACCESS_MEDIA_PROVIDER#content://media/external/images/media#_data = ?#/storage/emulated/0/DCIM/Screenshots/pic1.jpg
What else should I put in my code to solve this issue. Thanks in advance.
To delete media files in android 10 and above, You can try contentresolver API.
You need to pass the media's content uri to the 'contentResolver.delete()' method & you will be able to do it easily.
Here is my post on how to do that using Java - Scoped Storage in Android — Writing & Deleting Media Files
Hello I am trying to create a csv file in my android application. My code works when I try to run it on android 10 below. But I cant seem to find a problem why I cant do it on devices that are Android 11 already.
Here is my Manifest Permissions:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<application
...
android:requestLegacyExternalStorage="true">
Here is my code where I create a folder inside the download folder
#NonNull
public static File getStorageDirectory() {
if (Environment.getExternalStorageState() == null) {
File f = new File(Environment.getDataDirectory().getAbsolutePath() + "/Download/(Project Name)/");
if(!f.exists())
f.mkdirs();
return f;
} else {
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download/(Project Name)/");
if(!f.exists())
f.mkdirs();
return f;
}
}
Here is my code on how I create my csv file
File baseDir = Utility.getStorageDirectory();
String fileName = pollutant.getStationName();
String date = Utility.convertDate(new Date().getTime(), "dd-MM-yyyy HH:mm:ss");
File csvFile = new File(baseDir, fileName + "(1hr - "+pollutant.getPollutantName()+")(" + date + ").csv");
FileWriter writer;
if(csvFile.exists() && !csvFile.isDirectory()) {
writer = new FileWriter(csvFile , true);
} else {
writer = new FileWriter(csvFile);
}
I am already creating a folder in the download folder in android 11 problem is when I am trying to do the create csv part program return a
java.io.FileNotFoundException: ... open failed: EPERM (Operation not permitted)
I am really having a hard time to fix my problem for devices with android 11
"dd-MM-yyyy HH:mm:ss"
You have a forbidden character in your file name.
A : is not allowed.
Change to "dd-MM-yyyy HH.mm.ss" or so.
I believe that this a permission issue. In Order to Write into files you need to allow it.
Here is example of the code:
public void onPermission()
{
// Permision can add more at your convinient
if ((ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) !=
PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(
this,
new String[]
{
Manifest.permission.READ_CONTACTS,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.BLUETOOTH,
Manifest.permission.CAMERA,
Manifest.permission.CALL_PHONE
},
0
);
}
}
Usage :
onPermission(); at onStart() or in the onAcitivty().
Alternatively you can use a library as well ,
Reference : https://github.com/Karumi/Dexter
For External Storage:
Add attribute in your app's AndroidManifest.xml file inside the application tag:
android:requestLegacyExternalStorage="true"
eg.
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:requestLegacyExternalStorage="true">
...
</application>
For More Reference : https://stackoverflow.com/a/61406983/10758321
I have a similar behavior in my “SFTP Server s0 v1” app and the problem respective the allowed characters in file- and directory-names is related to the underlying file system – see Comparison of file systems . You can check which file system is mounted to which directory by calling adb shell mount. For example creating a file in your application home directory (e.g. /data/data/ch.becke.sftp_server__s0_v1/files) is no issue because the /data directory is mounted as ext4 (/dev/block/dm-5 on /data type ext4 ...) which allows almost all characters in file names. Whereas the external storage directory (e.g. /storage/emulated/0 or /storage/140E-3C1A) is mounted as vfat or fuse which only allows a limited set of characters in file names. (in my app I alternatively also support managing files using scoped storage but I personally think this is even worse because it auto replaces all special characters with an underscore character _).
I am writing an android App. In the app I need access to the download directory \DOWNLOAD. I can read and write files within the app directory and I see the DOWNLOAD directory when I request the files within root directory.
But when I use
File[] files = new File("\DOWNLOAD").listFiles();
the result is not a valid array but null.
I added
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
to the AndroidManifest.xml and I enabled the Storage permission on the phone.
What else must I do to get access to the files in download directory?
Kind regards,
Wolfgang
It seems that adding
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
is not sufficient. Now I additionally request this permission on runtime with following code:
String[] requiredPermissions = { Manifest.permission.READ_EXTERNAL_STORAGE };
ActivityCompat.requestPermissions(this, requiredPermissions, 0);
and it works again :)
The solution for the problem was to add also
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
to the AndroidManifest.xml and to use
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).listFiles();
Thanks, for your help!
Add Storage read permission in manifest file
Refer: https://www.journaldev.com/9400/android-external-storage-read-write-save-file
https://gist.github.com/granoeste/5574148
String home = System.getProperty("user.home");
File file = new File(home+"/Downloads/" + fileName + ".txt");
Asking this questions after trying a lot. I have checked a lot ob blogs and questions.
When I'm trying to create a new folder in the device's doucments folder, its not creating the folder even after giving all permissions including runtime permissions.
This is my code to create the new folder -
var path = (Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS))
var directory = path.absolutePath + "/justCheck"
var file = File(directory)
if(!file.exists()){
file.createNewFile()
}
I'm checking on Android device with marshmallow OS. This is my permissions in Manifest -
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Also, I have authorized the runtime permissions.
Am I doing anything wrong ? Why is the older not getting created ?
You've to create a folder not a file:
file.mkdirs();