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.
Related
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()
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.
I want to create a sub directory which is non-private under the Environment.DIRECTORY_PICTURES directory. I used the code shown below but of no avail. The directory is created but it remains private. I don't know where I'm wrong.
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyImages");
file.mkdirs();
File f = new File(file,"Image1");
First, you have not created a file, at least in the code that is shown above. You have created a Java File object, and you created a directory, but you did not create a file for Image1, and so your directory is empty. I know of no way to force your empty directory to be picked up by anything, though you should see it if you use adb shell or DDMS to examine your device.
When you do eventually write a file to this directory, be sure to call getFD().sync() on the FileOutputStream before you close() that stream. Then, use MediaScannerConnection and its static scanFile() method to have your newly-created file be indexed by the MediaStore. Until you do this, your newly-created file will not be visible via MTP or many third-party apps.
In Android, I want to create a pointer that points to already existing file by way of its filepath.
For example, my pseudo-code:
String path = "file/directory/filename";
File ptr = File's pointed to by the path
The Android documentation only provides methods with which one can create a new file through a file path, but I just want a File object that only points to an already existing File.
How do I do that?
Use:
String path = "file/directory/filename";
File ptr = new File(path);
// check If file exists.
if(ptr.exists()) {
// Your code Here if file exists
}
File f = new File(path);
f points to a virtual file , if the file doesnt exist, writing anything to it, will either create it or cause a system crash , depending on the type of content you are writing to it.
deleting a file that doesnt exist will also fail, so any operation with files must be encapsulated with try catch for IOException
see http://developer.android.com/reference/java/io/File.html
also on android's new Kitkat you explicity have to request the permission to READ_EXTRANL_STORAGE if you want to read, and WRITE_EXTERNAL_STORAGE if you want to write
I want to create .nomedia file in the internal cache directory where I will store images and tried the following..
File dir = getCacheDir();
File output = new File(dir, ".nomedia");
boolean fileCreated = output.createNewFile();
When I try this fileCreated is false and the file doesn't get created.
If I used nomedia without a dot the file gets created but this is no use as the MediaScanner is looking if the .nomedia file exists.
I also tried this with a FileOutputStream and to write data in the file in case it was because I was only created an empty file but this doesn't work either.
Anyone have an idea why this could be failing?
Hmm, are you really sure that the file not already exists? Please note that you need to do an ls -a to see a file starting with a dot.
The documentation of File.createNewFile() says:
Returns true if the file has been
created, false if it already exists.
If the file is not created for other reasons (i.e. security), it should throw an exception.