Get list of files from specific Folder in Android Q - android

I am trying to access the specific folder in android Q but the app is getting crashed.
I am using below code which is working fine till Android PIE but it is not working in Android Q
val directory = File(path)
val files = directory.listFiles()
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Getting bellow error
NullPointerException: Attempt to get length of null array

With Android Q file interface can't be used to access public directories, unless you use the requestLegacyStorage in your app manifest (not raccomanded since it won't work anymore with Android R). To access the directory you need to use the DocumentFile class, for example:
DocumentFile f = DocumentFile.fromTreeUri(context, uri);
f.listFiles();

Related

Android Kotlin - Can not delete file from download folder

I am unable delete txt file from download directory. File.delete() does not work. I have also tried with context.deleteFile(), that does not work either. I am not getting any exception, just nothing happens. This works on many Android devices, but it does not work on the device that I needed it to work, which is Android device with embedded printer. Android version 8.1.
I have found this answer, stackoverflow, which is similar to official documentation Google.
I have tried to use Uri.fromFile(directory) and directory.toUri()
instead of MediaStore.Images.Media.EXTERNAL_CONTENT_URI from the example, since file is in download folder, but I got null pointer exception.
How I can get correct download folder Uri? Or is there another way to delete file?
Thank you in advance for any help and suggestions.
EDIT:
getAbsolutePath() = /storage/emulated/0/Download/print.txt
Code:
val downloadPath: File = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
// dowloadPath is passed as directory
fun readFile(directory: File, context: Context): String {
val fileToRead = File(directory, "print.txt")
//...
if (fileToRead.exists()) {
//...
fileToRead.delete()
}
}
Permissions:
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="32" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="29" />
And inside <application
android:requestLegacyExternalStorage="true"
android:requestRawExternalStorageAccess="true"

Flutter - Save a local text file visible for user on Android

I'm trying to create a text file in a folder where the user can see for the file explorer apps of the mobile device, preferably in Documents folder.
Here is what I've till now (consider that we have all required permissions)
final directory = Platform.isAndroid
? await getExternalStorageDirectory()
: await getApplicationDocumentsDirectory();
final filePath = '${directory!.path}/example.txt';
File file = File(filePath);
await file.create();
await file.writeAsString('Some random text content');
After that I try to find this new file on my file explorer apps, but it seems to not exists and no error logs can been seen.
If you want see an example code I wrote this PoC -> https://github.com/felipeemidio/save-local-file-poc
What I've tried so far?
If you try to execute file.exists() command it'll return true.
Use image_gallery_saver lib to "download" my file results on an error for trying to save a plain/text file where should only be image/* files.
Use getExternalStorageDirectory(type: StorageDirectory.documents) instead of getExternalStorageDirectory() changes nothing.
downloads_path_provider lib does not support null-safety.
If you set the directory path with the static value '/storage/emulated/0/Documents/example.txt', is possible to see the file through Archives app, but will not show when you select the option to see all Documents files.
Try file path as
String filePath = '/storage/emulated/0/Downloads/example.text';
and don't forget to add permissions in manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

PDF.js does not open pdf file android

I am trying to read local pdf file which is saved in Android download folder.
I created app and set everything like it should be and app reads pdf if it is saved in asset:
Control.LoadUrl(string.Format("file:///android_asset/pdfjs/web/viewer.html?file={0}", string.Format("file:///android_asset/abc.pdf")));
But if I use:
Control.LoadUrl(string.Format("file:///android_asset/pdfjs/web/viewer.html?file={0}", string.Format("file:///storage/emulated/0/Download/foo.pdf")));
nothing is showing.
I checked if file exist:
string abc;
if (File.Exists(string.Format("/storage/emulated/0/Download/foo.pdf")))
{
abc = "exist!!!!";
}
else
{
abc = "not exist!";
}
and it confirmes file exist.
If i use OpenPdf project: https://github.com/acaliaro/OpenPdf
then it can open file saved in Download folder.
What am I doing wrong? Different Android version? Api level? I compared OpenPdf project with my project, everything seems to be ok.
Permisions are set too:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
For everybody who has similar issue:
Solution is very trivial.
Apart from permissions adjusted in AndroidManifest.xml I had to go to phone aplications settings and turn on memory permission manualy.
Everything is working now.

A list of downloaded files is null

I'm trying to get a list of downloaded files, here's code in Kotlin:
val dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
val files = dir.listFiles()
I have files in the folder but this code returns null. How come?
Set the permission to read from external storage.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Android Permissions Issue

I am writing a simple activity to record and save audio, preferably to a folder within my application, but, for simplicity, to the SD card. The line of code that's giving me trouble is
String path = Environment.getExternalStorageDirectory().toString() + "/" + "tempAppFiles/";
String filename = "test"+".mp4";
recorder.setOutputFile(path + filename);
where recorder is an instance of MediaRecorder.
When I run the application, I get a permissions error that states
07-31 15:51:51.810: W/System.err(13670): java.io.FileNotFoundException: /mnt/sdcard/tempAppFiles/test.mp4 (Permission denied)
I looked this problem up and found that I needed to add several permission tags to my manifest, and I added
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
to my manifest.
I am still getting the same permissions issue, and I can't find anyone with a similar problem.
Any ideas?
Hmm... The permissions seem to be correct, perhaps you've put them in the wrong place, they need to be children of the root note .
Next you can check, whether you can write the file by checking
boolean canIWrite = path.canWrite();
As you also get a FileNotFound exception you could try...
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
...instead of...
String path = Environment.getExternalStorageDirectory().toString();
If that is no help at all, there is still an official example of capturing audio - you should compare your code to:
http://developer.android.com/guide/topics/media/audio-capture.html
tempAppFiles does not set well with me. Seems like you should be writing to a directory that is under the package name of the app you are writing ...

Categories

Resources