Im developing a Music player for which I need to retrieve all mp3 files from my storage. Im using getExternalStorageDirectory() function to obtain the external storage directory. But When I tried listfiles() on this path,its returning a null object. Im using a API Level 21 . And I have already gave
uses-permission in android manifest
both for reading and writing external storages,Even after doing this Im getting a null object.
Code:
File root= new
File(Environment.getExternalStorageDirectory().getPath());
Log.v("file exists ", root.exists()+""); Log.v(" file is
a directory",root.isDirectory()+""); Log.v("file can be
read",root.canRead()+""); Log.v("listfile value",root.listFiles()+"");
Outputs:
1.True
2.True
3.False
4.null
Directory is exists( I checked using isDirectory and exists() functions )
But canRead() function is returning false value, which means I dont have authority to read the directory, How to correct this?
Any solution?
Thanks in advance.
Related
val inputFile: File = File("/storage/emulated/0/Download/Ball - 113146.mp4")
// Check whether the input file exist or not
if (!inputFile.canRead()) {
throw FileNotFoundException("Unable to read $inputFile")
}
i'm expecting true from this canRead() function but it's always giving false. I have already taken the required permissions(READ_EXTERNAL_STORAGE).`
Your File does not exist.
Try to use file.exists() to check if the file exists or not.
Also, just the permission READ_EXTERNAL_STORAGE is not enough if you are targeting Android Version 11.
This is because you are trying to get a file in the "Downloads" folder, so you need to use Scoped Storage.
More infos here.
I'm [still] new on android development and about Java and about Kotlin (also an explanation in Java could be ok, however, I'm studying it also, Kotlin is prefered) and I'm struggling for deleting a simple downloaded file into the ExternalStorage.
Of course I enabled permission for read & write, and, even if this code returns a "True", I still can see the untouched file into my Download folder
here the code:
___UPDATE
// uri of my file in external storage ~/Download dir
var uri = Uri.parse (Environment.getExternalStorageDirectory().getPath() + "/Download/$myFilename$myExtensionVar")
// file object pointing at uri of file in external storage
val downloadedFile = File(uri.toString())
var deletedBool:Boolean = downloadedFile.delete()
println("myTag - deleted Boolean: $deletedBool")
if (deletedBool){
println("myTag - uri of file-to-be-deleted: $uri")
var secondStepToDelete:Int = context.getContentResolver().delete(uri, null, null)
println("myTag - second Step for deletion: $secondStepToDelete")
}
The file i am trying to rid of is a multimedia file (.mp3) and I added the second block of code (the one inside the IF statement) since I found that should work, having to do with the "DocumentProvider" (I'm new and I still don't know how to proper call its methods..) but, of course, It doesn't work at all.
I think I do need the ID (long type i guess) for the file stored into the external storage, however I haven't found yet how to get it
Thanks in advance for the help!
To build a File object, use the File constructor. To build a File object for a location off of a certain root directory, use the two-parameter File(File, String) constructor:
val downloadedFile = File(Environment.getExternalStorageDirectory(), "Download/$myFilename$myExtensionVar")
Unless you are getting a Uri from DownloadManager or something, there is no Uri that you need to delete().
I have more written here on external storage and how to work with it.
In Kotlin
file.deleteRecursively()
Is there any code to check whether or not the file's path is the filesytems ("/storage" or "/storage/emulated/0" or "/") in Android 7 ? Because when I call below code, it returns null in case the path of "folder" relates to filesystems in Android 7. I want to check to show some message like "This is the File System of Android" before calling below code.
File[] childFiles = folder.listFiles();
Use folder.exists(). Or folder.canRead().
But in Android 7 its impossible to list the "/" directory. And some other directories as you have seen.
Check childFiles for null and return if so.
Bad times ;-).
I am new to Android and mostly using snippets of code from other posts to build my project. I am having a hard time creating a new directory and file on my device. I am using the following code, but I am unable to verify the success of the creation of this path. I want to be able to mount my phone to my laptop and find a file named "MyRecording.pcm" in a folder "/My/Files". I am using the boolean value of mkdirs() to verify whether or not the path was created on my device. If that path was not created then my TextView will tell me "Directories do not exist"; otherwise, my code will create the file MyRecording.pcm. I keep getting an error/warning "result of mkdirs() is ignored". Please help.
File path = new File(Environment.getExternalStorageDirectory() + "/My/Files");
path.mkdirs();
if(!path.exists()) { statusText.setText("Directories do not exist");}
else recordingFile = File.createTempFile("MyRecording", ".pcm", path);
Do you have the permission set in your manifest?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Also, Android Studio is giving you the warning about mkDirs () because it returns a boolean indicating whether the directory was created. It's just reminding you that you never used the result. It doesn't matter.
This is my code to create a File Object. I am sure that the file is existing. However the file length() returns 0 and the exists() returns false too.
File uploadFile = new File(Environment.getExternalStorageDirectory() + "/DCIM/DSC00050.jpg");
int totalSize = (int) uploadFile.length(); // Get size of file, bytes
After Writing File uploadFile = new File(...); it will not create the file itself.
AFAIK If File.exists() is returning false, then file just doesn't exist yet.
but yes You can create file by calling file.createNewFile()
if exists() returns false means the file does not exist or not accessible. Make sure that the SD card is mounted and that your app has sufficient permission i.e. READ_EXTERNAL_STORAGE. I suspect this is the problem because you should get exception in that case.
The other thing is to log the absolute file path uploadFile.getAbsolutePath() and make sure that it is correct.
Edit:
Are you sure that your image is directly under the DCIM folder not DCIM/Camera ? Use any file browser in Android and check the file complete path. I do not see any other problems in the code.