Android: File name not changing programmatically - android

I tried to change the file name of a song by programmatically by using the code
File from = new File(filedirectory,currentName+type);
File to = new File(filedirectory,newName+type);
from.renameTo(to);
But after executing the code the current file is not show in the music list, also the new one is not showing in the list.
The file path and the file name are:
filedirectory: /storage/emulated/0/media/audio/music/
newName: aaaa.m4a
currentName: bbb.m4a
In the manifest I given all the necessary permission and am executing the code in Kitkat.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
After rename the file, I checked whether the file exists or not by pragmatically, the result is file not exists with the new name.

Have you ever tried to use "adb shell" and "cd /storage/emulated/0/media/audio/music/", then list file and check the files there? Whether the filename has been changed or not?
If the filename changed, then it is an issue of sync in the Music Player.
I have ever met issue that I changed the photo album, but cannot get the changes in my App by get system photo album through content provider. I have no idea how to solve it, and it disappears after phone restarted.

Related

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.

Can't create new file path in External Storage

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.

file.lis() returns "null" although it shouldn't

I'm working in a listActivity and I'm trying to show the children of a given directory. Here is what I've tried to get the children of a given directory :
File currentFile = new File(Environment.getExternalStorageDirectory(), "Pictures");
String[] children = currentFile.list();
In fact, "children" is null and I don't know why. When I ask the absolute path of "currentFile", the answer is "/storage/emulated/0/Pictures".
Thanks for your help!
Make sure the folder exists, and check if you have added the permission to your manifest.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
and if you are running on Android M, you would need to request runtime permission
https://developer.android.com/preview/features/runtime-permissions.html#coding

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